如何将标量传递给 Fortran 子程序的向量(一维数组)? [英] How can a scalar be passed to a vector (1D array) to a Fortran subroutine?

查看:87
本文介绍了如何将标量传递给 Fortran 子程序的向量(一维数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这个程序:

INTEGER i,k
REAL*8  mp(15,48)
REAL*8  sp(15)
k=0
do i=1,12
   k=k+1
   call Equaltensors(sp,mp(1,k),15)
enddo
end

c=====================

subroutine Equaltensors(tensA,tensB,n)
REAL*8 tensA(n),tensB(n)
INTEGER i
do   i=1,n
     tensB(i)=tensA(i)
enddo
return
end

所以基本上 mp(1,1) 等的值作为向量 tensB(15) 传递给子程序,其中 n=15.我不明白的是如何将实数存储在子程序中的一维数组中.

So basically the value of mp(1,1) and so on is passed to the subroutine as a vector tensB(15) with n=15. What I don't understand is how a real number can be stored in a one-dimension array in a subroutine.

推荐答案

你的问题标题有点误导.Fortran 不允许您将标量传递给数组.但是它允许将数组的单个元素传递给例程的数组虚拟参数 - 这在 Fortran 中称为序列关联".正如 IanH 和其他人所说,以下元素会自动与虚拟数组的元素相关联,直到被调用例程的实际数组中的最后一个元素.

The title of your question is a bit misleading. Fortran doesn't allow you to pass a scalar to an array. But what it DOES allow is passing a single element of an array to a routine's array dummy argument - this is called "sequence association" in Fortran. As IanH and others have said, the following elements are automatically associated with the elements of the dummy array, up to the last element in the called routine's actual array.

不过,此功能有一些限制.如果元素是 POINTER 数组,则不能这样做.

There are some restrictions on this feature, though. If the element is of a POINTER array,you can't do this.

回到你的标题,我见过很多程序将常量 3 传递给一个例程,其中哑元是一个数组.该例程仅使用第一个元素,但这是不合法的,较新的编译器可能会检测到错误并抱怨.一种解决方法是使用数组构造函数将参数转换为数组 - 例如,CALL FOO ([3]),但这仅适用于读取而不是写入值的情况.

Going back to your title, I have seen many programs pass, say, the constant 3 to a routine where the dummy is an array. The routine only uses the first element, but this is not legal and newer compilers may detect the error and complain. One workaround for this is to turn the argument into an array by using an array constructor - for example, CALL FOO ([3]), but this works only if the value is to be read, not written.

我已经写了一些关于这个一般问题的博客文章 - 参见 http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argumenthttp://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2

I've written some blog posts on this general issue - see http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument and http://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2

这篇关于如何将标量传递给 Fortran 子程序的向量(一维数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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