Matlab to Python conversion

There seems to be an natural progression that occurs for users of data anaylsis programs. I believe the forces guiding those changes are not coincidental, but out of necessity based on the ease of learning, functionality, extensibility, scalability and cost. In my opinion, the best method for theory is still handwritten, but when more advanced concepts are learned, more powerful tools make those concepts easier to understand.

Anectdotally, everyone seems to start with a spreadsheet application for data analysis and plotting, which, at least 3 or 4 years ago would have been a Microsoft product. Excel works well for simple plotting, small datasets, and the software is ubiqutous but not free.

Some users may have had the curosity to investigate the macro tool and discovered programming by way of Visual Basic for Applications (VBA) in Excel. This gives the user a bit more control over data creation and processing with loops.

Once at a university, many students, especially engineering students, will discover MATLAB, and realized that VBA is very tedious compared to MATLAB syntax. The student will spend hundreds of hours learning programming fundamentals with this great new tool, and create many programs with it.

The crux of the programmers life so far will be when that MATLAB license expires and, not resorting to any illegal software copyright infringement, will be left out in the cold world of computing for data anaylsis and engineering.

Until, one day, when that recent graduate discovers that the road does not end with MATLAB, but is just the beginning. The realization that real programming languages exist and are used every day for data anaylsis that do not cost a dime and work very well, but may have a bit of a learning curve.

That is where we are now, how do I get all my old excel, matlab and vba projects into python? The first step is to install python. The really important library is scipy, which has nearly all the libraries one would need to perform basic scientific work.

Note this work was done on a Windows 7 machine with Anaconda 2.7 installed

Option 1 - Manual Conversion

There are a few options for converting code. The most reliable is probably by hand. To covert by hand requires a through understanding of python and matlab. I am assuming matlab syntax is well understood, but python less so. scipy provides a python for matlab users which describes to similarities and differences well, but the devil is in the details. Many other comparisions exist for python and matlab, like here on pyzo's website

Option 2 - Reuse old code with Octave

oct2py , source code

This is a neat module that is based on octave, which is an open-source matlab clone. It allows a simple translation of matlab/octave syntax to python directly.

In [1]:
%matplotlib inline
from oct2py import octave
from oct2py import Oct2Py
import numpy as np

Instantiate the Oct2Py object as oc

In [3]:
oc = Oct2Py()

Matrix creation

First, lets remind ourselves of matlab sytnax of matrix/array creation (which I happen to prefer over python, but hey, beggers can't be choosers')

In [4]:
y = oc.eval("[1 2 3];")
y
Out[4]:
array([[ 1.,  2.,  3.]])

Not bad. For me, the difference in slicing arrays and matrices is a tough transition, but this makes it much easier than learning the fundamentals

In [5]:
y = oc.eval("[1 2 3;4 5 6];")
y
Out[5]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

We can see it creates a matlab matrix and converts it exactly how we would need it in numpy

In [6]:
type(y)
Out[6]:
numpy.ndarray

Lets try something a bit more complex with analaogous syntax. Here is a matrix define natively in python

In [7]:
numpyarr = np.array([[1, 2,3,4], [5,6,7,8],[9,10,11,12],[13,14,15,16]], dtype=float)
numpyarr
Out[7]:
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.]])

and here is the same matrix defined in matlab/octave syntax

In [8]:
matlabarr = oc.eval("[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];") 
matlabarr
Out[8]:
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.]])

clearly, they are the same!

In [9]:
matlabarr == numpyarr
Out[9]:
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

Plotting with oct2py

Just using oct2py, we can plot using gnuplot, I haven't been able to find a way to plot them inline, but they still look good in the gnuplot window

In [14]:
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
y = np.sin(x)
oc.plot(x,y,'-o', linewidth=2)

We can see that is uses wxpython, which is another gui package. I am not 100% sure when that package was installed, but I am guess it was installed along with Octave

More matlab execution

Although this doesn't convert the code to python, it executes it very well. Here is a MATLAB code snippet for LU Decomposition. We can create a python string with the contents of this and evaluate it as octave code

% function [L,U] = LUCrout(A)
% Decomposes matrix A into a Lower matrix L and Upper matrix prednisone U
% A = LU
A = [1,5,3,5;  2,5,6,7; 9,0,3,4; 9,4,7,6]
A
[R,C] = size(A);
for i = 1:R
    L(i,1) = A(i,1);
    U(i,i) = 1;
