# Mathematica

April 6, 2011 — September 9, 2024

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

## 3 Substitution

The substitution operator is `/.`

which is terrible to search for. Search under its alias, `ReplaceAll`

(also `ReplaceRepeated`

/`//.`

).

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.

## 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:

Previously there was

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

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:

Nuclear option:

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

- James J. Kelly’s Essential Mathematica for Students of Science

## 14 References

*Integral Equation and Operator Theory*.

*Linear Algebra and Its Applications*.

*Theoretical Computer Science*.

## 5 Comments

Comment syntax:

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.