python代码转成fortran_我如何轻松地将FORTRAN代码转换为Python代码(真正的代码,而不是包装器)...

I have a numerical library in FORTRAN (I believe FORTRAN IV) and I want to convert it to Python code. I want real source code that I can import on any Python virtual machine --- Windows, MacOS-X, Linux, Android. I started to do this by hand, but there are about 1,000 routines in the library, so that's not a reasonable solution.

解决方案

edit: added information on numpy

Such a tool exists for Fortran to Lisp, or Fortran to C, or even Fortran to Java. But you will never have a Fortran to Python tool, for a simple reason: unlike Fortran, Lisp or C, Python does not have GOTO [1]. And there are many GOTOs in Fortran (especially Fortran IV) code. Even if there is a theorem by Jacopini stating that you can emulate GOTO with structured programming, it's far too cumbersome to implement a real (and efficient) language conversion tool.

So not only will you need to translate the code of 1000 routines, but you will also need to understand each algorithm, with all its imbricated gotos, and translate the algorithm into a structured program before writing it in Python. Good luck!

Hey, why do you think a wrapper is bad? Windows, OSX and Linux all have Fortran and C [2] compilers and good wrappers!

For C (not your language here, but f2c may be an option), there is SWIG, and Fortran has f2py, now integrated with numpy. SWIG has some support for Android.

By the way, instead of converting to "pure" Python, you can use numpy: numpy capabilities are similar to Fortran 90 (see a comparison here), so you may consider first translating your programs to F90 for a smoother transition. There seems to be also a Numpy on Adnroid. And in case you need numpy on 64-bit Windows, there are binaries here.

If you decide to use wrappers, gfortran runs on Linux (simply install from distribution packages), Windows (MinGW), and Android.

If you go along that line, don't forget you compile FIV code, so there is the usual "one-trip loop" problem (usually a compiler option is fine). You will probably have also to manually convert some old, non standard statements, not found in modern compilers.

You have also, obviously, the option to switch your project language to Lisp or Java...

[1] You may ask: but if GOTO is the problem, how come there is a Fortran to Java tool? Well, it uses tricks with the JVM, which has internally the GOTO instruction. There is also a GOTO in Python bytecode (look for JUMP here), so there may be something to investigate here. So my previous statement is wrong: there may be a Fortran to Python tool, using bytecode tricks like in Java. But it remains to develop, and availability of good libraries (like numpy, matplotlib, pandas...) makes it unnecessary, to say the least.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PythonFortran的混合编程可以通过使用F2PY(Fortran to Python)工具来实现。F2PY是一个用于将Fortran代码转换Python可调用模块的工具,它可以将Fortran代码编译成Python模块,使得Python代码可以调用Fortran代码。 以下是Python封装Fortran的步骤: 1. 编写Fortran代码,并创建一个Fortran模块文件。例如,假设我们有一个Fortran程序文件`test.f90`,其中包含一个名为`add`的子程序,它可以计算两个整数的和。我们可以使用以下命令编译Fortran代码,生成一个名为`test_module.so`的共享库文件: ``` f2py -c -m test_module test.f90 ``` 2. 在Python中导入创建的Fortran模块。可以使用以下代码: ```python import numpy as np from test_module import add a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = add(a, b) print(c) ``` 在上述代码中,我们首先导入了Numpy库和我们创建的Fortran模块`test_module`中的`add`子程序。然后,我们使用Numpy创建了两个数组`a`和`b`,并将它们作为参数传递给`add`子程序。最后,我们打印输出结果。 注意,在使用F2PY封装Fortran代码时,需要将Fortran代码中的变量类型和Python中的变量类型进行匹配。同时,还需要考虑Fortran代码中的数组是按列存储还是按行存储的,以及是否使用了Fortran特有的数组下标从1开始的特性。 除了使用F2PY进行Python封装Fortran外,还可以使用Cython、SWIG等工具进行混合编程。这些工具都可以将Fortran代码编译成Python可调用模块,使得Python代码可以调用Fortran代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值