Fortran ASSOCIATE 语法允许索引访问吗?

发布于 2024-09-05 04:17:47 字数 649 浏览 3 评论 0原文

有没有一种很好的方法来编写 Fortran ASSOCIATE 语句,将其变成

FORALL (i = 2:n-2)
    v(:,i) = v(:,i) + MATMUL(A, &
             c(2)*u(:,i-2) + c(1)*u(:,i-1) + c(0)*u(:,i) + c(1)*u(:,i+1) + c(2)*u(:,i+2))
END FORALL

如下所示的内容

ASSOCIATE ( U => ..., V => ...)
    FORALL (i = 2:n-2)
        V(i) = V(i) + MATMUL(A, &
               c(2)*U(i-2) + c(1)*U(i-1) + c(0)*U(i) + c(1)*U(i+1) + c(2)*U(i+2))
    END FORALL
END ASSOCIATE

我盯着 Adams 等人的 The Fortran 2003 Handbook 第 8.2 节,但我不知道如何编写关联名称 =>选择器 构造以允许对associate-name 进行索引访问。

显然,我想要的几行内容是多余的。我有一堆我想浓缩的东西。

Is there a nice way to write a Fortran ASSOCIATE statement to turn this

FORALL (i = 2:n-2)
    v(:,i) = v(:,i) + MATMUL(A, &
             c(2)*u(:,i-2) + c(1)*u(:,i-1) + c(0)*u(:,i) + c(1)*u(:,i+1) + c(2)*u(:,i+2))
END FORALL

into something like the following

ASSOCIATE ( U => ..., V => ...)
    FORALL (i = 2:n-2)
        V(i) = V(i) + MATMUL(A, &
               c(2)*U(i-2) + c(1)*U(i-1) + c(0)*U(i) + c(1)*U(i+1) + c(2)*U(i+2))
    END FORALL
END ASSOCIATE

I'm staring at Adams et al's The Fortran 2003 Handbook section 8.2, but I can't see how to write the associate-name => selector construct to allow for indexed access into the associate-name.

Obviously what I'm going for is overkill for a couple of lines. I've got a bunch of 'em that I'd like to condense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

樱花细雨 2024-09-12 04:17:47

除非我误读了事情,否则我认为这是不可能的。 规范说(第 8.1.4.3 节):

在 SELECT TYPE 或 ASSOCIATE 构造中,每个关联实体与其所属的实体具有相同的等级
关联的选择器。

据我所知,您需要一个 1 级关联实体 (V),并且需要一个 2 级关联实体
关联的选择器(用于保存v)。

Unless I'm misreading things, I don't think this is possible. The specification says (section 8.1.4.3):

Within a SELECT TYPE or ASSOCIATE construct, each associating entity has the same rank as its
associated selector.

and as far as I can see, you want a rank 1 associating entity (V) and will need a rank 2
associated selector (to hold v).

清醇 2024-09-12 04:17:47

如果目标是使代码更短/更好,我认为实现这一目标的最佳方法不需要 ASSOCIATE 构造:

forall(i=2:n-2)
  v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c([2,1,0,1,2]))
end forall

只要 5 大小的系数数组不改变,就可以预初始化为 a

real :: c5(5)
c5 = c([2,1,0,1,2])

然后在单行中运行,

forall(i=2:n-2) v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c5))

注意它必须是 lbound(u,2)<=0

If the aim is to make the code shorter/nicer, I think the best way to achieve that does not need an ASSOCIATE construct:

forall(i=2:n-2)
  v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c([2,1,0,1,2]))
end forall

which, as long as the 5-sized coefficient array does not change, could be preinitialized as a

real :: c5(5)
c5 = c([2,1,0,1,2])

And then run in a single line as

forall(i=2:n-2) v(:,i) = v(:,i) + MATMUL(A,MATMUL(u(:,i-2:i+2),c5))

note that it must be lbound(u,2)<=0.

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