34
$\begingroup$

I am a beginning PhD student in math, and I would like to focus on optimization. I am learning programming for the first time, and I have written out some rudimentary optimization algorithms in both Python and MATLAB. From my very limited experience, MATLAB was a whole lot easier to use than Python. Being able to do matrix and vector operations directly in MATLAB (rather than having to go through NumPy in Python) was extremely nice.

I saw this question, and it appears that Python is extremely popular in industry, at least in optimization. I was wondering if, to maximize my job prospects, I should learn Python or MATLAB? I would be interested in answers from any field, not only optimization.

$\endgroup$
11
  • 2
    $\begingroup$ Probably you should learn both. There is one whale of a lot of stuff in MATLAB, sone of which is not available in Python, but things seem to be heading toward Python. $\endgroup$ Commented May 17, 2020 at 16:47
  • 1
    $\begingroup$ If you liked Matlab vector/matrix operations, and do not want to get stuck with commercial product, then why not Fortran? It is free (gfortran), has even better for vector/matrix operation than Matlab itself, and very very fast as well. $\endgroup$
    – Nasser
    Commented May 18, 2020 at 7:43
  • 1
    $\begingroup$ @Nasser Fortran is a completely different language (also from the level it operates on) compared to Matlab/Python. The latter allow you to write vector/matrix operations in a way simpler manner while executing Fortran/C code behind the scenes. Sure, Fortran still has its validity to be used, but for prototyping/production (mostly), Python is the way to go. As a note: if you need to implement a routine, then maybe Fortran can be useful again, but then it's easy enough to learn $\endgroup$
    – Mayou36
    Commented May 18, 2020 at 8:51
  • 6
    $\begingroup$ There's also the snarky comment "if Matlab is an option, Julia is the solution", but I conceit that people choose Matlab not for the language, but for specific toolboxes that are available for it exclusively. $\endgroup$ Commented May 18, 2020 at 16:55
  • 6
    $\begingroup$ As Robert suggested above, I would urge you to consider Julia as it's a strong candidate for a language very well suited to the domain you are describing. $\endgroup$
    – Alec
    Commented May 19, 2020 at 2:26

12 Answers 12

55
$\begingroup$

Regardless of what completes the phrase "Python vs ...", the answer is always going to be Python.

Very few people who are serious about using optimisation in production use MATLAB, and the ones who do can't wait to move away from it.

As to why, there are plenty of reasons. MATLAB is:

  • licensed

  • closed source

  • not object-oriented friendly, so it's a very bad choice for complex software

  • hard to distribute computations, and has a very expensive license for doing so

  • very hard to interface with

  • not scalable

  • nigh-impossible to hire professional programmers for

Seriously, if you want to be able to use your own code after your PhD, don't use commercial packages.

From a programming perspective, I have personal experience in the bottlenecks. I wrote the very first version of my solver in MATLAB, and I could solve problems of ~5 variables in reasonable time. Moving to C++ increased that to 200 variables. Rewriting the solver in C++ once I had experience increased that to 100,000 variables, and hiring professional developers increased that to >1,000,000 variables.

What's interesting here is that with the experience I have now, 10 years later, I know there's no way I could have ever scaled our software beyond 100 variables in MATLAB, but I could have in Python.

$\endgroup$
13
  • $\begingroup$ Thanks for your answer; lesson learned, I'll veer away from Matlab. I see you're also a fan of C++. Would you say I should stick to Python, or try C++? $\endgroup$
    – Blue
    Commented May 17, 2020 at 22:05
  • 1
    $\begingroup$ Python is far easier to learn and better in virtually every way for beginners but remember, any code written in python can be written in C++ for better performance. If performance is the deciding factor, I'd recommend trying out Cpython before you move to C++. $\endgroup$
    – Krish
    Commented May 18, 2020 at 11:37
  • 1
    $\begingroup$ Although i in general echo @Krish sensiment, learning c++ does force you to learn a lot of concepts, which will also help you write efficient and scalable python. So don't do it because you assume you will need the performance, but do it to become a better programmer in general. $\endgroup$ Commented May 18, 2020 at 11:48
  • 5
    $\begingroup$ @Krish a bit pedantic, but to avoid confusion: "CPython" is just the standard Python interpreter, you probably mean "Cython" which allows fully compilable functions with c-like types. Although CPython does support foreign function interfaces and modules written in "real" C++ $\endgroup$
    – ManfP
    Commented May 18, 2020 at 12:25
  • 1
    $\begingroup$ And if you are concerned about using proprietary software, don't forget about GNU Octave. $\endgroup$ Commented May 18, 2020 at 16:04
20
$\begingroup$

