Mathematica

April 6, 2011 — September 9, 2024

algebra
compsci
computers are awful
stringology
Figure 1

A computational symbolic algebra system.

1 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.

1.1 Pros

  • Built-in latex editor
  • Magickal calculus engine. It gives me all the integrals I can eat…

1.2 Cons

  • … but at great cost to my soul.
  • In software engineering terms, it’s a weird language, with horrible default scoping (Cross-document namespace pollution? Really?)
  • Even as probably the most popular computer algebra system, it is not that popular. Ergo, weak community.

2 Tips

Mathematical is the only application I know which does not save things automatically, or, better, recover lost work after a crash.

There is an autosave options NotebookAutoSave, which we set from the options inspector in the Format menu. 🤪 Wolfram Support Quick Answers: How do I enable autosaving in a Mathematica notebook?.

2.1 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.

3 Substitution

The substitution operator is /. which is terrible to search for. Search under its alias, ReplaceAll (also ReplaceRepeated///.).

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

There are some technical details that can be useful.

4 Higher order functions

Nifty tricks like postfix application etc are… beyond the scope of my current project but I found the keywords eventually. Those keywords are Postfix, Sequence, Apply, and maybe Right Composition and infix notation. It is worth looking at the language syntax to find stuff like that out organically.

5 Comments

Comment syntax:

(* stuff *)

This is not well documented presumably because they because prefer us to use the literate programming system, where some cells can be text cells; That is fine, but sometime I need inline comments.

6 Typing symbols

Typing symbols look confusing from the helpfiles (How do you type ⊗?). Inside the app one uses the combination of Esc and autocomplete.

7 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 we should not rely upon.

8 Non-commutative algebra

Generic non-commutative operator algebra is not easy to find in basic Mathematica, and even the more specialised case of matrices is not great.

The add-on, NCAlgebra (source) is a powerful non-commutative algebra system (Camino et al. 2003; de Oliveira 2012; Mora 1994). TBH it is the main thing I use Mathematica for.

NCAlgebra including generic treatment of matrix derivatives, generic matrix decompositions, matrix polynomials and rationals, semi-definite programming and other useful stuff.

Documentation now seems to be at ncalgebra.github.io.

8.1 NC installation

As of version 6.0.x the “Paclet” isntall works:

PacletInstall["https://github.com/NCAlgebra/NC/blob/master/NCAlgebra-6.0.3.paclet?raw=true"];

Previously there was

git clone https://github.com/NCAlgebra/NC

The latter might be worth doing if one is hacking NCAlgebra itself?

8.2 NC Tips

The NC documentation does not AFAICT mention that it implements its own trace operator called tr (as opp to Tr, the mainstream one, which confuses NCAlgebra).

It ALSO introduces its own transpose operator tp, which is documented, but they don’t mention that it displays as tr (which if I typed it in would give me trace), which leads to pointless confusion.

NCReplace substitutes ./. Usually I want NCReplaceRepeated.

The command to collect terms is NCCollect or NCStrongCollect. Usually what I actually want is NCCollect[Something, {x}, ByTotalDegree -> True]. NB: NCCollect* acts independently upon a variable and its transpose, so often I need to collect on, say, {x, tp[x]}.

8.3 Other non commutative algebra packages

See carlwoll/TensorSimplify: TensorSimplify simplifies tensors involving Dot, Tr and the IdentityTensor which looks handy but has not been updated in a while and has perfunctory documentation.

Mathematica has Symbolic Tensor functionality; It is not very popular online, possibly because of being verbose? Or new-ish?

For more differential geometry stuff see Ricci: A Mathematica package for doing tensor calculations in differential geometry.

9 Scope

Most tutorials execute everything in promiscuous global scope, which is fine until it is not. Since I am not a heavy Mathematica user, half my time is spent debugging problems with stale definitions and weird scope behaviour leading to namespace collisions (shadowing in Mathematica parlance).

We can get a local scope with Block which helps.

Multiple clashing function definitions will hang around silently conflicting with one another; 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.

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

ClearAll["Global`*"]

Nuclear option:

Quit[]

10 Evaluation

I’m still deeply confused about when stuff is lazily or eagerly evaluated, especially when nested. This is notoriously even more complicated in worksheet style interfaces (shared with, e.g. jupyter), where we introduce the additional confounder that it is not even clear which code has been invoked, let alone executed.

Keywords to understand are Hold, and Delayed.

11 Function wrangling and differential equations

The confusing terminology 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?.

12 Building libraries/packages

🤷 But see, for example, how a non-trivial app like NCAlgebra works.

13 Other useful stuff

Here are some other references that I have found useful.

14 References

Camino, Helton, Skelton, et al. 2003. “Matrix Inequalities: A Symbolic Procedure to Determine Convexity Automatically.” Integral Equation and Operator Theory.
de Oliveira. 2012. Simplification of Symbolic Polynomials on Non-Commutative Variables.” Linear Algebra and Its Applications.
Mora. 1994. An Introduction to Commutative and Noncommutative Gröbner Bases.” Theoretical Computer Science.