问题描述
我在尝试使用 f2py(在 gfortran 中)在 Python 中制作一堆 Fortran 代码时遇到了一个问题。我的 Fortran 代码有一大堆子程序,它在 gfortran 和在线虚拟 Fortran IDE 中都可以很好地编译。 我遇到问题的子例程是一个合并排序子例程(我从 rosettastone 获取并修改了它),它看起来像这样:
subroutine MSort(N,A,A_out)
implicit none
integer,intent(in) :: N
real,dimension(N),intent(in) :: A
real,intent(out) :: A_out
real :: work((size(A) + 1) / 2)
A_out=A
call MergeSort(A_out,work)
contains
subroutine merge(A,B,C)
implicit none
real,target,intent(in) :: A(:),B(:)
real,intent(inout) :: C(:)
integer :: i,j,k
if (size(A) + size(B) > size(C)) then
stop
end if
i = 1; j = 1
do k = 1,size(C)
if (i <= size(A) .and. j <= size(B)) then
if (A(i) <= B(j)) then
C(k) = A(i)
i = i + 1
else
C(k) = B(j)
j = j + 1
end if
else if (i <= size(A)) then
C(k) = A(i)
i = i + 1
else if (j <= size(B)) then
C(k) = B(j)
j = j + 1
end if
end do
end subroutine merge
subroutine swap(x,y)
implicit none
real,intent(inout) :: x,y
real :: tmp
tmp = x; x = y; y = tmp
end subroutine
recursive subroutine MergeSort(A,work)
implicit none
real,intent(inout) :: A(:)
real,intent(inout) :: work(:)
integer :: half
half = (size(A) + 1) / 2
if (size(A) < 2) then
continue
else if (size(A) == 2) then
if (A(1) > A(2)) then
call swap(A(1),A(2))
end if
else
call MergeSort(A( : half),work)
call MergeSort(A(half + 1 :),work)
if (A(half) > A(half + 1)) then
work(1 : half) = A(1 : half)
call merge(work(1 : half),A(half + 1:),A)
endif
end if
end subroutine MergeSort
end subroutine MSort
我编译它
$ f2py -c -m fprogram fprogram.f90
并在我的 python 代码的开头(在 jupyter 笔记本中)插入 import fprogram
,我想像这样使用它(我知道原始是一个列表,它不是尺寸问题):
size=len(original_list)
sorted_list=fprogram.MSort(size,original_list)
我收到错误信息
module 'fprogram' has no attribute 'MSort'
与此同时,当我以同样的方式使用 fprogram 中的任何其他子程序时,它完美地工作。
我已经将 fortran 代码修改为没有带有 intent(inout)
的变量,因为在以前的情况下这已经解决了我的问题,但这次没有用。我该怎么做才能让 python 识别这个子程序?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)