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

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

问题描述

我在FORTRAN中有一个数值库(我相信FORTRAN IV),我想将它转换为Python代码。我想要真正的源代码,我可以导入任何Python虚拟机--- Windows,MacOS-X,Linux,Android。我开始这样做,但是库中有大约1000个例程,所以这不是一个合理的解决方案。

解决方案

edit :在numpy上添加了信息



这种工具存在于 Fortran to Lisp ,或者 Fortran to C ,甚至< Fortran to Java 。但是,你永远不会有Fortran to Python工具,原因很简单:与Fortran,Lisp或C不同, Python没有GOTO [1]。 Fortran中有许多GOTO(特别是Fortran IV)代码。即使 Jacopini 有一个定理说明你可以用结构化编程来模拟GOTO,但实现一个真正的(和高效)的语言转换工具。



因此,您不仅需要翻译1000个例程的代码,还需要 >每个算法都带有所有的叠加goto,并在将算法写入Python之前将算法转换为结构化程序。祝你好运!



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



对于C语言(不是您的语言,而是f2c 可能是一个选项),有 SWIG ,Fortran有 f2py ,现已与 numpy 。 SWIG有一些支持Android



顺便说一句,与其转换为纯粹的Python,您可以使用numpy:numpy的功能与Fortran 90相似(请参阅比较此处),因此您可能会首先考虑将您的程序转换为F90以实现更平滑的过渡。 Adnroid似乎也有 Numpy 。如果你需要在64位Windows上使用numpy,那么有这里有二进制文件。



如果您决定使用包装,gfortran可以在Linux上运行(只需从分发包安装),Windows( MinGW )和 Android的
如果你沿着这条线,不要忘记你编译FIV代码,所以有通常的一次循环问题(通常编译器选项是好的)。您可能还需要手动转换一些旧的,非标准的语句,这些语句在现代编译器中是没有的。



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

[1]您可能会问:但是如果GOTO是问题,那么Fortran到Java的工具怎么样?那么,它使用JVM的技巧,它内部有 GOTO说明。在Python字节码中也有一个GOTO(查找JUMP 这里),所以可能有在这里进行调查。所以我之前的说法是错误的:这里可能是一个Fortran to Python工具,使用像Java这样的字节码技巧。但它仍然需要开发,并且好的库(如numpy,matplotlib,pandas等)的可用性使得它不必要,至少可以说。


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.

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

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