如何轻松地将 FORTRAN 代码转换为 Python 代码(真实代码,而不是包装器) [英] How can I easily convert FORTRAN code to Python code (real code, not wrappers)

查看:105
本文介绍了如何轻松地将 FORTRAN 代码转换为 Python 代码(真实代码,而不是包装器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 FORTRAN 中有一个数值库(我相信 FORTRAN IV),我想将它转换为 Python 代码.我想要可以在任何 Python 虚拟机上导入的真实源代码——Windows、MacOS-X、Linux、Android.我开始手动执行此操作,但库中大约有 1,000 个例程,因此这不是一个合理的解决方案.

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.

推荐答案

这样的工具存在于 Fortran到 Lisp,或 Fortran 到 C,甚至是 Fortran to Java.但是您永远不会拥有 Fortran 到 Python 工具,原因很简单:与 Fortran、Lisp 或 C 不同,Python 没有 GOTO [1].Fortran(尤其是 Fortran IV)代码中有很多 GOTO.即使 Jacopini 有一个定理表明您可以使用结构化编程来模拟 GOTO,但这还很遥远实现一个真正的(和高效的)语言转换工具太麻烦了.

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.

因此,您不仅需要翻译 1000 个例程的代码,还需要理解每个算法及其所有重叠的 goto,并在编写之前将算法翻译成结构化程序它在 Python 中.祝你好运!

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!

嘿,为什么你认为包装不好?Windows、OSX 和 Linux 都有 Fortran 和 C [2] 编译器和良好的包装器!

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

对于 C(这里不是您的语言,但 f2c 可能是一个选项),有SWIG,Fortran 有 f2py,现在与 numpy.SWIG 有一些 对 Android 的支持.

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.

顺便说一句,与其转换为纯"Python,您可以使用 NumPy:NumPy 的功能类似于 Fortran 90(请参阅此处的比较),因此您可以考虑先将您的程序翻译成 F90 以实现更平滑的过渡.似乎还有一个 Numpy on Adnroid.如果您在 64 位 Windows 上需要 NumPy,这里有二进制文件 这里.

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.

如果您决定使用包装器,gfortran 可以在 Linux(只需从分发包安装)、Windows(MinGW) 和 Android.如果你沿着这条线走,别忘了你编译 FIV 代码,所以有通常的单程循环".问题(通常编译器选项很好).您可能还必须手动转换一些现代编译器中没有的旧的、非标准的语句.

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.

显然,您还可以选择将项目语言切换为 Lisp 或 Java...

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

[1] 你可能会问:如果 GOTO 是问题所在,那怎么会有 Fortran to Java 工具呢?好吧,它使用 JVM 的技巧,它在内部具有 GOTO 指令.Python 字节码中还有一个 GOTO(查找 JUMP here),所以这里可能有一些东西需要调查.所以我之前的陈述是错误的:可能是一个 Fortran 到 Python 的工具,使用 Java 中的字节码技巧.但它仍有待开发,至少可以说,良好的库(如 NumPy、matplotlib、pandas...)的可用性使得它变得不必要.

[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 the availability of good libraries (like NumPy, matplotlib, pandas...) makes it unnecessary, to say the least.

这篇关于如何轻松地将 FORTRAN 代码转换为 Python 代码(真实代码,而不是包装器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