MINLOC 是否适用于从索引 0 开始的数组?(Fortran 90/95) [英] Does MINLOC work for arrays beginning at index 0? (Fortran 90/95)

查看:59
本文介绍了MINLOC 是否适用于从索引 0 开始的数组?(Fortran 90/95)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 C 一段时间后,我回到 Fortran 并在我的代码中分配了从索引 0 到 N 的数组:

After using C for a while, I went back to Fortran and allocated the arrays in my code from index 0 to N:

real(kind=dp), dimension(:), allocatable :: a 
allocate(a(0:50))

我需要找到数组的最小绝对值的索引,所以我使用了 MINLOC,为了检查这个,我将它与 MINVAL 进行了比较:

I needed to find the index of the minimum absolute value of the array, so I used MINLOC, and to check this I compared it to MINVAL:

minloc(abs(a(:)))
minval(abs(a))

MINLOC的结果是索引42,而MINVAL的结果对应的是41.这是输出中的相关部分:

The result of MINLOC was index 42 but the result of MINVAL corresponded to 41. Here is the relevant section from the output:

Index i    a(i) 

39         0.04667    
40         0.02222    
41         0.00222           !This was clearly the minimum value
42         0.02667

MINLOC = 42
MINVAL = 0.00222

我认为这与 Fortran 内在没有正确处理索引为 0 的数组有关,因为以这种方式声明数组不是标准的 Fortran 风格(但它仍然被允许!).

I assume this is something to do with the Fortran intrinsic not handling arrays with index 0 correctly, since it is not standard Fortran style to declare arrays in this way (but it is still permitted!).

任何人都可以确认这一点或提供解决方法吗?

Can anyone confirm this or offer a workaround?

推荐答案

你的数组 a 确实从索引 0 开始,但你没有使用它.您搜索了最少的数组 abs(a(:)).这个匿名数组表达式从 1 开始,就像所有数组默认的一样.

Your array a indeed starts at index 0, but you did not use that. You searched for a minimum of array abs(a(:)). This anonymous array expression starts at 1 as all arrays do by default.

但即使您使用了 a,结果也会相同,并且与 Fortran 中数组参数传递的工作方式一致.

But even if you used a the result would be the same and is consistent with how array argument passing works in Fortran.

Fortran 标准明确指出:

The Fortran standard clearly states:

返回的 i 下标位于 1 到 ei 的范围内,其中 ei 是ARRAY 维度的范围.如果 ARRAY 的大小为零,则所有结果的元素为零.

The i subscript returned lies in the range 1 to ei , where ei is the extent of the idimension of ARRAY. If ARRAY has size zero, all elements of the result are zero.

如果您使用了假定的形状参数,则数组不会自动传递下限.例如如果你有自己的函数

Lower bounds are not automatically passed with the array if you used assumed shape arguments. For example if you have your own function

  function f(arg)
    real :: arg(:)

arg 无论实际参数在调用代码中的何处开始,始终从 1 开始.

arg starts always at 1 no matter where the actual argument started in the calling code.

您可以将其更改为从其他值开始

You can change it to start at some other value

  function f(arg)
    real :: arg(-42:)

它将从该值开始编入索引.

and it would be indexed starting from that value.

这篇关于MINLOC 是否适用于从索引 0 开始的数组?(Fortran 90/95)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