有没有办法在where构造中进行函数调用?

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

我想做的事情如下:

Real a(10)
Real b(10)

! define a and b, then

where (a<b)
  a = f1(a,b)
elsewhere
  a = f2(a,b)
endwhere

function f1(a,b)
! somehow or another operates only where a < b
end function f1

function f2(a,b)
! somehow or another operates only where a>=b
end function f2

我想我可以做点什么

a = f1(a,b)
a = f2(a,b)

function f1(a,b)
where (a<b) ...
end function f1

function f2(a,b)
where (a>=b) ...
end function f2

但是我认为我试图解开的方式在某些方面会很好。有没有办法做到这一点?

在回应一些评论时,我想通过调用已定义的函数来操作a,这些函数具有太多代码行以在这里很好地适应。我不能讨论这些函数,但是比如说它们依赖于某种收敛循环:

function f1(a,b)

real a(10), b(10)
real f1(10)

real tmp(10), conv

real tol = 1.e-5
tmp = a
f1 = sin(b*a)
conv = max(abs(f1-tmp))

while (conv > tol)
  tmp = f1
  f1 = sin(b*tmp)
  conv = max(abs(f1-tmp))
endwhile

return

end function

并且有一些方法可以改善这一点,并且数学可能是这样的,它不会收敛等等,但这是一个得到我正在谈论的例子。问题是,我希望它只在where结构定义的域上运行。谢谢。

function fortran where subroutine
1个回答
2
投票

f1f2是非特定函数的情况下,这些函数基于参数数组的所有元素进行评估。

但是,对于元素函数f1f2,只处理匹配真实掩码的ab元素(元素)。

查看您的示例函数,元素函数将不合适,因此您将不得不使用替代方法。但是,如果你可以使用where构造来包装整个事物,那么你可能会对元素有好运。

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