Sunday, February 6, 2011

Python: Tips for Matlab Users: Standardizing Syntax

Remember how in Matlab you have to call libraries and denote commands with prefixes referring to the library they are hosted in? You don't? Because you DIDN'T:). You do in python though. This is really because python is not originally a scientific computing software but it is supposed to allow for addons, packages, and all kinds of extension including scientific computing. Interestingly enough, this is also the way to call functions from other py files (that's vaguely familiar in Matlab).

But python is a similar to C with respect to loading headers and similar to Matlab in the implementation. Like C we have to import our "modules" or "libraries" (same thing) or specific "functions." Before an example read this article about the "from" and "import" commands.

For our an example, lets solve a complex generalized eigenvalue problem, calculate the execution time and plot the spectrum. We need scipy (numpy can only do single matrix eigenvalue problems) and a time function. More specifically, we need the eig() function inside the linalg library in scipy.

First the correct and safe way:
Here we want to import the eig() function from the scipy.linalg library, then prefix all scipy commands with "sc" in case we want to use modules with conflicting function names. This requires two import statements. One for the special function eig and one to denote the scipy commands.
Next we want the plot library available so we import the matplotlib.pyplot with the prefix plt. The pyplot is the special collection of functions inside matplotlib to make nice plots - just like eig was in the linalg library in scipy. Finally we want to import the time function
Now we define the matrix size and generate random NxN complex matrices A and B. Start the clock. Compute the eigenvalues and eigenvectors (L,V). Generate the figure and define the active object number. Scatter plot the spectrum. Force the plot to draw. Display the calculation time. All this is shown below.

################################################
import scipy as sc
from scipy.linalg import eig

import matplotlib.pyplot as plt
import time

N=50
A=sc.random.random((N,N))+sc.random.random((N,N))*1j
B=sc.random.random((N,N))+sc.random.random((N,N))*1j
tic = time.time()
(L,V) = sc.linalg.eig(A,B)
toc = time.time()
print toc-tic, " has passed"
plt.figure(1)
plt.scatter(sc.real(L),sc.imag(L))
plt.show()
################################################

Notice how i use the sc prefix on every scipy command. This might be important if i'm also using built in sympy commands with the same function names...but i'm not. Let's do this again. This time no prefixes.

################################################
from scipy import *
from scipy.linalg import *
from matplotlib.pyplot import *
from time import *

N=50
A=random.random((N,N))+random.random((N,N))*1j
B=random.random((N,N))+random.random((N,N))*1j
tic = time()
(L,V) = eig(A,B)
toc = time()
print toc-tic, " has passed"
figure(1)
scatter(real(L),imag(L))
show()
################################################

The from import * commands give access to all the functions in the library...not just the eig function... and with no prefixes. The random still has a prefix and i'm not sure why or how to make it go away but I started learning python about 48 hours ago so oh well. Either way, this is similar syntax to matlab so python should be a viable alternative!

FYI run times for both are about the same for the eig command with both running 64 bit versions.

1 comment:

Cagatay said...

Thanks for the above important information on python. I believe SAGE has some fundamental modules on generalized eigenvalue problem and it can be used on other such problems.

FYI sage can interpret python code.

One can find useful information on this website:
http://www.sagemath.org/doc/constructions/linear_algebra.html

For freedom!!