Fortran90 到 C# 的转换问题

发布于 2025-01-05 04:55:48 字数 677 浏览 4 评论 0原文

我正在将一些 Fortran90 代码转换为 C#。我对Fortran77有一些了解,但对Fortran90不熟悉。我遇到了以下代码行,我不确定如何翻译。

C1 = real(product((/(-1,i1=1,m-1)/))*product((/(i1,i1=2,m)/)))

我认为这应该转换为:

int product1 = -1; int product2 = 1;
for (int i1 = 1 ; i1 <= (m-1); i1++)
{
    product1 *= -1;
}
for (int i2 = 2, i2 <= m; i2++)
{
    product2 *= i2;
}
float C1 = (float)(product1 * product2);

我的不确定性源于这样一个事实:存在用于初始化数组的隐含 do 循环构造;即,

A = (/2*I, I = 1,5/)

但我从未见过在相关 Fortran 语句中使用“产品”一词。我知道有一个用于向量或矩阵乘法的内在函数,称为 PRODUCT,但“product”在我正在使用的代码中不是数组,并且内在函数 PRODUCT 的语法使用 MASK,所以很明显我的语句没有使用此函数。

任何见解或帮助将不胜感激。谢谢。

I am converting some Fortran90 code to C#. I have some knowledge of Fortran77 but am not familiar with Fortran90. I have run across the following line of code that I am not certain how to translate.

C1 = real(product((/(-1,i1=1,m-1)/))*product((/(i1,i1=2,m)/)))

I am thinking this should be converted as:

int product1 = -1; int product2 = 1;
for (int i1 = 1 ; i1 <= (m-1); i1++)
{
    product1 *= -1;
}
for (int i2 = 2, i2 <= m; i2++)
{
    product2 *= i2;
}
float C1 = (float)(product1 * product2);

My uncertainty stems from the fact that there exists an implied do loop construction for initializing arrays; i.e.

A = (/2*I, I = 1,5/)

but I have never seen the word "product" used as in the Fortran statement in question. I know there is an intrinsic function for vector or matrix multiplication called PRODUCT but "product" is not an array in the code I am working with and the syntax of the intrisic function PRODUCT uses MASK so clearly my statement is not using this function.

Any insight or help would be greatly appreciated. Thank you.

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

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

发布评论

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

评论(1

病毒体 2025-01-12 04:55:48

如果您分解各个部分并打印它们,您会注意到这些只是使用简洁的矢量化术语创建的术语:

! given: (/(expr, start, end)/)
!
! (/(-1, i1=1, m-1)/) = vector, -1 repeated m-1 times
!
! (/(i1, i1=2, m)/)   = vector, 2..m
!
! Both are vectors with m-1 terms

另一件事需要注意的是 product() 不需要 3 个参数。第二个参数(要使用的维度)和第三个参数(数组掩码)不是必需的。

此时很明显,第一个产品实际上是 -1m-1,第二个产品是 m!.

因此,正确的(但不一定有效)翻译可能是:

// product((/(-1,i1=1,m-1)/)) => -1^m-1
double i = (m % 2 == 0 ? -1 : 1);

// product((/(i1,i1=2,m)/))   => m!
double mfact = i;
for (int jj = 2; jj < m; ++jj)
{
    mfact *= jj;
} // C1 = mfact;

简洁,与 F90 的“精神”接近,但效率很低:

double i = (m % 2 == 0 ? -1 : 1);
double C1 = Enumerable.Range(2, m)
                      .Aggregate(i, (x, y) => x * y);

If you decompose the parts and print them, you'll notice these are simply terms created using concise vectorized terms:

! given: (/(expr, start, end)/)
!
! (/(-1, i1=1, m-1)/) = vector, -1 repeated m-1 times
!
! (/(i1, i1=2, m)/)   = vector, 2..m
!
! Both are vectors with m-1 terms

The other thing to note is that product() does not require 3 arguments. The second argument (the dimension to use) and the third argument (an array mask) are not required.

At this point it becomes clear that the first product is actually -1m-1 and the second product is m!.

So, a proper (but not necessarily efficient) translation could be:

// product((/(-1,i1=1,m-1)/)) => -1^m-1
double i = (m % 2 == 0 ? -1 : 1);

// product((/(i1,i1=2,m)/))   => m!
double mfact = i;
for (int jj = 2; jj < m; ++jj)
{
    mfact *= jj;
} // C1 = mfact;

Succinct, close in "spirit" with the F90, but hardly efficient:

double i = (m % 2 == 0 ? -1 : 1);
double C1 = Enumerable.Range(2, m)
                      .Aggregate(i, (x, y) => x * y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文