replacing Matlab with python
i am a engineering student and i have to do a lot of numerical processing, plots, simulations etc. The tool that i use currently is Matlab. I use it in my university com开发者_Python百科puters for most of my assignments. However, i want to know what are the free options available.
i have done some research and many have said that python is a worthy replacement for matlab in various scenarios. i want to know how to do all this with python. i am using a mac so how do i install the different python packages. what are those packages? is it really a viable alternative? what are the things i can and cannot do using this python setup?
On a Mac the easiest ways to get started are (in no particular order):
- Enthought Python Distribution which includes most scientific packages you are likely to need. Free for academic/non-commercial use.
- Macports - up to date with latest releases, so
sudo port install py26-numpy py26-scipy py26-matplotlib py26-ipython
should get you started. - Scipy Superpack - script to install recent svn versions of all the important packages.
I've done exactly this (replace Matlab with Python) about 2 years ago and haven't looked back. The broadcasting in Python, more intuitive memory model and other Numpy advantages make numerical work a complete pleasure. Plus with f2py, cython it is incredibly easy to put inner loops in another language. This is a good place to start - other impressive pages to provide motiviation are PerformancePython and ParallelProgramming. Be sure to understand Pythons "variable is a reference to an object" semantics... after that adjustment everything is plain sailing. One of the coolest things that beats matlab is in 2 lines I run over 8 cores... p = Pool(8); res = p.map(analysis_function,list_of_data)
- MATLAB parallels toolboxes are so expensive I've yet to see a University that actually has them.
I've been programming with Matlab for about 15 years, and with Python for about 10. It usually breaks down this way:
If you can satisfy the following conditions: 1. You primarily use matrices and matrix operations 2. You have the money for a Matlab license 3. You work on a platform that mathworks supports
Then, by all means, use Matlab. Otherwise, if you have data structures other than matrices, want an open-source option that allows you to deliver solutions without worrying about licenses, and need to build on platforms that mathworks does not support; then, go with Python.
The matlab language is clunky, but the user interface is slick. The Python language is very nice -- with iterators, generators, and functional programming tools that matlab lacks; however, you will have to pick and choose to put together a nice slick interface if you don't like (or can't use) SAGE.
I hope that helps.
There are three aspects to consider when replacing Matlab with Python; the core language capabilities, the IDE and the Price.
Capabilities Comparison
This section outlines all of the capabilities of each platform. In short everything Matlab can do, Python can also do plus much more. However, things like linear algebra and rapid prototyping Matlab does more cleanly.
Matlab
Everything in Matlab is ready bundled and toolboxes are seamlessly integrated. It is also much more mature - but was not designed for general purpose programming meaning anything not linear algebra related is painful to implement.
- Toolboxes: The Matlab toolboxes are very good and python implementations often struggle to compete with them (even for not so specialised things like optimisation algorithms).
- Linear Algebra: Matlab has concise linear algebra capabilities which are easier to read and interpret.
- Visualisation Capabilities: Has very good and very broad range of visualisation capabilities.
Python
Python will require a number of additional modules for it to become a good Matlabreplacement.
- General Purpose Programming: Python is a proper programming language which offers a lot more flexibility over Matlab.
- Numpy: Provides arrays and linear algebra routines, however has more awkward syntax than Matlab. I have fallen in love with the broadcasting feature however, no more
repmat
rubbish. - SciPy: Stats, optimization and other useful mathematical stuff.
- MatPlotLib: Provides, in my opinion, higher quality plots than Matlab. Furthermore it is designed to be syntactically similar to Matlab so should be easy to pick up.
- SymPy: Good symbolic programming package.
IDE Comparison
This is where python is lacking in my opinion. I simply find algorithm development (particularly involving a lot of linear algebra) less painful in the Matlab IDE.
Matlab
Here are some key Matlab features which will be hard to live without as an engineering student. Note that Python IDEs do have some of these capabilities also, but are more sloppily implemented.
- Powerful Debugging: Can easily explore functions in a very non-restrictive manner. Python also allows for debugging but is often not as cleanly implemented in the IDEs.
- Variable Explorer: MATLAB has a good variable explorer which always good for viewing of many supported data types. Some python IDEs struggle to display numpy arrays which can be annoying.
- Variable Survival: If code breaks during runtime, the variables still persist which helps debugging. Furthermore it is easier to run different scripts in a row assuming persistent variables. In Python you will have to manually save variables in a script to have them available for other scripts.
- Profiler: Very good for a breakdown of where your algorithms are bottlenecking.
- Command Line Single line instructions can be executed on the command line. Python is full support for this also in all good IDEs.
- GUI Plotting Tools: Results and variables can be quickly and painlessly plotted from the user interface. Most Python IDEs are geared towards software development and so don't support this (although sypder does have limited support through matplotlib).
- Documentation: All functions have native documentation within the IDE.
Python
There are some new python IDEs which are starting to compete with some of MATLAB's key advantages. I personally like PyCharm which was recently released as a free community version. It has the following capabilities and is very well designed from a UI perspective.
- Bebugging
- Variable Explorer
- Command Line
- Documentation
Price Comparison
Python is free and has an active support community. Matlab also had a good support community too, but is anything but free.
Maybe you like sagemath, which 'combines the power of many existing open-source packages into a common Python-based interface'. Here is a feature tour.
Try Sage - it is designed as an open source replacement for Matlab, Mathematica etc. It is implemented in Python and can be scripted with Python, but it also adds a lot of maths-specific features. There is an installer for the Mac, so you will not need to download lots of individual packages.
There is also GNU Octave - another open source alternative to Mathematica/Matab that has its own programming language. However I have not found any information on a Mac version (though I have not looked very hard).
python(x,y) is quite powerful, but only for Windows or Linux so you'll have to use bootcamp or Linux. A more lightweight package for mathematics is Matplotlib, which basically adds plotting abilities to the Python language (better used together with IPython).
Duplicate of this.
My sense is that for pure numerical/linear algebra computations and visualization Matlab is a slightly more consistent development environment. Numpy/Scipy/Matplotlib feel, to me, a bit haphazard. If you are building a full program -- to automate a system or display results on a webpage -- Python the advantage of being a real programming language first and foremost. But for interactive numerical processing I think Matlab still wins. The lack of element wise infix operators in Python is one small example (PEP 225). For statistical computing, data exploration and visualization, it is hard to beat R.
It would be great if the matlab to python conversion mat2py project at sourcefourge took off..
精彩评论