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

  • Do you have materials from your courses that you could make available? Much obliged. – Rabbit Sep 8 at 15:20
  • 6
    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
  • 2
    @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
  • 3
    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

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)

  • 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"] &

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

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.

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.