Fortran:灵活的数组过滤
在Fortran中,我们编码如下:
!vectors w,q are of the same size ... w = ... !a vector of integers [0,...,n) if (allocated(t)) deallocate(t); allocate(t(count(w/=0))) t = pack(q, w/=0) m = count(t>0) if (allocated(b)) deallocate(b) allocate(b(m)) b = pack(t,t>0)
在Python中翻译代码:
t = q[w!=0] b = t[t>0]
不确定我们所做的Fortran实现是否正确。请注意,它是一个非常大的程序的一部分,我们遇到了一些恼人的运行时错误,有时甚至没有消息但崩溃......
in Fortran we coded as the following:
!vectors w,q are of the same size ... w = ... !a vector of integers [0,...,n) if (allocated(t)) deallocate(t); allocate(t(count(w/=0))) t = pack(q, w/=0) m = count(t>0) if (allocated(b)) deallocate(b) allocate(b(m)) b = pack(t,t>0)
to translate a code in Python:
t = q[w!=0] b = t[t>0]
not sure the Fortran implementation we did is correct. Note that it is part of a very big program and we are getting some annoying runtime errors, sometime even no message but crash...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 F2003 中,将 t 和 b 声明为可分配向量并简单地编写:
这已经被几个编译器支持,例如 ifort 的 gfortran (GCC 4.6) (Intel 编译器套件 > 11.x)
In F2003, declare t and b as allocatable vectors and write simply :
This is already supported by several compilers like gfortran (GCC 4.6) of ifort (Intel compiler suite > 11.x)