把简单的matlab/Octave脚本转换成python脚本的工具
来源:互联网 发布:java微信分享接口开发 编辑:程序博客网 时间:2024/06/06 13:23
http://stackoverflow.com/questions/9845292/a-tool-to-convert-matlab-code-to-python
这对于现有源代码的改写有帮助;
但是,从编程的角度,应该还是原生态的好.
OMPC好用,但是Python 2.5支持最好;
SMOP一直在更新中(github),这个应该是目前最佳; Small Matlab Octave Python首字母缩写;
不能指望借此就完全用Python替代Octave和Matlab,这也不是作者的初衷; 何况软件这么小,也很难完成那么复杂的工作.
这是一个跨平台的命令行下使用的工具,但仅在Linux上测试过;
SMOP
SMOP stands for Small Matlab/Octave to Python compiler. It is used to convert Matlab programs to Python.
SMOP is not a polished product, nor a replacement to Octave and Matlab. Taking into account its size (less than 3000 lines), this is not surprizing. There are no toolboxes. Small everyday functions (max, length, etc.) are recognized and supported, but that's all.
SMOP is written in Python, using PLY -- Python Lex/Yacc for lexical analysis and parsing, and numpy for runtime environment. SMOP is platform-independent, but is tested only on Linux. It is a command-line utility.
Example
It is possible to run an example without installing smop. Just unzip it somewhere, and cd there. In your current directory you will find a bunch of .py files and a file named fastsolver.m. It is taken from the winning submission to Matlab programming competition in 2004 (Moving Furniturehttp://www.mathworks.cn/matlabcentral/contest/contests/12/submissions/29989).
Now type python main.py fastsolver.m -o fastsolver.py. If you don't specify the output file with -o option, it is written to a.py. Each time a function is translated, its name is written.
lei@fuji:~/smop/smop$ python main.py fastsolver.mfastsolver.m solver cbest mainsolver imoves easysolver localfiddler findoverlaps dijkstra improve TLL79 solverA solver1 movefrompos onemove solver2 SearchPath Faster10IntReps2 matrixsolver outoftheway ismember1 ismember2 setdiff unique sub2ind randperm perms itTakesAThief movefurniture findshortestpath dealWall1lei@fuji:~/smop/smop$
The entire submission contains 2093 lines, and it is automatically translated to Python by smop. These are the good news. The bad news are that generating the code is not enough to run the program, so there are no performance numbers yet.
- While the submission itself --- the solver program --- does not use graphics, the envelope code that is responsible to run the submission, collect and display the results, does. So about 100 lines of the envelope must be rewritten by hand.
- Many standard functions are not yet implemented --- rand, find, and others. They are on the issues list.
- Some matlab constructs, especially creating arrays by out of bound assignment, are used in the submission, but not yet supported by smop. Meanwhile, these lines should be rewritten in the Matlab code.
01 function moves=solver(A,B,w0)02 [moves,optmove,optscore]=cbest(A,B,w0);03 curscore=sum(w0(moves(:,1)));04 lots=1;05 if length(moves)-optmove<20||curscore/optscore<1.0506 lots=2; return07 else08 lenw=length(w0);09 [xx,nseq]=sort(rand(1,lenw));10 A1=A;11 B1=B;12 w01=w0;13 for i=1:lenw14 A1(A==i)=nseq(i);15 B1(B==i)=nseq(i);16 w01(nseq(i))=w0(i);17 end;18 [moves2,optmove,optscore]=cbest(A1,B1,w01);
becomes
TBD
The table below tries to summarize various features.
There are several alternative tools for converting Matlab code to Python code (not tested yet):
- Small Matlab to Python compiler: convert Matlab code to Python code, also developed here:SMOP@chiselapp
- LiberMate: translate from Matlab to Python and SciPy
- OMPC: Matlab to Python (a bit outdated)
- Matlab to Python conversion: No download files available
Also, for those interested in an interface between the two languages and not conversion:
pymatlab
: communicate from Python by sending data to the MATLAB workspace, operating on them with scripts and pulling back the resulting data- Python-Matlab wormholes: both directions of interaction supported
- Python-Matlab bridge: use Matlab from within Python, offers matlab_magic for iPython, to execute normal matlab code from within ipython
- PyMat: Control Matlab session from Python
pymat2
: continuation of the appearingly abandoned PyMat.mlabwrap
, mlabwrap-purepy: make Matlab look like Python library (based on PyMat)oct2py
: run GNU Octave commands from within Pythonpymex
: Embeds the Python Interpreter in Matlab, also on File Exchangematpy
: Access MATLAB in various ways: create variables, access .mat files, direct interface to MATLAB engine (requires MATLAB be installed).- MatPy: Python package for numerical linear algebra and plotting with a MatLab-like interface
Btw might be helpful to look here for other migration tips:
- http://bci2000.org/downloads/BCPy2000/Migration.html
On a different note, though I'm not a fortran
fan at all, for people who might find it useful there is:
matlab2fortran
- 把简单的matlab/Octave脚本转换成python脚本的工具
- 替代matlab的工具octave
- 把txt文件转换为tsv文件的python脚本
- Python把对应格式的csv文件转换成字典类型存储脚本
- 简单的Python脚本实践
- caffe的简单python脚本
- matlab调用Python的.py脚本文件
- 用 python 脚本,把当前目录及子目录下的 wav 音频文件转换为 flac 格式
- arcgis中脚本工具调用公用的python脚本
- 打包python脚本为可执行文件的工具
- [Python]HTML转换为TXT的脚本
- [Python]HTML转换为TXT的脚本
- Octave:替代Matlab的免费数值计算工具
- Python脚本转换成windows的exe可执行程序
- 一个简单的脚本程序----Python
- 一个简单的python读写文件脚本
- 简单验证码识别的python脚本
- Python:简单的文件备份脚本
- 新的征程
- Android从无知到有知——NO.1
- 【Java项目实战】——DRP之Oracle_11g数据库安装
- locate find whereis which type
- 操作系统文件系统综合设计
- 把简单的matlab/Octave脚本转换成python脚本的工具
- WebView示例
- ubuntu下shell命令没有了的解决方法
- js单列和继承的一些写法
- Longest Palindromic Substring
- python每次处理一个字符(读书笔记)
- 【HDU】3342 Legal or Not 拓扑排序
- day119V20140710
- ssl协议是什么