Fortran:灵活的数组过滤

发布于 2024-12-26 15:12:42 字数 461 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2025-01-02 15:12:42

在 F2003 中,将 t 和 b 声明为可分配向量并简单地编写:

t = pack(q, w/=0)
b = pack(t,t>0)

这已经被几个编译器支持,例如 ifort 的 gfortran (GCC 4.6) (Intel 编译器套件 > 11.x)

In F2003, declare t and b as allocatable vectors and write simply :

t = pack(q, w/=0)
b = pack(t,t>0)

This is already supported by several compilers like gfortran (GCC 4.6) of ifort (Intel compiler suite > 11.x)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文