是否MINLOC工作在指数从0开始的数组? (Fortran语言90/95) [英] Does MINLOC work for arrays beginning at index 0? (Fortran 90/95)

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

问题描述

使用C一段时间后,我又回到了Fortran和分配在我的code数组索引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(:))。这个匿名数组前pression从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:

我在返回标在于范围为1〜EI,其中EI是
  ARRAY的idimension程度。如果数组的大小为0,全
  结果的元素是零。

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无论身在何处实际参数开始在调用code。

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天全站免登陆