# How much more work is it to code math models in Python, compared to working with Matlab?

If one had to code up a new dynamical system for a research group at a university, and the university has a Matlab total headcount license so that one could code in Matlab, are there any benefits to coding in Python instead? How much more work would it be to code in Python than in Matlab? About double the lines of code, perhaps?

For instance, in Matlab one could use the ode45 solver - but how would one solve a system of ODEs in Python? Write out a numerical method ... manually?

Someday, I won't be part of a lab anymore, and I will either have to purchase Matlab myself or learn Python, so I'm wondering about how the transition is like, and whether it's better to code in Python sooner than later. Also, industry employers love Python, not Matlab, it seems.

• GNU Octave and Scilab are free alternatives to Matlab, although they are not exactly 100% compatible. – Ertxiem - reinstate Monica Jan 2 at 1:27
• You might consider learning R as well. It's quite adept at mathematical manipulation. – João Mendes Jan 6 at 10:26
• What programs have you written or studied, in Matlab or in Python ? See Norvig, Teach Yourself Programming in Ten Years. – denis Jan 8 at 10:07

There are libraries that you can use in Python that will give you all (or at least nearly all) of the functionality of MATLAB. For example, scipy.integrate.solve_ivp() supports a number of methods for ODE integration that are comparable to what you can get with the various odexxx() functions in MATLAB. So no, you wouldn't have to write your own ODE integrator.

Whether you find Python and various libraries as easy to use or as well supported as MATLAB is a different question and really a matter of opinion. You've also touched on the problem that you may not have access to a MATLAB license after you leave your university.

• I love Python, but my experience (at least in my specific field of work) is that the "missing 5%" of functionality of Python is quite easy to stumble upon. For instance, recently I had trouble finding an analogue of condest and symmetric sparse direct solvers. – Federico Poloni Jan 2 at 10:31

Going from MATLAB to Python does introduce quite a bit of syntax overhead. One way to quantify it is the nice QuantEcon cheatsheet which showcases how there's a lot of extra "stuff" going on when trying to write simple linear algebra commands in Python. The verbose NumPy syntax is really just a symptom of how it was not developed as a technical computing language, so unlike in languages like MATLAB and Julia, scientific computing commands like those in linear algebra did not get nice special syntax. It's a small issue, but it does add up.

In general you can do what you did in MATLAB and translate it over to Python, but there are some caveats. There are a lot of areas in the standard scientific libraries in Python which are still underdeveloped. For example, in SciPy you won't find DAEs, so there's no direct alternative to ode15i. In addition, benchmarks do show that moving from MATLAB to Python will give a performance hit on non-stiff ODEs, and will give a performance hit on stiff ODEs if you don't use odeint or LSODA. This is where the dark side of SciPy's differential equation support appears. Its odeint and LSODA wrappers are by far its most efficient methods, but it just turns out that these are missing a lot of features. If you search the solve_ivp documentation page, you'll see for example that Jacobian sparsity handling and a bunch of other options are not available when using those methods, which means SciPy doesn't really have a good option when the stiff problems get difficult. There are lot of other issues to point out, like the stability of its event handling system, etc. So "it supports" a lot (but not all) of the things from MATLAB, but in many cases you can only use that option if you use the slow methods and/or the features are prone to bugs.

But there are other alternatives you might want to look at. Octave and Scilab are free MATLAB alternatives but once again lack a lot of the differential equation solver features you'd expect from MATLAB, such as event handling on DAEs. I also find that R's deSolve is much better behaved than SciPy, so that's worth a look too.

However, one alternative you may want to consider above all of those is Julia. As shown in the benchmarks above, switching from MATLAB to Julia can give around a 100x acceleration (usually we see around 20x-30x in real-world scenarios). The DifferentialEquations.jl library in Julia has a feature superset of the MATLAB ODE suite, which includes high order, implicit, adaptive, etc. methods for ODEs, SDEs, DAEs, DDEs, etc. The event handling is more fully featured, for example allowing changing the number of ODEs along the way, and features like automated GPU-acceleration and parameter estimation with adjoint sensitivities is built right into the library.

If you want a deeper overview of what's out there, I wrote a blog post that went into detail on what the methods are, the history behind them, and how they are evolving/continuing to be wrapped.

(Disclosure: I am the developer of the Julia DifferentialEquations.jl library, so please feel free to follow the links, look at the documentations, and re-run the benchmarks on your own computer to independently verify any statements.)

• I checked the differences between Matlab and Python, e.g. via the quantecon cheatsheet link you provided, and there's not much additional syntax overhead as you claim there is. – user33708 Jan 2 at 6:09
• @user33708 I edited your comment slightly for the tone purpose. Comments are meant to be more constructive on the clarification & improvements. I would encourage you to ask Chris a question to support his claim (which one(s)?); however, in a bit more friendly fashion. The chat is also a good option for a prolonged discussion. – Anton Menshov Jan 2 at 8:58
• Indeed I teetered a bit before answering this one because it's also a possible close vote as inviting too much opinion, but I tend to think disciplinary action should only be a last resort so I decided to just answer it with as many links as possible and mention why I randomly know each feature problem in SciPy's library. The QuantEcon cheatsheet is one way to measure the code difference and I'd eyeball it to be around 2x characters, but since I am not ready to count it exactly right now I decided to qualitatively summarize, and whether it's over the "too much" line is definitely personal. – Chris Rackauckas Jan 2 at 12:14
• As for the other features, the OP directly mentioned that they were interested in differential equations, and so doing a feature and performance comparison is one way to quantify the differences. You will indeed find SciPy misses some features of MATLAB's ODE suite, and indeed some of the missing features are not as obvious because the features can be solver dependent. Though note that SciPy is not a subset of MATLAB's feature's either: Shampine may have created the MATLAB suite and the stiffness detection error estimators, but did not create an LSODA-like auto-switching algorithm. – Chris Rackauckas Jan 2 at 12:17
• Whether or not the exact differences will impact you or your work is completely dependent on what you're doing, but in general you will find some feature loss if going to SciPy (other examples are mass matrices and lower order L-stable integrators), so you should double check what it has in comparison to what you are currently using in MATLAB. But one final note is that, I wouldn't worry about "what employers want": employers want to see impactful modeling, and you can learn that in any language. – Chris Rackauckas Jan 2 at 12:25

Learning Python allows you so solve many text-file parsing/processing and manipulation tasks (it is universal language).

But you should write the code in a way it will be useful to the team even in the case you will leave. So if nobody else uses Python with the libraries, it would be a bad taste to become the rogue Python coder. Ask the group leadership. Maybe they are interested to have someone explore Python, to have option. Maybe someone else already did, and found shortcomings you are not yet aware.

Maybe you will learn Python by yourself on the side, and when some file parsing tasks where Python excels will face your group, you will be ready to tackle them.