victorlei / smop Goto Github PK
View Code? Open in Web Editor NEWSmall Matlab to Python compiler
License: MIT License
Small Matlab to Python compiler
License: MIT License
Since ply
is developed separately, importing it from site-packages
will benefit from any updates and upgrades it experiences.
function foo(x)
foo = 10
end
matlab:
function ret = noargin()
ret = 0
python:
# Autogenerated with SMOP version 0.25
# main.py test.m -o test.py
from __future__ import division
import numpy as np
from runtime import *
def noargin_(,nargout=1):
#3
ret=0
return ret
Matlab's operator = deep copies the matrix, while in numpy it's only the reference that is copied, even when affecting a sub part of the matrix/vector.
Fix:
a = b
should be:
a = np.array(b)
when b is a vector/matrix.
But it's not useful when affecting the result of an operation:
a = b * b
for example.
PS: adding spaces right and left of the = operator would make code more readable, as keeping comments and line feeds.
It would be nice to make smop
compatible with python3
, since ply
is.
=>> function x=foo(y); x=y; end
Traceback (most recent call last):
File "main.py", line 139, in
main()
File "main.py", line 93, in main
resolve.resolve(t,symtab)
File "/home/lei/smop-github/smop/resolve.py", line 67, in resolve
u = G.node[n]["ident"]
KeyError: 'ident'
lei@dilbert ~/smop-github/smop $
lenght function should be converted to len
currently:
arr{1} = 1;
arr{2} = 2;
lentgh(arr);
converted to
arr[1] = 1
arr[1] = 1
lenght(arr)
I accidentally uploaded a development version -- sorry I have no experience with git... Meanwhile download version 0.22 -- it is supposed to work . In particular, you should be able to run
python -m fastsolver.py
Without error messages.
what simple and obvious step did I miss?
Maybe even in the home page would be very helpful. Though i am sure it would be done pretty soon, but having some demo will generate more interest!
There are several other projects that translate matlab to python. The most relevant seems to be these two, also written in python and BSD / MIT licensed:
https://bitbucket.org/juricap/ompc/src
https://github.com/miaoever/Mat2py
I'm not certain which project is furthest along (I chose this one to bug since it was the most recent), but working on finishing one codebase to perfection rather than having three different but incomplete ones ought to be much more efficient...
Hi all,
I am new to Python and need to convert some MATLAB code to Python code, so I tried the "Working example: solver.m" on https://github.com/victorlei/smop/blob/master/README.rst as a start, but got the error message:
~/Downloads/smop-0.25.4/smop> python main.py solver.m
Traceback (most recent call last):
File "main.py", line 164, in
main()
File "main.py", line 157, in main
G = resolve.resolve(func_obj)
File "/nfs/yugong/data/jnwang/Downloads/smop-0.25.4/smop/resolve.py", line 65, in resolve
G = as_networkx(t)
File "/nfs/yugong/data/jnwang/Downloads/smop-0.25.4/smop/resolve.py", line 50, in as_networkx
G.add_node(uu, ident=u) # label=label)
TypeError: add_node() got an unexpected keyword argument 'ident'
I also tried the latest version smop-0.25.6, and ended up with the same error, anyone knows what is the problem? I am using openSUSE 13.1, python 2.7.6, and MATLAB R2012b.
Thanks a lot!
Comments annotate code as a docstring and at the few places where the code itself does not suffice for readability. For this reason it would be nice to provide an option for converting MATLAB
comments to python
comments. This was mentioned also in #12 and in this issue on chiselapp
.
Example:
function a = test(b)
[c,d] = butter(4, 'low')
a = filter(c,d, b)
a = round(a)
end
gets converted to:
def test(b):
c, d = butter(4, 'low') # nargout=2
a = filter_(c, d, b)
a = round_(a)
return a
This happens both in the master branch and @perimosocordiae's divergent branch.
Any pointers?
This Matlab function:
function a = test(m)
a = m.member(:, 0)
end
produces this python function:
def test(m):
a = m.member(:, 0)
return a
The obvious conclusion I can make is that smop is detecting any symbol with '.' in it as a function. Any idea what would need to be changed to fix this?
perhaps adding 'convert' to the project description would increase the probability of google suggesting it for a "convert matlab to python" search
Matlab operator * is the dot product when multiplying matrices or vectors
and is translated to python operator * which is the element-wise multiplication when
using matrices or vectors; while matlab operator .* which is the element-wise multiplication is translated to .dot()
The generated code needs spaces and other adjustments to become PEP8 conformant, hence more readable. Issue opened following comment in #12.
If you ever tried to understand the output from the parser, you will appreciate -d option. Take a look at solver.pdf for an example.
Running Red Hat Linux:
$ smop
Does not display the text in usage() of main.py.
For unknown reason, UNC path combined with asterisk, as in smop \foo\bar\bzz*.m is not understood and somehow prevents smop from being run. Other, similar, expressions work just fine. For example, smop "\foo\bar\bzz*.m" (note the quotes) is okay.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.