我的理解是你可以从Fortran中的函数返回一个数组,但由于某种原因,我的代码只返回我要求它返回的数组中的第一个值.这是功能:
function polynomialMult(npts,x,y)
integer npts
double precision x(npts), results(npts + 1), y(npts,npts)
polynomialMult = x(1:npts) + 1
end function
Run Code Online (Sandbox Code Playgroud)
这就是我所说的
C(1:numPoints) = polynomialMult(numPoints,x,f)
print *, C(1:numPoints)`
Run Code Online (Sandbox Code Playgroud)
现在它没有做任何有用的事情,因为我在编写逻辑之前试图理解语法.我看到了一些关于为函数指定类型的东西,但是当我写的时候
integer function polynomialMult(npts,x,y)
Run Code Online (Sandbox Code Playgroud)
或者无论我得到编译错误.
在Python中:
def select(x):
y = []
for e in x:
if e!=0:
y.append(e)
return y
Run Code Online (Sandbox Code Playgroud)
适用于:
x = [1,0,2,0,0,3] select(x) [1,2,3]
被翻译成Fortran:
function select(x,n) result(y)
implicit none
integer:: x(n),n,i,j,y(?)
j = 0
do i=1,n
if (x(i)/=0) then
j = j+1
y(j) = x(i)
endif
enddo
end function
Run Code Online (Sandbox Code Playgroud)
问题出在Fortran中:
对于1如果定义为y(n),则输出将为:
x = (/1,0,2,0,0,3/) print *,select(x,6) 1,2,3,0,0,0
这是不希望的!
!-------------------------------
评论:
1-所有给出的答案在本文中都很有用.特别是MSB和eryksun的.
2-我试图调整我的问题的想法,并编译,F2Py但它没有成功.我已经使用GFortran对它们进行了调试,所有这些都是成功的.它可能是一个错误F2Py或我不知道正确使用它的东西.我将尝试在另一篇文章中介绍此问题.
更新: 可以在此处找到链接的问题.