end
for j = 2:R
    U(1,j) = A(1,j)/L(1,1);
end
for i = 2:R
    for j = 2:i
        L(i,j) = A(i,j) - L(i,1:j-1)*U(1:j-1,j);
    end
    for j = i+1:R
        U(i,j) = (A(i,j) - L(i,1:i-1)*U(1:i-1,j))/L(i,i);
    end
end
L
U
In [15]:
x = '''
% function [L,U] = LUCrout(A)
% Decomposes matrix A into a Lower matrix L and Upper matrix U
% A = LU
A = [1,5,3,5;  2,5,6,7; 9,0,3,4; 9,4,7,6]
A
[R,C] = size(A);
for i = 1:R
    L(i,1) = A(i,1);
    U(i,i) = 1;
end
for j = 2:R
    U(1,j) = A(1,j)/L(1,1);
end
for i = 2:R
    for j = 2:i
        L(i,j) = A(i,j) - L(i,1:j-1)*U(1:j-1,j);
    end
    for j = i+1:R
        U(i,j) = (A(i,j) - L(i,1:i-1)*U(1:i-1,j))/L(i,i);
    end
end
L
U
'''
In [16]:
oc.eval(x)
A =

        1        5        3        5
        2        5        6        7
        9        0        3        4
        9        4        7        6

A =

        1        5        3        5
        2        5        6        7
        9        0        3        4
        9        4        7        6

L =

  1.0e+001 *

  0.10000  0.00000  0.00000  0.00000
  0.20000  -0.50000  0.00000  0.00000
  0.90000  -4.50000  -2.40000  0.00000
  0.90000  -4.10000  -2.00000  -0.27333

U =

  1.00000  5.00000  3.00000  5.00000
  0.00000  1.00000  -0.00000  0.60000
  0.00000  0.00000  1.00000  0.58333
  0.00000  0.00000  0.00000  1.00000

If you are using an ipython notebook, we can take advantage of the magic functions and execute Octave/MATLAB code directly in the cells. To activate this functionality, we need to install Octave first, then install oct2py.

In [17]:
%load_ext oct2py.ipython

For single line octave code, we can use this syntax

In [18]:
x = %octave [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
x

Out[18]:
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.]])

This is very similar to the example earlier, except that is it running the code rather than evaluating a string. We can see that it creates a numpy array just like before

In [19]:
type(x)
Out[19]:
numpy.ndarray

If we want multi-line MATLAB/Octave sytax, we can use this syntax

In [20]:
%%octave
x = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
x
typeinfo(x)
x =

        1        2        3        4
        5        6        7        8
        9       10       11       12
       13       14       15       16

ans = matrix

in that cell, x is still a matlab matrix, but when we show 'x' in the next cell, it is now a numpy array

In [21]:
x
Out[21]:
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.]])

Lets try our LU function by loading the file, and replacing adding the octave magic function. execute

%load LU.m

and then add

%%octave

to the very top and delete

#%load LU.m
In [22]:
%%octave
% function [L,U] = LUCrout(A)
% Decomposes matrix A into a Lower matrix L and Upper matrix U
% A = LU
A = [1,5,3,5;  2,5,6,7; 9,0,3,4; 9,4,7,6]
A
[R,C] = size(A);
for i = 1:R
    L(i,1) = A(i,1);
    U(i,i) = 1;
end
for j = 2:R
    U(1,j) = A(1,j)/L(1,1);
end
for i = 2:R
    for j = 2:i
        L(i,j) = A(i,j) - L(i,1:j-1)*U(1:j-1,j);
    end
    for j = i+1:R
        U(i,j) = (A(i,j) - L(i,1:i-1)*U(1:i-1,j))/L(i,i);
    end
end
L
U
A =

        1        5        3        5
        2        5        6        7
        9        0        3        4
        9        4        7        6

A =

        1        5        3        5
        2        5        6        7
        9        0        3        4
        9        4        7        6

L =

  1.0e+001 *

  0.10000  0.00000  0.00000  0.00000
  0.20000  -0.50000  0.00000  0.00000
  0.90000  -4.50000  -2.40000  0.00000
  0.90000  -4.10000  -2.00000  -0.27333

U =

  1.00000  5.00000  3.00000  5.00000
  0.00000  1.00000  -0.00000  0.60000
  0.00000  0.00000  1.00000  0.58333
  0.00000  0.00000  0.00000  1.00000