I agree with everything Nikos said and I add some colors to some of the reasons:

  • Python is free and open-source but Matlab is not. Anyone can write codes in Python and share it with others who can easily run that code (as it's free software) but your Matlab codes can be run only by those who have a license.
  • Just because Python is open-source and free, it means it's widely available to a larger audience. And when you're talking about projects in industry, spending money on a piece of software, IMO, is not justifiable when there is a widely accessible alternative.
  • It makes more sense not to tie a piece of code to a person. Since there are more and more Python users, almost anyone can read and modify your Python code in a company, but if you leave a place and there are not many proficient in Matlab, that's a big loss for the company. It can be easily avoided by using general-purpose programming languages such as Python.
  • Remember that the work in the industry is not just focused on matrix and vector operations. So, my suggestion: gradually start getting out of Matlab comfort zone.
$\endgroup$
3
  • $\begingroup$ You can distribute MATLAB code using compiler; and I don't think many care about stuff being open source. The non-free is a huge MATLAB negative though and the main reason everyone picks Python (unless they have to use MATLAB for some reason). $\endgroup$ Commented May 19, 2020 at 15:21
  • 1
    $\begingroup$ @ZizyArcher The compiler only works with a subset of MATLAB's functionality. If you want to package arbitrary MATLAB code as a standalone program executed by the MATLAB Runtime, you can; however, the MATLAB Runtime is a ridiculous 2GB download. $\endgroup$ Commented May 19, 2020 at 18:05
  • 2
    $\begingroup$ +1. I'd emphasize that there are lots of "non-optimizer" software developers out there that an "optimizer" will need to work with, because the optimizing code is often only part of a (much) larger architecture. And these developers will all speak Python and not Matlab. Thus, being fluent in Python will make slotting into a larger team much easier. $\endgroup$ Commented May 19, 2020 at 18:59
17
$\begingroup$

Nikos Kazazakis and EhsanK have given you great reasons for using Python. I will focus on the point from you about needing to use an additional package/library in Python for matrix and vector operations. In industry projects you will encounter many challenges and things you want to do, but you don't want to (and shouldn't) implement. Currently I work on a route planning software, most of what I do is sadly not core optimization work, but everything needed to making it useful for customers. Make imports from various data sources, integrating the data into objects (doing this in a non object oriented language would be annoying), make your product available via an API, and so on. This is to say I use a lot of things that come not out of the box. Adding numpy to that list of imports for the optimization work won't break the camel's back.

All this is to say: needing to import an additional library for functionality shouldn't be used to determine usefulness of a language most of the time ;).

$\endgroup$
1
  • $\begingroup$ I see, I never looked at it from that perspective. Thanks for the answer! $\endgroup$
    – Blue
    Commented May 18, 2020 at 14:10
15
$\begingroup$

I work for a company that offers a commercial optimization solver. The solver offers interfaces to both MATLAB and Python for solving problems defined in those languages.

We only get one or two queries about MATLAB interfacing with our product per year, but we get many times that amount of questions on interfacing Python with our product.

Many bluechip and non B.C companies already have Python in heavy use. If I were you, I would concentrate on becoming knowledgeable with Python, not just for optimization purposes, but it's very useful in other areas of computing too.

$\endgroup$
4
  • $\begingroup$ Thanks, I appreciate your advice! Will definitely stick with Python. $\endgroup$
    – Blue
    Commented May 18, 2020 at 14:12
  • 4
    $\begingroup$ What is a "B.C company"? $\endgroup$ Commented May 18, 2020 at 14:52
  • 1
    $\begingroup$ B.C. = Blue Chip. $\endgroup$
    – Jason
    Commented May 19, 2020 at 3:10
  • 10
    $\begingroup$ For the non-Americans among us: a blue chip company is a recognized, well-established company. A high-value company that can take a beating. $\endgroup$
    – Mast
    Commented May 19, 2020 at 9:17
9
$\begingroup$

MATLAB is a language built on top of a library. Python (with NumPy & numba) is a language with a library built under it.

Neither is ideal. Like all languages, both have a few quirks, due to their history.

My suggestion: Door Number 3, Julia.

