matlab制作脚本工具,把简单的matlab/Octave脚本转换成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.m

fastsolver.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

dealWall1

lei@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.05

06 lots=2; return

07 else

08 lenw=length(w0);

09 [xx,nseq]=sort(rand(1,lenw));

10 A1=A;

11 B1=B;

12 w01=w0;

13 for i=1:lenw

14 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.

Implemented features

Lexical and syntactical analysis

Mostly complete, including some weird Matlab features

Name resolution

For each occurrence of a variable, find a set of its possible definitions

Inlining of small functions

Array subscripts translated from 1-based (Matlab and Fortran style) to 0-based (C and Python style)

Also, end subscript implemented

from:step:to translated to from:to:step

Upper bound is n+1

Unimplemented features

Structs

To be implemented as soon as cc possible.

Arrays silently become C=style (rows first).

In some cases it may break the code. Not detected.

Function handles and lambda expressions

Handles break the heuristic that tells between function calls and array references.

Graphics,

Never

Auto-expanding arrays

Unlike other languages, matlab allows out-of-bounds assignment. As MathWorks tries to phase out this feature, there is a lot of legacy code depending on it.

Sparse matrices

Have good chances of being implemented, especially taking into account that scipy have several implementations to choose from.

Full support for boolean indexing. Currently, some expressions don't work

For example, x(x>0.5) = 1 works, but y=x>0.5; x(y)=1 does not work.

Command syntax

Too complex to support

Type, rank and shape inference

Strings

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 Python

pymex: Embeds the Python Interpreter in Matlab, also on File Exchange

matpy: 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:

On a different note, though I'm not a fortran fan at all, for people who might find it useful there is:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值