replacing Matlab with python
Asked Answered
G

8

17

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 computers 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?

Grosgrain answered 21/11, 2009 at 18:24 Comment(2)
If you're looking for an IDE, spyder is has a lot of the same functionality as matlab: en.wikipedia.org/wiki/Spyder_(software)Transduction
One of the most objective comparisons between MATLAB and Python I've read, which mentions many of the features that really make a difference is this.Gagger
T
21

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.

Tolman answered 21/11, 2009 at 18:55 Comment(5)
You may also want to install the developers tools; various modules do need the gcc compiler and make to install.Scanlan
"MATLAB parallels toolboxes are so expensive I've yet to see a University that actually has them." Actually the Parallel Computing Toolbox is just a standard toolbox and all of the university site licenses I've used include it. If you're talking about the Distribute Computing Server - then you're absolutely right that it's expensive, but you don't need it to run on 8 local cores.Entelechy
I stand corrected - I thought it was more expensive. Still not seen it in the UK though. To give you an idea at my top 10 >25,000 student university we have 2 optimization toolbox licenses! We had a new computer suite for an MSc course of about 10 computers - I looked at getting DCS and the cost in total was 3 x the cost of the hardware.Tolman
Ouch! That's rough. The cost of DCS is part of the reason I've started learning Python for some of my groups bigger projects. Overall great answer by the way. Enthought looks very handy (too bad the 64-bit version isn't freely available for academics).Entelechy
The 64-bit version of EPD is free for academics. You just have to give them your e-mail address.Ridgeling
U
13

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.

Urethrectomy answered 22/11, 2009 at 3:12 Comment(2)
Agree with everything. Would add if you need things like non-double sparse matrices (which matlab doesn't support), memory mapped files, low level control of memory (when things are copied etc.) go with Python. I think nice interfaces for Python are starting to show up now the scientific stack is starting to stabalise - see for example code.google.com/p/spyderlib - and I expect them to improve rapidly in the next couple of years.Tolman
With numpy, it's just as easy to work with matrices in python.Mehalick
H
6

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.

Howlan answered 21/11, 2009 at 18:24 Comment(1)
I've found the IDE in matlab the EXACT opposite of what you state, maybe because this is from 2013, but jesus, even then matlab's IDE was absolutely horrible compared to even the most basic Python IDE, like Spyder. It actually takes longer to program in matlab IDE than it does to use Visual Studio Code with a matlab plugin because of how bad it is. Its shameful that mathworks is still selling this product for as much as it does but doesn't even put effort into its own IDE for even the most basic of functionality.Trisaccharide
P
4

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.

Percept answered 21/11, 2009 at 18:27 Comment(0)
T
4

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).

Tarsuss answered 21/11, 2009 at 21:53 Comment(0)
V
1

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).

Volant answered 21/11, 2009 at 18:41 Comment(0)
C
0

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.

Centigram answered 21/11, 2009 at 18:36 Comment(2)
Default for array operations in Numpy is elementwise... lack of operators for matrix multiplication is easily overcome by using dot() or matrix class if you have a lot of linear algebra (for matrix class normal * operator is matrix multiplication)Tolman
I agree that you can work around this, but for somebody who spends all day working with matrices these workarounds are not as nice as what matlab style languages offer. Especially in dynamic typed languages where I don't know whether an object is an array or matrix and thus don't know the meaning of *. Element and object wise operators are also useful far beyond matrix multiply. For instance element and object wise logical operators serve different purposes and addition operators that impose column conformability (as in Stata's mata language) are also helpful.Centigram
L
0

It would be great if the matlab to python conversion mat2py project at sourcefourge took off..

Litta answered 25/11, 2009 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.