In either case (MATLAB, Python, Julia), you should ask yourself:

  1. Is your immediate goal to master the math, or to master how to program it?
  2. Is your long-term career goal academia, or industry?
  3. Is it important to you that other people can run your code (e.g. github repo)?
  4. Is your code all short, or might it grow into a large (many 1000-line, 100's of functions) code?

If your answers are: 1: math, 2: academia, 3: no, 4: all short, then MATLAB is fine. It is an excellent tool. I love it for doing something fast and not fussing with things; it's an industry standard in DSP and radar and other problems that rely very heavily on linear algebra. There is a lot of code written in it; it will be around for a long time to come.

Nevertheless, if those are not your answers, then between the two, I definitely suggest Python over MATLAB. You will become a much better programmer, and your job prospects will be much better, too. If your code gets long, beware that managing a large (many 1000-lines) MATLAB code is a nightmare IMO (namespaces, anyone?). If you find you have to use MATLAB at some later date, it will be easy. Conversely, if you learn MATLAB and later on find you need to learn Python + NumPy + numba, then you will probably find this very difficult.

This is all the more true if we are talking about optimization, which is at least tangential to machine learning (ML). MATLAB is trying to compete in this space, but if you look online for job ads, they are almost all asking for Python, not MATLAB. In fact, in ML, you will find (in my experience) more job ads asking for Julia than for MATLAB.

But again (and yes it is just my personal opinion), I would suggest you take a look at Julia. I have not been as excited about a language after trying Julia since.... well, since I started learning Python 18 years ago. I use all three at work, and Julia is my first choice most of the time; I think the language is truly going places. But, I'm a risk-taker. It's not the safe move. If you want safe, the safe move is Python.

Disclosure: I have no interests, financial or otherwise, in MATLAB, Python, or Julia, other than my own experience using them for work/research.

$\endgroup$
1
  • 2
    $\begingroup$ Another thing that's nice about Julia is that the community of Julia users who actually use the language for operations research is very close-knit and welcoming. If you post on the Julia discourse forum it is very common for the developers of JuMP and other optimization packages to respond directly, sometimes within 3 hours. Python has a very active SO tag, but it is a wide open field with lots of people who are Python experts but not optimization specialists, and this makes it hard to find common ground if you have a strong background in optimization theory and are new to coding. $\endgroup$
    – Max
    Commented Apr 1, 2022 at 5:18
8
$\begingroup$

I did my PhD on a topic involving numerical simulations of mechanical systems. I worked primarily in MATLAB, which I already had experience in and seemed to have some good 'out of the box' optimisation algorithms.

Looking back, I wish I did my work in Python. I work in Software Engineering now and Python is a lot more applicable to other languages and looks better on a CV. Also - as mentioned by others here - Python is open source. This means that your results would be more accessible by others and easier to reproduce (a big plus for research).

$\endgroup$
1
  • 1
    $\begingroup$ For a lot of work, reproducibility (more specifically, the ease or ability to even try to reproduce it) is a minus for their research, because it easier to determine it's bogus. $\endgroup$ Commented May 19, 2020 at 22:05
7
$\begingroup$

I am geophysics professor and have been solving scientific computing problems in Matlab since 2000. In the last ~8 years graduate students have been preferring to work in Python. I have the following observations:

On a practical level Python is MUCH slower than Matlab.

Code that my graduate students write is literally orders of magnitude slower than my Matlab for solution for matrices that arise from discretizing PDE's.

How can this be, aren't both using the same libraries under the hood? Yes, but clearly Matlab is much better at recognizing special forms of matrices and choosing optimized solvers for them. Once my graduate students have have put several weeks into optimizing their code, it is still much slower (seconds vs. tens of minutes). You may think my graduate students suck - but some of them are in a leading computational math graduate program.

One reason I am more likely to switch to Julia than Python is that many of the advertised advantages of Python, such as great string manipulation simply don't matter for scientific programming. Also the syntax is clumsy and verbose. Most people that know what they are doing are picking up Julia. For example the new Climate model (https://clima.caltech.edu/) is being developed in Julia, not Python. The same is true for various Astrophysical codes (https://juliacomputing.com/case-studies/celeste/).

$\endgroup$
3
  • $\begingroup$ May be true, but Julia has a marketing problem. It doesn't seem to have caught on in industry (yet). Perhaps part of the problem is a not very sexy name. Julia. Sounds like someone's great aunt (a not very exciting old lady). Here's what Wikipedia says on the naming of Julia : 'In an interview with InfoWorld in April 2012, Karpinski said of the name "Julia": "There's no good reason, really. It just seemed like a pretty name."[33] Bezanson said he chose the name on the recommendation of a friend.[34]'' $\endgroup$ Commented Nov 17, 2020 at 12:57
  • $\begingroup$ Check do your student use jit, it is enabled by default in Matlab but Python requires installing a special library (i.e. module) such as numba. When jit is enabled Python might well outperform Matlab . See a benchmark at stackoverflow.com/a/51911200/5874981 for time series sample entropy. Julia is an interesting though a NIche project. For things where speed is crucial Rust or old good C++ is a better bet and Python is catching up with speed. $\endgroup$
    – Serge
    Commented Jul 20, 2023 at 15:53
  • $\begingroup$ True for a beginner Matlab is easier in implementing a number crunching efficiently, as you do not have to learn to use proper libraries say for matrix manipulation and jit. $\endgroup$
    – Serge
    Commented Jul 20, 2023 at 15:53
4
$\begingroup$

My first language I have learned was MATLAB. After learning C++ I realized MATLAB is bad for really learning about programming in such. I would recommend you also Python as language because it is easier to switch from Python to MATLAB then in the other direction.

Besides the downturns of commercial and closed software you should also keep in mind that MATLAB is a working universe in itself. For example you can really quick do a program for image pattern recognition without having (to much) knowledge about image processing and link your code into another toolbox for autonomous driving (just as an example, because that is something people like to forget about MATLAB). Python has a little bit steeper learning curve but for your case it is much worther I think.

$\endgroup$
4
$\begingroup$

Sounds like you are intending to build solvers. You will be better off in MATLAB. As you noted, matrix and vector operations are part of the language. You'll need matrix decompositions and maybe eigenvalue solvers to build your solvers, probably for dense and sparse matrices, and MATLAB has all of those. If you find that there's a library you need in some other language, you can call it from MATLAB.

There are lots of solvers written in MATLAB, for example, SDPT3 and SeDuMi, the solvers inside of CVX. Besides github, look on the MATLAB File Exchange to see more.

Full disclosure: I work at MathWorks. But was using MATLAB a long time before that!

$\endgroup$
2
$\begingroup$

I'd go with Python because

  • Python is a general purpose programming language and is much more widely used in industry than MATLAB. In the industry you rarely find yourself just code optimization algorithms or models in your daily work. It is more like you write an entire pipeline from data acquisition, data cleaning, model development (both statistical predictive models and/or optimization models) to visualization. Python provides more flexibility in that sense and have more APIs developed to communicate with other tools or applications (e.g databases) used in the pipelines.

  • The trend in industry is that many companies are moving to Python or using Python as the primary languages for analytics work, which means you can get more support from your IT department in terms infrastructure and computational capacity. For example, would your future employer's MATLAB capacity allow you to process tens of millions or billions of records? It is more likely that its Python infrastructure allows so because there are many other teams needing that capacity.

  • In your industry career, you would sooner or later collaborate with (e.g. code sharing) people from non-engineering background. Python is more widely used among them such as statisticians.

  • Have a look at the job description of your future ideal job and you will get a sense about what is needed.

$\endgroup$
2
$\begingroup$

As a veteran MATLAB user, I'm horrified by some of the conventions the developers of Python chose to use. There are some inconsistencies in array notation that are very troubling. They decided to defy convention used in many other programming languages, including MATLAB, C, FORTRAN, Julia, etc. Matrix manipulation is extremely difficult and prone to error in Python. Yeah, it's free, but if it takes you a long time to try to build something that works, what good is that? There's a free MATLAB alternative in Octave, which everyone conveniently forgets, too. MATLAB is a natural language for matrix manipulation, which is crucial in every field. Practically all of the applications that Python claims to have were already developed in MATLAB.

$\endgroup$
2
  • $\begingroup$ Thanks for your answer. But isn't Matlab much slower for large problems, as the other answers claim? $\endgroup$
    – Blue
    Commented Oct 10, 2021 at 17:37
  • $\begingroup$ MATLAB is not slower than Python. It's generally faster. Very old versions of MATLAB were slow, but more recent versions are quite fast for an interpreted language. I suspect the reason for it is MATLAB probably compiles more functions than it once did, so that complex computations are more quickly computed rather than broken down into sequences of simpler compilations. $\endgroup$ Commented Jan 13, 2022 at 6:39
2
$\begingroup$

In 2017 I was a grizzled veteran of Matlab and a reluctant Python novice. I had just started a new position at a startup that was about to conduct their first trials of their flagship device, second generation. In the scramble of the final days before trial began, a decision was made that calibration should no longer be manual, but automated (because duh). No big deal. Simple Matlab script and Matlab compiler, and we’d be good to go. Afternoon’s worth of work, easy. But I had not yet installed Matlab on my computer. And the guy I replaced hadn’t shared the password to his computer. And we were out of licenses, and had no budget. And when I reached out to Matlab’s support team, I was ping ponged between people who were less than motivated to help our little startup put out this little fire we were dealing with. And when the licensing issue was resolved, I goofed the install (which I hadn’t done myself since I was a student) and needed more help from Matlab support. While all of this was going on, I was also beating my brains against the keyboard learning the nuances of NumPy. By the time I got Matlab up and running, I had already solved the problem, and shipped a critical piece of code in Python. I have not used Matlab again since, and I don’t miss it. A year later, I was promoted to the systems team lead, right around the time we were due to renew our Matlab support. Which we did not do. Python til I die.

$\endgroup$
1
  • $\begingroup$ Haha thanks for the entertaining story. $\endgroup$
    – user56202
    Commented Apr 21, 2022 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.