使用 f2py 时出现问题

发布于 2024-09-16 20:29:14 字数 1859 浏览 4 评论 0 原文

我有一些用 Fortran 编写的例程,我想在我的 python 代码中使用它们。快速的网络搜索告诉我有关 f2py 的信息,我尝试了一下。使用

 f2py -c numericalMethods.f -m numericalMethods

它似乎可以工作一段时间,直到在转换过程中产生很多错误。知道为什么以下代码无法与 f2py 一起使用吗?

  SUBROUTINE n_bezier(t, nx2, BezX, BezY)
  PARAMETER (N_SEG=130)
  PARAMETER (nmax=130)
  DOUBLE PRECISION t(nmax)
  DOUBLE PRECISION nx2(nmax)
  DOUBLE PRECISION BezX(N_SEG), BezY(N_SEG)
  DOUBLE PRECISION coeff(nmax)
  INTEGER i, j
  DOUBLE PRECISION r,fact


  do i = 1, N_SEG
    r = real(i) / real(N_SEG)
    BezX(i) = 0 
    BezY(i) = 0
    do j=1,nmax 
        coeff(j) = BICO(nmax,j) *
 .                 (1-r)**(nmax-j) * r**j
        print*, 'coeff for j= ', j, ' --> ', coeff(j) 
    if(coeff(j).ne.0) then
            BezX(i) = BezX(i) + coeff(j)*t(j)
            BezY(i) = BezY(i) + coeff(j)*nx2(j)
        endif
    enddo
  enddo  
  END

  function fact(n)
  INTEGER n, p
  DOUBLE PRECISION fact
     p = 1
     do i = 1, n
        p = p * i
     end do
     fact = p
  end

  FUNCTION BICO(N,K)
  BICO=ANINT(EXP(FACTLN(N)-FACTLN(K)-FACTLN(N-K)))
  RETURN
  END

  FUNCTION FACTLN(N)
  DIMENSION A(100)
  DATA A/100*-1./
  IF (N.LT.0) PAUSE 'negative factorial'
  IF (N.LE.99) THEN
    IF (A(N+1).LT.0.)  A(N+1)=GAMMLN(N+1.)
    FACTLN=A(N+1)
  ELSE
    FACTLN=GAMMLN(N+1.)
  ENDIF
  RETURN
  END


  FUNCTION GAMMLN(XX)
  REAL*8 COF(6),STP,HALF,ONE,FPF,X,TMP,SER
  DATA COF,STP/76.18009173D0,-86.50532033D0,24.01409822D0,
 .     -1.231739516D0,.120858003D-2,-.536382D-5,2.50662827465D0/
  DATA HALF,ONE,FPF/0.5D0,1.0D0,5.5D0/
  X=XX-ONE
  TMP=X+FPF
  TMP=(X+HALF)*LOG(TMP)-TMP
  SER=ONE
  DO 11 J=1,6
      X=X+ONE
      SER=SER+COF(J)/X
  11    CONTINUE
  GAMMLN=TMP+LOG(STP*SER)
  RETURN
  END

编辑:这里是包含错误的日志文件。

I have some routines written in fortran that I'd like to use in my python code. A quick websearch informed me about f2py, and I gave it a try. Using

 f2py -c numericalMethods.f -m numericalMethods

it seems to work for a while until a lot of errors are spawn during the conversion. Any idea of why the following bit of code fails to work with f2py?

  SUBROUTINE n_bezier(t, nx2, BezX, BezY)
  PARAMETER (N_SEG=130)
  PARAMETER (nmax=130)
  DOUBLE PRECISION t(nmax)
  DOUBLE PRECISION nx2(nmax)
  DOUBLE PRECISION BezX(N_SEG), BezY(N_SEG)
  DOUBLE PRECISION coeff(nmax)
  INTEGER i, j
  DOUBLE PRECISION r,fact


  do i = 1, N_SEG
    r = real(i) / real(N_SEG)
    BezX(i) = 0 
    BezY(i) = 0
    do j=1,nmax 
        coeff(j) = BICO(nmax,j) *
 .                 (1-r)**(nmax-j) * r**j
        print*, 'coeff for j= ', j, ' --> ', coeff(j) 
    if(coeff(j).ne.0) then
            BezX(i) = BezX(i) + coeff(j)*t(j)
            BezY(i) = BezY(i) + coeff(j)*nx2(j)
        endif
    enddo
  enddo  
  END

  function fact(n)
  INTEGER n, p
  DOUBLE PRECISION fact
     p = 1
     do i = 1, n
        p = p * i
     end do
     fact = p
  end

  FUNCTION BICO(N,K)
  BICO=ANINT(EXP(FACTLN(N)-FACTLN(K)-FACTLN(N-K)))
  RETURN
  END

  FUNCTION FACTLN(N)
  DIMENSION A(100)
  DATA A/100*-1./
  IF (N.LT.0) PAUSE 'negative factorial'
  IF (N.LE.99) THEN
    IF (A(N+1).LT.0.)  A(N+1)=GAMMLN(N+1.)
    FACTLN=A(N+1)
  ELSE
    FACTLN=GAMMLN(N+1.)
  ENDIF
  RETURN
  END


  FUNCTION GAMMLN(XX)
  REAL*8 COF(6),STP,HALF,ONE,FPF,X,TMP,SER
  DATA COF,STP/76.18009173D0,-86.50532033D0,24.01409822D0,
 .     -1.231739516D0,.120858003D-2,-.536382D-5,2.50662827465D0/
  DATA HALF,ONE,FPF/0.5D0,1.0D0,5.5D0/
  X=XX-ONE
  TMP=X+FPF
  TMP=(X+HALF)*LOG(TMP)-TMP
  SER=ONE
  DO 11 J=1,6
      X=X+ONE
      SER=SER+COF(J)/X
  11    CONTINUE
  GAMMLN=TMP+LOG(STP*SER)
  RETURN
  END

EDIT : Here is the log file containing the errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-09-23 20:29:14

将我最终解决问题的评论放入答案中:

遵循在网上找到的一些建议 https://cd34.com/blog/programming/python/mysql-python-and-snow-leopard -->评论37,我将10.4文件夹替换为10.5 SDK文件夹。 stdarg.h 错误不再存在。不是很干净和正统,但似乎有效。 .so 文件现已创建!感谢大家的帮助。

Putting my comment that finally solved the problem into an answer :

Following some advice found on the net https://cd34.com/blog/programming/python/mysql-python-and-snow-leopard --> comment 37, I replaced the 10.4 folder by the 10.5 SDK one. And the stdarg.h error is no more. Not very clean and orthodox, but it seems to work. The .so file is created now! Thanks for everyone's help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文