In many of the classes that I teach, I require students to learn the basics of Mathematica which we use throughout the semester to do computations and to submit homeworks (in notebook form). Some students really like this and some... not so much.

Since I teach in an engineering department, almost everyone already knows some programming language: Matlab, python, java, or C are the most common, though there is quite a variety. One thing that I have found pretty effective is to try and relate Mathematica formalisms, structures, and ideas to those that students already know. For example:

$-$ When talking about using the Listable Attribute of functions, I compare this to Matlab's vectorization

$-$ When talking about alternatives for loops, Mathematica's Table function is analogous to python's List Comprehensions, for example, observe the similarity between

squares = [x**2 for x in range(10)]

and

squares = Table[x^2, {x, Range[10]}]

$-$ Mathematica's Notebook format is analogous to Jupyter notebooks which merge word processing, computation, and interactive presentations.

My question is this: What are some other analogies between Mathematica functions, expressions, and structures that might be helpful to new users in understanding "what Mathematica is thinking" or "why it works that way"?

Update: It seems that we have some very good answers for Matlab and for python. How about other languages? Any nice analogies for/with other popular languages?

  • 11
    It is the other way around: Jupyter notebooks are (still poor) analogs of Mathematica notebooks. – Anton Antonov Sep 8 at 15:47
  • 1
    @bills I was half-joking, referring to the historical time-line of notion and implementation of "computational notebook". You are referring to the exposure time-lines of individuals/students. – Anton Antonov Sep 8 at 16:00
  • 4
    @sunt05 some percentage of the students in my class feel the same way. But the class is not about learning Mathematica or learning python, but about image processing. One thing python lacks is the easy interactivity of Mathematica, which is really key when examining many different variations on parameters, filters, and processing methods. I'd be happy to see an answer from you with more python-esque analogies – bill s Sep 10 at 21:17
  • 8
    Perhaps analogies are more harmful than helpful beyond a certain point. They might encourage students to think in Python and try to translate their Python solution directly to Mathematica. That often results in a hideously complex and inefficient solution. An alternative approach would be to show how certain basic tasks are accomplished. Describe those basic tasks in English, not in Python. The tasks should be chosen based on what is taught in your course, and what mistakes student most commonly made in past years. – Szabolcs Sep 11 at 13:51
  • 1
    @Szabolcs -- it is certainly true that even the best intentioned methods can go awry. Introductory programming courses often stress For loops so much that students don't even realize there are alternatives. By pointing out that they may already know an alternative to the evil For (either via vectorization in Matlab or list completion in python) they may be able to avoid Fors unpleasant complexity. And yes... it is a constantly evolving landscape -- when I first started teaching courses in Mathematica, python was not part of the scene. – bill s Sep 11 at 14:59

