26
$\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$
  • 1
    $\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$ – Mark L. Stone May 17 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 May 18 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 May 18 at 8:51
  • 2
    $\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$ – Robert Schwarz May 18 at 16:55
  • 2
    $\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 May 19 at 2:26
43
$\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.

| improve this answer | |
$\endgroup$
  • $\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 May 17 at 22:05
  • $\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 May 18 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$ – Rasmus Damgaard Nielsen May 18 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 May 18 at 12:25
  • 1
    $\begingroup$ And if you are concerned about using proprietary software, don't forget about GNU Octave. $\endgroup$ – Theodore Tsirpanis May 18 at 16:04
15
$\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.
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks for your answer! $\endgroup$ – Blue May 17 at 22:05
  • $\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$ – Zizy Archer May 19 at 15:21
  • $\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$ – TheGreatCabbage May 19 at 18:05
  • 1
    $\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$ – Stephan Kolassa May 19 at 18:59
13
$\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 ;).

| improve this answer | |
$\endgroup$
  • $\begingroup$ I see, I never looked at it from that perspective. Thanks for the answer! $\endgroup$ – Blue May 18 at 14:10
11
$\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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks, I appreciate your advice! Will definitely stick with Python. $\endgroup$ – Blue May 18 at 14:12
  • 2
    $\begingroup$ What is a "B.C company"? $\endgroup$ – Peter Mortensen May 18 at 14:52
  • $\begingroup$ B.C. = Blue Chip. $\endgroup$ – Jason May 19 at 3:10
  • 5
    $\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 May 19 at 9:17
6
$\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).

| improve this answer | |
$\endgroup$
  • 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$ – Mark L. Stone May 19 at 22:05
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.

| improve this answer | |
$\endgroup$
3
$\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!

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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