1

我正在尝试编写一个具有标量输入但数组输出的函数。

我的例子是:

Ts(theta) = reshape((/ dcos(theta)**2.d0, dsin(theta)**2.d0, -dsin(2.d0*theta)/2.d0,  & 
    dsin(theta)**2.d0, dcos(theta)**2.d0, dsin(2.d0*theta)/2.d0, &
    dsin(2.d0*theta), -dsin(2.d0*theta), dcos(2.d0*theta) /), (/3,3/))

Ts(theta)读取为一维数组,但我希望输出为二维数组。

这可能吗?

4

1 回答 1

4

Of course

function Ts(theta)
  real(rp) :: Ts(3,3)
  real(rp),intent(in) :: theta

  Ts = reshape((/ cos(theta)**2, sin(theta)**2, -sin(2*theta)/2,  &
      sin(theta)**2, cos(theta)**2, sin(2*theta)/2, &
      sin(2*theta), -sin(2*theta), cos(2*theta) /), (/3,3/))
end function

where rp is the right kind constant for current real precision.

Remark: do not use specific functions as dsin and dcos, they are remnants of FORTRAN 66 and obsolete since FORTRAN 77.

Your syntax Ts(theta) = means you probably tried a statement function. They are obsolete also. I am not sure if they can be array-valued, you may try it.

于 2013-06-01T11:07:09.170 回答