Imho some important things to translate between Matlab and Mathematica:

  • "everything is a matrix (or inefficient)" vs. "everything is an expression"

  • indexing into arrays: : vs. All

  • indexing into arrays: j:i:k vs. j;;k;;i

  • constructing ranges: j:i:k vs. Range[j,k,i]

  • column-major vs. row-major!: mat(:) vs. Flatten[Transpose[mat]] or (mat')(:) vs. Flatten

  • combining tensors: cat vs. Join and ArrayFlatten

  • anonymous functions: @(x) x^2 vs. #^2& or x \[Function] x^2

  • building simple tensors: zeroes, ones vs. ConstantArray

  • more tensors: eye and speye vs. IdentityMatrix and IdentityMatrix[#,SparseArray]&

  • diag and spdiags vs. DiagonalMatrix and DiagonalMatrix[SparseArray[#]] & / SparseArray together with Band (but also diag vs. Diagonal btw.)

  • even more tensors: rand vs. RandomReal

  • loops: for vs. Do, Table, Array, and Map (and not For!!11eleven)

  • arrayfun and cellfun vs. Map (with level spec {-1}) (special thanks to mikado for pointing this one out)

  • while and repeat vs. While, but also NestWhile, NestWhileList, FixedPoint, and FixedPointList

  • if ... else ... end vs. If

  • if ... elseif... elseif... end vs. Which

  • piecewise vs., well, Piecewise

  • solving linear systems: \ and / vs. LinearSolve and LinearSolve[#1][#2, "T"] & (for details, see also this post)

  • more linear systems: pinv vs. LeastSquares and PseudoInverse

  • struct vs. Association

  • cell vs. List

Certainly less important

  • kron vs. KroneckerProduct

  • meshgrid vs. Tuples (Due to the intuitive plotting in Mathematica, Tuples within Mathematica has not nearly the same importance as meshgrid has within Matlab.)

  • class vs. tags (TagSet and TagSetDelayed) (though each Matlab programmer I've ever met refused to use classes...)

  • isa vs. Head and patterns

  • mex vs. Compile (and LibraryLink for the pro users)

  • One could also mention Piecewise in the context of If and Which... – kirma Sep 14 at 10:36
  • 1
    @kirma Good point. I added it, but compared it to piecewise rather than to if. ;) – Henrik Schumacher Sep 14 at 12:03
  • Is IdentityMatrix[@,SparseArray]& intended to be IdentityMatrix[#,SparseArray]&? Might also be worthwhile to point out DiagonalMatrix, though I don't recall if there's a clear Matlab analog. – eyorble Sep 14 at 12:31
  • @eyorble Good point. Corrected it. There are als diag and spdiags in Matlab. – Henrik Schumacher Sep 14 at 12:38
  • @HenrikSchumacher Ah. I've never used Matlab. ;) – kirma Sep 14 at 13:06

When a language, e.g., Python, not emphasizing but has to talk about "functional programming", usually it speaks about three functions: map, filter and reduce. I always think comparison a good approach to learn things, so below I share the comparison I made before.

enter image description here

enter image description here

enter image description here

Besides, Function (&) vs lambda, Array, Table vs "list comprehensions" (Table has been mentioned but Range[] is redundant.).

  • I think if you want to talk correspondence, the entirety of itertools is big. It provides a slew of utilities that Mathematica has, but does so in a more efficient lazy fashion. – b3m2a1 Sep 28 at 18:06
  • @b3m2a1 Why are they more efficient? – Αλέξανδρος Ζεγγ Sep 29 at 10:30

For those with experience in python, WRI has already provided a nice introductory tutorial along with many analogies.

However, for the class intended for image processing as mentioned by the OP, pure python is for certain not enough: numpy, pandas, scipy and pillow are some of the essential packages to go with.

  • As someone who has used pillow a fair amount, I do tend to think it is less-intuitive and certainly less-powerful than Mathematica. It is nice to know that it exists and get some sense for how to use it for when you lose that academic Mathematica license and the cost is prohibitive, but as they say, "use the right tool for the job" and for maybe the first time ever, the right tool is Mathematica. I do enjoy numpy, though, as a stand in for Mathematica arrays. It is often much more efficient. – b3m2a1 Sep 28 at 18:04
  • @b3m2a1 Actually I was really reluctant to write up this post days ago as I love Mathematica to death. However, I feel helpless now and then if I want to communicate my research outcome with others in Mathematica. As long as WRI stays with their current business model, it is hopeless to see new blood flowing into the Mathematica usership, not to mention discipline-specific python packages are moving really fast : I personally interact with atmospheric models a lot and feel Mathematica lacks essential tools to deal with those tools. – sunt05 Sep 28 at 18:36
  • I won't deny any of that. Modern research is done is python (and some R and MATLAB) and Mathematica has decidedly abdicated its once considerable position in that. My sole argument was about image processing with Mathematica vs. pillow. In that specific case, Mathematica still outperforms python in usability and performance. – b3m2a1 Sep 28 at 18:44

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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