Excellent. This appraoch keeps the code more native than having the copy and paste the code and create a string to be evalauted. And just for fun we can plot inline also!!!

In [23]:
%%octave
p = [12 -2.5 -8 -0.1 8];
x = 0:0.01:1;

polyout(p, 'x')
plot(x, polyval(p, x));
12*x^4 - 2.5*x^3 - 8*x^2 - 0.1*x^1 + 8
gnuplot Produced by GNUPLOT 4.4 patchlevel 4 6 6.5 7 7.5 8 8.5 9 9.5 0 0.2 0.4 0.6 0.8 1 Plot_1 <g style="fill:none; color:red; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> <path d="M30.8,82.6 L33.0,82.7 L35.2,82.8 L37.4,83.1 L39.6,83.4 L41.9,83.8 L44.1,84.2 L46.3,84.8 L48.5,85.4 L50.7,86.1 L52.9,86.9 L55.1,87.8 L57.3,88.7 L59.5,89.7 L61.8,90.8 L64.0,91.9 L66.2,93.2 L68.4,94.5 L70.6,95.8 L72.8,97.3 L75.0,98.8 L77.2,100.3 L79.4,101.9 L81.7,103.6 L83.9,105.4 L86.1,107.1 L88.3,109.0 L90.5,110.9 L92.7,112.8 L94.9,114.8 L97.1,116.8 L99.3,118.8 L101.6,120.9 L103.8,123.0 L106.0,125.2 L108.2,127.3 L110.4,129.5 L112.6,131.7 L114.8,133.9 L117.0,136.1 L119.2,138.3 L121.5,140.5 L123.7,142.7 L125.9,144.8 L128.1,147.0 L130.3,149.1 L132.5,151.2 L134.7,153.3 L136.9,155.3 L139.1,157.3 L141.4,159.2 L143.6,161.0 L145.8,162.8 L148.0,164.6 L150.2,166.2 L152.4,167.8 L154.6,169.2 L156.8,170.6 L159.0,171.9 L161.2,173.0 L163.5,174.0 L165.7,174.9 L167.9,175.7 L170.1,176.3 L172.3,176.8 L174.5,177.1 L176.7,177.2 L178.9,177.2 L181.1,177.0 L183.4,176.6 L185.6,176.0 L187.8,175.2 L190.0,174.2 L192.2,172.9 L194.4,171.4 L196.6,169.7 L198.8,167.7 L201.0,165.4 L203.3,162.9 L205.5,160.0 L207.7,156.9 L209.9,153.5 L212.1,149.8 L214.3,145.7 L216.5,141.3 L218.7,136.6 L220.9,131.5 L223.2,126.0 L225.4,120.1 L227.6,113.9 L229.8,107.2 L232.0,100.2 L234.2,92.7 L236.4,84.8 L238.6,76.4 L240.8,67.6 L243.1,58.3 L245.3,48.5 L247.5,38.2 L249.7,27.4 L251.9,16.1 " stroke="rgb( 0, 0, 255)"></path> </g> </g> <g style="fill:none; color:black; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> </g> </svg> </div> </div> </div> </div> </div> <div class="cell border-box-sizing code_cell rendered"> <div class="input"> <div class="prompt input_prompt">In [24]:</div> <div class="inner_cell"> <div class="input_area"> <div class=" highlight hl-ipython3"><pre><span></span><span class="c">%%octave</span> <span class="n">x</span> <span class="p">=</span> <span class="mi">1</span><span class="p">:</span><span class="mi">10</span><span class="p">;</span> <span class="n">y</span> <span class="p">=</span> <span class="nb">exp</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="nb">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="nb">xlabel</span><span class="p">(</span><span class="s">'x'</span><span class="p">)</span> <span class="nb">ylabel</span><span class="p">(</span><span class="s">'y'</span><span class="p">)</span> <span class="nb">title</span><span class="p">(</span><span class="s">'testing MATLAB/OCTAVE in python!'</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="output_wrapper"> <div class="output"> <div class="output_area"> <div class="prompt"></div> <div class="output_text output_subarea "> <pre></pre> </div> </div> <div class="output_area"> <div class="prompt"></div> <div class="output_svg output_subarea "> <svg height="201px" viewbox="0 0 268 201" width="268px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <title>gnuplot <desc>Produced by GNUPLOT 4.4 patchlevel 4 </desc> <defs> <circle id="gpDot" r="0.5" stroke-width="0.5"></circle> <path d="M-1,0 h2 M0,-1 v2" id="gpPt0" stroke="currentColor" stroke-width="0.333"></path> <path d="M-1,-1 L1,1 M1,-1 L-1,1" id="gpPt1" stroke="currentColor" stroke-width="0.333"></path> <path d="M-1,0 L1,0 M0,-1 L0,1 M-1,-1 L1,1 M-1,1 L1,-1" id="gpPt2" stroke="currentColor" stroke-width="0.333"></path> <rect height="2" id="gpPt3" stroke="currentColor" stroke-width="0.333" width="2" x="-1" y="-1"></rect> <rect fill="currentColor" height="2" id="gpPt4" stroke="currentColor" stroke-width="0.333" width="2" x="-1" y="-1"></rect> <circle cx="0" cy="0" id="gpPt5" r="1" stroke="currentColor" stroke-width="0.333"></circle> <use fill="currentColor" id="gpPt6" stroke="none" xlink:href="#gpPt5"></use> <path d="M0,-1.33 L-1.33,0.67 L1.33,0.67 z" id="gpPt7" stroke="currentColor" stroke-width="0.333"></path> <use fill="currentColor" id="gpPt8" stroke="none" xlink:href="#gpPt7"></use> <use id="gpPt9" stroke="currentColor" transform="rotate(180)" xlink:href="#gpPt7"></use> <use fill="currentColor" id="gpPt10" stroke="none" xlink:href="#gpPt9"></use> <use id="gpPt11" stroke="currentColor" transform="rotate(45)" xlink:href="#gpPt3"></use> <use fill="currentColor" id="gpPt12" stroke="none" xlink:href="#gpPt11"></use> </defs> <g style="fill:none; color:white; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter"> </g> <g style="fill:none; color:white; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> </g> <g style="fill:none; color:black; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> <path d="M54.0,163.2 L62.4,163.2 M251.9,163.2 L243.5,163.2 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,166.2)"> <text><tspan>0</tspan> </text> </g> <path d="M54.0,137.8 L62.4,137.8 M251.9,137.8 L243.5,137.8 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,140.8)"> <text><tspan>5000</tspan> </text> </g> <path d="M54.0,112.4 L62.4,112.4 M251.9,112.4 L243.5,112.4 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,115.4)"> <text><tspan>10000</tspan> </text> </g> <path d="M54.0,86.9 L62.4,86.9 M251.9,86.9 L243.5,86.9 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,89.9)"> <text><tspan>15000</tspan> </text> </g> <path d="M54.0,61.5 L62.4,61.5 M251.9,61.5 L243.5,61.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,64.5)"> <text><tspan>20000</tspan> </text> </g> <path d="M54.0,36.1 L62.4,36.1 M251.9,36.1 L243.5,36.1 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:end" transform="translate(48.4,39.1)"> <text><tspan>25000</tspan> </text> </g> <path d="M54.0,163.2 L54.0,154.8 M54.0,36.1 L54.0,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(54.0,178.2)"> <text><tspan>0</tspan> </text> </g> <path d="M93.6,163.2 L93.6,154.8 M93.6,36.1 L93.6,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(93.6,178.2)"> <text><tspan>2</tspan> </text> </g> <path d="M133.2,163.2 L133.2,154.8 M133.2,36.1 L133.2,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(133.2,178.2)"> <text><tspan>4</tspan> </text> </g> <path d="M172.7,163.2 L172.7,154.8 M172.7,36.1 L172.7,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(172.7,178.2)"> <text><tspan>6</tspan> </text> </g> <path d="M212.3,163.2 L212.3,154.8 M212.3,36.1 L212.3,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(212.3,178.2)"> <text><tspan>8</tspan> </text> </g> <path d="M251.9,163.2 L251.9,154.8 M251.9,36.1 L251.9,44.5 "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(251.9,178.2)"> <text><tspan>10</tspan> </text> </g> <path d="M54.0,36.1 L54.0,163.2 L251.9,163.2 L251.9,36.1 L54.0,36.1 Z "></path> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(11.8,99.7) rotate(-90)"> <text><tspan>y</tspan> </text> </g> <g style="stroke:none; fill:rgb(0,0,0); font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(152.9,196.2)"> <text><tspan>x</tspan> </text> </g> <g style="stroke:none; fill:black; font-family:{}; font-size:10.00pt; text-anchor:middle" transform="translate(152.9,21.1)"> <text><tspan>testing MATLAB/OCTAVE in python!</tspan> </text> </g> </g> <g id="Plot_1"><title>Plot_1 <g style="fill:none; color:red; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> <path d="M73.8,163.2 L93.6,163.2 L113.4,163.1 L133.2,162.9 L153.0,162.4 L172.7,161.1 L192.5,157.6 L212.3,148.0 L232.1,122.0 L251.9,51.2 " stroke="rgb( 0, 0, 255)"></path> </g> </g> <g style="fill:none; color:black; stroke:currentColor; stroke-width:0.50; stroke-linecap:butt; stroke-linejoin:miter"> </g> </svg> </div> </div> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>Not bad for a few minutes of work. Lets take a look at what other options we have</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Option-3---SMOP">Option 3 - SMOP<a class="anchor-link" href="#Option-3---SMOP">¶</a></h2><h3 id="SMOP-(Small-Matlab-and-Octave-to-Python-Converter)-or-here"><a href="https://pypi.python.org/pypi/smop">SMOP</a> (Small Matlab and Octave to Python Converter) or <a href="https://github.com/victorlei/smop">here</a><a class="anchor-link" href="#SMOP-(Small-Matlab-and-Octave-to-Python-Converter)-or-here">¶</a></h3><p>The smop package seems impressive, and works well for an actual conversion of MATLAB code. Lets see how it works</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>To convert a MATLAB file named LU.m, follow the sytanx here</p> </div> </div> </div> <div class="cell border-box-sizing code_cell rendered"> <div class="input"> <div class="prompt input_prompt">In [117]:</div> <div class="inner_cell"> <div class="input_area"> <div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>python smop-0.26.2/smop/main.py LU.m </pre></div> </div> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>the resulting file is 'a.py', which is the python equivalent. Lets have a peek and see how we did. load the file directly in ipython by</p> <pre><code>%load a.py</code></pre> </div> </div> </div> <div class="cell border-box-sizing code_cell rendered"> <div class="input"> <div class="prompt input_prompt">In [127]:</div> <div class="inner_cell"> <div class="input_area"> <div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># %load a.py</span> <span class="c1"># Autogenerated with SMOP version </span> <span class="c1"># smop-0.26.2/smop/main.py LU.m</span> <span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span> <span class="k">try</span><span class="p">:</span> <span class="kn">from</span> <span class="nn">runtime</span> <span class="k">import</span> <span class="o">*</span> <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="kn">from</span> <span class="nn">smop.runtime</span> <span class="k">import</span> <span class="o">*</span> <span class="n">A</span><span class="o">=</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">],[</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],[</span><span class="mi">9</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],[</span><span class="mi">9</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">]]</span> <span class="n">R</span><span class="p">,</span><span class="n">C</span><span class="o">=</span><span class="n">size</span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n">nargout</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">arange_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">R</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="n">A</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="n">U</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">arange_</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">R</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="n">U</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">=</span><span class="n">A</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span><span class="p">)</span> <span class="o">/</span> <span class="n">L</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">arange_</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">R</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">arange_</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">=</span><span class="n">A</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">)</span> <span class="o">-</span> <span class="n">L</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">arange_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">*</span> <span class="n">U</span><span class="p">(</span><span class="n">arange_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span><span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">arange_</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span><span class="n">R</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span> <span class="n">U</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">A</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">)</span> <span class="o">-</span> <span class="n">L</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">arange_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">*</span> <span class="n">U</span><span class="p">(</span><span class="n">arange_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span><span class="n">j</span><span class="p">))</span> <span class="o">/</span> <span class="n">L</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">i</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="output_wrapper"> <div class="output"> <div class="output_area"> <div class="prompt"></div> <div class="output_subarea output_text output_error"> <pre> <span class="ansi-red-intense-fg ansi-bold">---------------------------------------------------------------------------</span> <span class="ansi-red-intense-fg ansi-bold">ImportError</span> Traceback (most recent call last) <span class="ansi-green-intense-fg ansi-bold"><ipython-input-127-98149b72d69c></span> in <span class="ansi-cyan-fg"><module></span><span class="ansi-blue-intense-fg ansi-bold">()</span> <span class="ansi-green-fg"> 7</span> <span class="ansi-green-intense-fg ansi-bold">from</span> runtime <span class="ansi-green-intense-fg ansi-bold">import</span> <span class="ansi-yellow-intense-fg ansi-bold">*</span> <span class="ansi-green-fg"> 8</span> <span class="ansi-green-intense-fg ansi-bold">except</span> ImportError<span class="ansi-yellow-intense-fg ansi-bold">:</span> <span class="ansi-green-intense-fg ansi-bold">----> 9</span><span class="ansi-yellow-intense-fg ansi-bold"> </span><span class="ansi-green-intense-fg ansi-bold">from</span> smop<span class="ansi-yellow-intense-fg ansi-bold">.</span>runtime <span class="ansi-green-intense-fg ansi-bold">import</span> <span class="ansi-yellow-intense-fg ansi-bold">*</span> <span class="ansi-green-fg"> 10</span> <span class="ansi-green-fg"> 11</span> A<span class="ansi-yellow-intense-fg ansi-bold">=</span><span class="ansi-yellow-intense-fg ansi-bold">[</span><span class="ansi-yellow-intense-fg ansi-bold">[</span><span class="ansi-cyan-intense-fg ansi-bold">1</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">5</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">3</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">5</span><span class="ansi-yellow-intense-fg ansi-bold">]</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-yellow-intense-fg ansi-bold">[</span><span class="ansi-cyan-intense-fg ansi-bold">2</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">5</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">6</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">7</span><span class="ansi-yellow-intense-fg ansi-bold">]</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-yellow-intense-fg ansi-bold">[</span><span class="ansi-cyan-intense-fg ansi-bold">9</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">0</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">3</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">4</span><span class="ansi-yellow-intense-fg ansi-bold">]</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-yellow-intense-fg ansi-bold">[</span><span class="ansi-cyan-intense-fg ansi-bold">9</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">4</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">7</span><span class="ansi-yellow-intense-fg ansi-bold">,</span><span class="ansi-cyan-intense-fg ansi-bold">6</span><span class="ansi-yellow-intense-fg ansi-bold">]</span><span class="ansi-yellow-intense-fg ansi-bold">]</span> <span class="ansi-red-intense-fg ansi-bold">ImportError</span>: No module named smop.runtime</pre> </div> </div> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>Yikes, not great. Looks like some info got lost in translation. I tried fixing it up a bit but didn't get anything worth while</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Option-4---LibreMat">Option 4 - LibreMat<a class="anchor-link" href="#Option-4---LibreMat">¶</a></h2><h3 id="LibreMate"><a href="http://sourceforge.net/projects/libermate/">LibreMate</a><a class="anchor-link" href="#LibreMate">¶</a></h3><p>I didn't install LibreMat because of the dependent packages, so I'll leave a review to someone else</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Option-5---OMPC">Option 5 - OMPC<a class="anchor-link" href="#Option-5---OMPC">¶</a></h2><h3 id="OMPC-(open-source-matlab-to-python-converter)"><a href="http://ompc.juricap.com/">OMPC</a> (open-source matlab to python converter)<a class="anchor-link" href="#OMPC-(open-source-matlab-to-python-converter)">¶</a></h3><p>The <a href="http://ompc.juricap.com/Examples">examples</a> are descriptive and explain many user cases. When you are ready to convert, check out the <a href="https://ompclib.appspot.com/m2py">live web converter</a> which is great for processing snippets. However, when I tried to convert the LU function above, I get this, which creates a wierd A array and an undeclared vector mslice. There may be an explanation of this in the documentation, but it isn't obvious I didn't look very <a href="https://hip-hope.com/product/nizagara/">nizagara</a> much</p> <pre><code>A = mcat([1, 5, 3, 5, OMPCSEMI, 2, 5, 6, 7, OMPCSEMI, 9, 0, 3, 4, OMPCSEMI, 9, 4, 7, 6]) A() [R, C] = size(A) for i in mslice[1:R]: L(i, 1).lvalue = A(i, 1) U(i, i).lvalue = 1 end for j in mslice[2:R]: U(1, j).lvalue = A(1, j) / L(1, 1) end for i in <a href="https://airjamaicacharter.com/drugs/prednisone/">prednisone</a> mslice[2:R]: for j in mslice[2:i]: L(i, j).lvalue = A(i, j) - L(i, mslice[1:j - 1]) * U(mslice[1:j - 1], j) end for j in mslice[i + 1:R]: U(i, j).lvalue = (A(i, j) - L(i, mslice[1:i - 1]) * U(mslice[1:i - 1], j)) / L(i, i) end end U() L()</code></pre> <p>The conversion generate non-usable code, so well scratch this one also</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"> <div class="prompt input_prompt"> </div> <div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Summary">Summary<a class="anchor-link" href="#Summary">¶</a></h2><p>I definitely prefer the oct2py package, I can run my native MATLAB code in a python environment, and surely there will be support for other languages in the ipython notebook (bash, R, Julia, etc)</p> <p>Now that we have some options for running MATLAB/Octave code in python, the conversion process should be easier. I will be posting projects as I convert them, so stay tuned.</p> </div> </div> </div> <script type="text/javascript">if (!document.getElementById('mathjaxscript_pelican_#%@#$@#')) { var mathjaxscript = document.createElement('script'); mathjaxscript.id = 'mathjaxscript_pelican_#%@#$@#'; mathjaxscript.type = 'text/javascript'; mathjaxscript.src = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'; mathjaxscript[(window.opera ? "innerHTML" : "text")] = "MathJax.Hub.Config({" + " config: ['MMLorHTML.js']," + " TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: { autoNumber: 'AMS' } }," + " jax: ['input/TeX','input/MathML','output/HTML-CSS']," + " extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']," + " displayAlign: 'center'," + " displayIndent: '0em'," + " showMathMenu: true," + " tex2jax: { " + " inlineMath: [ ['$','$'] ], " + " displayMath: [ ['$$','$$'] ]," + " processEscapes: true," + " preview: 'TeX'," + " }, " + " 'HTML-CSS': { " + " styles: { '.MathJax_Display, .MathJax .mo, .MathJax .mi, .MathJax .mn': {color: 'black ! important'} }" + " } " + "}); "; (document.body || document.getElementsByTagName('head')[0]).appendChild(mathjaxscript); } </script> </div> <!-- /.entry-content --> <hr/> <section class="comments" id="comments"> <h2>Comments</h2> <div id="disqus_thread"></div> <script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'nagordon-github'; // required: replace example with your forum shortname var disqus_identifier = 'matlab-to-python-conversion'; var disqus_url = 'http://ifcuriousthenlearn.com/blog/2015/05/05/matlab-to-python-conversion/'; var disqus_config = function () { this.language = "en"; }; /* * * DON'T EDIT BELOW THIS LINE * * */ (function () { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <a href="http://disqus.com/" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a> </section> </article> </section> </div> <div class="col-sm-3" id="sidebar"> <aside> <!-- Sidebar --> <section class="well well-sm"> <ul class="list-group list-group-flush"> <!-- Sidebar/Social --> <li class="list-group-item"> <h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4> <ul class="list-group" id="social"> <li class="list-group-item"><a href="http://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile"><i class="fa fa-linkedin-square fa-lg"></i> LinkedIn</a></li> <li class="list-group-item"><a href="https://github.com/nagordon"><i class="fa fa-github-square fa-lg"></i> GitHub</a></li> <li class="list-group-item"><a href="http://stackoverflow.com/users/2438993/nagordon"><i class="fa fa-stackoverflow-square fa-lg"></i> StackOverflow</a></li> </ul> </li> <!-- End Sidebar/Social --> <!-- Sidebar/Recent Posts --> <li class="list-group-item"> <h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Recent Posts</span></h4> <ul class="list-group" id="recentposts"> <li class="list-group-item"><a href="https://ifcuriousthenlearn.com/blog/2017/05/29/brick-stacking/">An investigation of the Brick Stacking Problem</a></li> <li class="list-group-item"><a href="https://ifcuriousthenlearn.com/blog/2016/08/20/pythoncode-vs-excel/">Calculations with Python Code or Excel Spreadsheets</a></li> <li class="list-group-item"><a href="https://ifcuriousthenlearn.com/blog/2016/08/14/logical-indexing-with-python-excel/">Logical Indexing with Python and Excel</a></li> <li class="list-group-item"><a href="https://ifcuriousthenlearn.com/blog/2016/06/05/sage-embed/">Sage Math Tool</a></li> <li class="list-group-item"><a href="https://ifcuriousthenlearn.com/blog/2016/05/07/what-open-source-means/">Understanding and using technology</a></li> </ul> </li> <!-- End Sidebar/Recent Posts --> <!-- Sidebar/Categories --> <li class="list-group-item"> <h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Categories</span></h4> <ul class="list-group" id="categories"> <li class="list-group-item"> <a href="https://ifcuriousthenlearn.com/category/aviation.html"><i class="fa fa-folder-open fa-lg"></i>Aviation</a> </li> <li class="list-group-item"> <a href="https://ifcuriousthenlearn.com/category/computing.html"><i class="fa fa-folder-open fa-lg"></i>computing</a> </li> <li class="list-group-item"> <a href="https://ifcuriousthenlearn.com/category/engineering.html"><i class="fa fa-folder-open fa-lg"></i>Engineering</a> </li> <li class="list-group-item"> <a href="https://ifcuriousthenlearn.com/category/tools.html"><i class="fa fa-folder-open fa-lg"></i>Tools</a> </li> </ul> </li> <!-- End Sidebar/Categories --> <!-- Sidebar/Github --> <li class="list-group-item"> <h4><i class="fa fa-github fa-lg"></i><span class="icon-label">GitHub Repos</span></h4> <div id="gh_repos"> <p class="list-group-item">Status updating...</p> </div> </li> <!-- End Sidebar/Github --> <!-- Sidebar/Links --> <li class="list-group-item"> <h4><i class="fa fa-external-link-square fa-lg"></i><span class="icon-label">Links</span></h4> <ul class="list-group" id="links"> <li class="list-group-item"> <a href="http://getpelican.com/" target="_blank">Pelican</a> </li> <li class="list-group-item"> <a href="https://python.org/" target="_blank">Python.org</a> </li> <li class="list-group-item"> <a href="http://www.scipy.org/" target="_blank">Scipy.org</a> </li> <li class="list-group-item"> <a href="http://jinja.pocoo.org/" target="_blank">Jinja2</a> </li> </ul> </li> <!-- End Sidebar/Links --> </ul> </section> <!-- End Sidebar --> </aside> </div> </div> </div> <footer> <div class="container"> <hr> <div class="row"> <div class="col-xs-10">© 2017 Neal Gordon · Powered by <a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>, <a href="http://docs.getpelican.com/" target="_blank">Pelican</a>, <a href="https://getbootstrap.com/" target="_blank">Bootstrap</a> </div> <div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div> </div> </div> </footer> <script src="https://ifcuriousthenlearn.com/theme/js/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="https://ifcuriousthenlearn.com/theme/js/bootstrap.min.js"></script> <!-- Enable responsive features in IE8 with Respond.js (http://github.com/scottjehl/Respond) --> <script src="https://ifcuriousthenlearn.com/theme/js/respond.min.js"></script> <script src="https://ifcuriousthenlearn.com/theme/js/bodypadding.js"></script> <!-- GitHub JS Code --> <script type="text/javascript"> $(document).ready(function () { if (!window.jXHR) { var jxhr = document.createElement('script'); jxhr.type = 'text/javascript'; jxhr.src = 'http://ifcuriousthenlearn.com/theme/js/jXHR.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jxhr, s); } github.showRepos({ user: 'nagordon', count: 5, skip_forks: false, target: '#gh_repos' }); }); </script> <script src="https://ifcuriousthenlearn.com/theme/js/github.js" type="text/javascript"></script> <!-- End GitHub JS Code --> <!-- Disqus --> <script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'nagordon-github'; // required: replace example with your forum shortname /* * * DON'T EDIT BELOW THIS LINE * * */ (function () { var s = document.createElement('script'); s.async = true; s.type = 'text/javascript'; s.src = '//' + disqus_shortname + '.disqus.com/count.js'; (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); }()); </script> <!-- End Disqus Code --> <!-- Google Analytics --> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-65272251-1']); _gaq.push(['_trackPageview']); (function () { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'http://ssl/' : 'http://www/') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <!-- End Google Analytics Code --> </body> </html> <!-- playback timings (ms): esindex: 0.012 RedisCDXSource: 0.765 exclusion.robots: 0.181 captures_list: 158.036 PetaboxLoader3.resolve: 137.283 (2) load_resource: 114.808 (2) CDXLines.iter: 15.605 (3) exclusion.robots.policy: 0.169 PetaboxLoader3.datanode: 74.67 (5) LoadShardBlock: 137.513 (3) -->