* Using Python for Scientific Computation (Pythonを科学技術計算に使う) [#s9a4a4f6]
** Introduction [#h5ede8ef]
Matlab sucks because it is too expensive for students and even for professional researchers who have not enough funding (like me :-). Actually, Mathworks offer a cheap student version of Matlab+some toolboxes for $200 or so. Therefore, the financial problem is more serious for researchers like me. That was the main motivation for me to start exploring possibilities of using Python as a replacement for Matlab. Over the past few months, I did some investigations and tests on Python and concluded that Python can be a replacement of Matlab in most cases, as far as my needs are concerned. Moreover, I found that Python surpasses Matlab in many aspects, so it is not just a Matlab replacement but much more. Now I'm migrating from Matlab to Python rapidly. This page is intended to be a place to dump some know-hows on using Python as a scientific/technical computation platform.
** What is Python [#fbadccd6]
Want to know what the heck is Python ? Read [[Wikipedia>http://en.wikipedia.org/wiki/Python_(programming_language)]]
** Setting up a working environment [#s2d26ba5]
*** Overview [#z4297315]
It is very important for me to prepare a comfortable and efficient working environment before using a programing language. Python can be used in various ways. Python's core is its interpreter. So you can write a program using your favorite editor and pass the file to the Python interpreter. However, this may not be the best way to do scientific computation. While you are examining your experimental data and tweaking it, interactive session is often useful and powerful.
Matlab provides such a capability through its own IDE (Integrated Development Environment). Python interpreter can also be invoked in interactive mode, but it is not that useful by itself. There are several Python IDEs which provide similar look & feel to the Matlab IDE. [[IDLE>http://docs.python.org/library/idle.html]] is a python IDE distributed with
the standard python package. Wikipedia has [[a list of Python IDEs>http://en.wikipedia.org/wiki/List_of_integrated_development_environments_for_Python]]. You can choose whichever one you like to do your Python programing.
the standard python package. Wikipedia has [[a list of Python IDEs>http://en.wikipedia.org/wiki/List_of_integrated_development_environments_for_Python]]. You can choose whichever one you like to do your Python programing. At the moment, Spyder seems to be the most Matlab like IDE for Python.
*** [[Using Python with Emacs>EmacsPython]] [#j8f1feb6]
As for myself, I don't like those IDEs for not well defined reasons. My default editor is Emacs and I feel comfortable doing everything in Emacs. So I decided to use Emacs as my Python development environment. Actually, I used to use Matlab from Emacs. Therefore, using Python through Emacs is a natural move for me.
A nice feature of Matlab standard IDE is the cell. You can divide a script into several parts (cells) by putting dividers (%%) in the file. Then you can execute each cell by pressing Ctrl-Enter with the cursor in a cell. This way, you can execute a small portion of a long script without executing the whole script. This is useful, for example, when you have a long calculation and just want to change the appearance of the plot at the end. ~
I also like the cell of Mathematica notebook, where you can have nested (hierarchical) cells. You can also open and close cells to improve the readability of a long script.
I don't know if any of the Python IDEs listed in the Wikipedia page has such a functionality but I knew that with Emacs, you can easily create such a function using Emacs Lisp. Therefore, I added a cell functionality to python-mode.el. If you are interested in my Emacs-Python environment, read [[this>EmacsPython]].
The above tutorial applies mainly to linux users, but Windows users should be able to do the same thing, in principle, as [[Emacs now works fine on Windows>http://ftp.gnu.org/gnu/emacs/windows/]]. I don't know anything about Mac, but [[Carbon Emacs>http://homepage.mac.com/zenitani/emacs-e.html]] may work.
*** Windows and Python [#ed78c524]
For Windows users, there are two easy ways to install Python and scientific extensions at once without a hustle. One is [[Enthought Python Distribution (EPD)>http://www.enthought.com/products/epd.php]] and the other one is [[python(x,y)>http://www.pythonxy.com/foreword.php]]. EPD is also available for Mac, Linux and Solaris. python(x,y) is available for Linux too. EPD is basically a commercial distribution. However, you can use it for free if you are in an academic institution. python(x,y) is free for everyone.
For Windows users, there are two easy ways to install Python and scientific extensions at once without a hustle. One is [[Enthought Python Distribution (EPD)>http://www.enthought.com/products/epd.php]] and the other one is [[python(x,y)>http://www.pythonxy.com/foreword.php]].
EPD is a commercial product. However, for academic use, it is free. Go to the download page of the EPD website. At the bottom, you will find a link to academic version. You must enter your valid email address with a domain name of an academic institute (i.e. an email address ends with "ac.jp"). The academic version of EPD is available in 64bit for all platforms. EPD is also available for Mac, Linux and Solaris.
python(x,y) is available for Windows and Linux. python(x,y) is free for everyone.
However, it provides 32bit version only at the moment.
A bit more detailed explanation of how to install EPD and Spyder is [[here>EPDandSpyder]].
** Examples [#t0e36105]
*** numpy basics [#g886b10b]
*** Matrix [#m1dce5e0]
*** Signal Processing [#vfbe6831]
** Online Resources [#cd5a378a]
- Python itself
-- [[Python Documentation Index>http://www.python.org/doc/]] Official Python Documentation. It includes a tutorial and library references.
-- [[Dive into Python>http://www.diveintopython.org/toc/index.html]] This is a nice introductory book for Python. Available in html and PDF.
- Numpy & Scipy
-- [[Scipy.org>http://www.scipy.org/]] The portal for Numpy&Scipy related resources.
-- [[Guide to Numpy>http://www.tramy.us/numpybook.pdf]] An extensive reference book of Numpy by the author of Numpy, Travis E. Oliphant.
-- [[Numpy & Scipy documentation>http://docs.scipy.org/doc/]] Tutorials and references for Numpy and Scipy.
-- [[Additional Documentation>http://www.scipy.org/Additional_Documentation]] Other tutorials and references for Numpy and Scipy.
-- [[Numpy for MATLAB users>http://www.scipy.org/NumPy_for_Matlab_Users]] This is a useful comparison table of MATLAB and Numpy functionalities.
-- [[Cookbook>http://www.scipy.org/Cookbook]] A collection of examples for Numpy and Scipy.
-- [[Official web page>http://matplotlib.sourceforge.net/]] It includes detailed tutorials and API references as well as an extensive collection of examples.
-- [[Official web page>http://ipython.scipy.org/moin/]]
-- [[A Demonstration of the 'IPython' Interactive Shell>http://showmedo.com/videotutorials/series?name=CnluURUTV]] A very nice video tutorial. You must watch it.
-- [[Scientific and parallel computing using IPython>http://showmedo.com/videotutorials/series?name=N49qyIFOh]] A nice vide tutorial series on how to use Python for scientific computing including parallel computing.
-- [[ShowMeDo>http://showmedo.com/]] This site includes many useful video tutorials for Python and other computer languages/softwares.
-- [[Scipy2009 Conference>http://conference.scipy.org/]] An annual conference on scientific computing using Python. It includes slides, videos and [[proceedings>http://conference.scipy.org/proceedings/SciPy2008]] from the previous year conference. There were [[introductory tutorials>http://conference.scipy.org/intro_tutorial]] and [[advanced tutorials>http://conference.scipy.org/advanced_tutorials]] given in the conference. All the videos and slides are available online. Very nice.
-- [[Enthought>http://enthought.com/]] A sponsor company for Numpy&Scipy. They have monthly webinars of Python for scientific computing.
-- [[Open source tools by Enthought>http://code.enthought.com/]] There are number of useful tools Enthought developed to use primarily for their products and released open source.