是否有相当于在Python中解压缩参数列表的Fortran?

问题描述 投票:0回答:1

我也在写我的第一个数值优化程序(牛顿方法)和我的第一个Fortran程序。我开始使用Python来了解第一个问题,现在我将移植到Fortran以便在另一个上工作。 (一次只有一件事,对吧?)

在Python中,有一种将参数传递给函数的简便方法:unpacking a list,如下所示:

def f(x_1, x_2):
    """Just a function with two arguments."""
    return math.sqrt(x_1 ** 2 + x_2 ** 2)

...
f(*[4, 3])  # calls f with parameters 4 and 3
# output: 5

Fortran像这个明星算子一样吗?我将二维点放入矩阵中,并且尝试学习将2D点“向量”之一传递给要评估的函数的最常规方法。看起来有点像这样:

! "double precision", using SELECTED_REAL_KIND
REAL(KIND=dp), DIMENSION(100, 2) :: iterates = 0.0_dp

! f expects two REALs
REAL(KIND=dp) :: first_value = f(iterates(1, :))
fortran fortran95 argument-unpacking
1个回答
0
投票

编号

您可以使函数接受向量。如果函数来自依赖项,则可以编写包装器:

function f_array_input(x)
  real(kind=dp), intent(in) :: x(2)
  real(kind=dp) :: f_array_input
  f_array_input = f(x(1), x(2))
end function

© www.soinside.com 2019 - 2024. All rights reserved.