Mathematica



A computer symbolic algebra system.

Basics

I’m all about open-source tools, as a rule. Mathematica is not that. But the fact remains that the best table of integrals that exists is Mathematica, that emergent epiphenomenon of the cellular automaton that implements Stephen Wolfram’s mind. I should probably work out what else it does, while I have their seductively cheap student-license edition chugging away. begrudging contested access to a small number of corporate licenses.

Pros

  • Magickal calculus engine. It gives you all the integrals you can eat, but at great cost to your soul.
  • Built-in latex editor

Cons

  • It’s a weird language, with horrible default scoping (Cross-document namespace pollution? Really?)
  • Even as probably the most popular computer algebra system, just not that popular. Ergo, weak community.

Where on earth is the download button?

One of the quirks of Mathematica is that there is no way, at least that I can find, to go from the wolfram.com front page to the place where I actually download the software. account.wolfram.com is where the licensed copy lives.

Gotchas

See the master list: What are the most common pitfalls awaiting new users?.

One that has bitten me:

  • {{1,2,4}} is a \(1 \times 3\) matrix or a row vector;
  • {{1},{2},{4}} is a \(3 \mathrm{x} 1\) matrix or a column vector;
  • {1,2,4} is a vector but not a matrix. Whether it is a row or column vector depends on the context in a confusing and opaque way that you should not rely upon.

Substitution

The substitution operator is /. which is terrible to search for. The help easiest to find under the alias ReplaceAll.

{x, x^2, y, z} /. x -> 1
{x, x^2, y, z} /. {x -> 1,y ->2, z->x}

Pipelines

The keywords here are Postfix, Sequence, Apply, and maybe Right Composition and infix notation. It is worth looking at the language syntax to find these handy tricks.

Comments

Comments syntax:

(* stuff *)

This is not obvious because they have a literate programming thing going no where some cells can be text cells; which is fine but sometime I need inline comments.

Typing symbols

Typing symbols is easy; just use the combination of Esc and autocomplete.

Non-commutative algebra

Generic operator algebra is not well-done in basic Mathematica, and even the more specialised case of matrices is not great. You can’t mkae them have, e.g. unknown size AFAICT. However, you can do alright by invoking NCAlgebra (source) which is a powerful non-commutative algebra system, including generic treatment of matrix derivatives, generic matrix decompositions, matrix polynomials, semi-definite programming and other useful stuff.

Scope

Most tutorials have you executing everything in promiscuous global scope. Since I am not a heavy Mathematica user, half my time is spent debugging problems with stale definitions and weird scope behaviour. A large part of what remains is worrying about when code is executed. You can get a local scope with Block.

Multiple clashing function definitions will hang around silently conflicting with one another; use ClearAll to remove all the definitions of a term to avoid this. As David Reiss points out, if I define

g[x_]:=x^2
g[2]:="cheese"

then when I execute g[2] I get "cheese" and not 4. This is also about evaluation time — for more on that see below.

The easiest way to get a fresh start for some overloaded name, as far as I can see, is:

ClearAll["Global`*"]

Function wrangling and differential equations

The confusing terminology here is pure functions.

dsaad[t_] = Q[t] /. First @ DSolve[{Q''[t] + 40 Q'[t] + 625 Q[t] == 100*Cos[10*t],
  Q[0] == 0, Q'[0] == 0}, Q, t]

Here are some worked examples of the nuts and bolts of this: Function output from DSolve, How to define a function based on the output of DSolve?.

Building libraries/packages

🤷 But see, for example, the NCAlgebra package docs for some pitfalls.


No comments yet. Why not leave one?

GitHub-flavored Markdown & a sane subset of HTML is supported.