# Mathematica

April 6, 2011 — April 5, 2023

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

- Magickal calculus engine. It gives me all the integrals I can eat, but at great cost to my soul.
- Built-in latex editor

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

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

Pro tip: use NCAlgebra to avoid these problems and various other related to Mathematica’s crappy matrix typing.

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

### 8.1 NC installation

Their recommended install is

`Import["https://raw.githubusercontent.com/NCAlgebra/NC/master/NCExtras/NCWebInstall.m", CharacterEncoding -> "UTF8"];`

That does not work for me for NCAlgebra v5.0.x on recent Mathematica, e.g. 13.2.1. This does:

As of version 6.0.x this works and is now my recommended method.

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`

).

### 8.2 NC Tips

`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 have us executing 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:

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 confoudner 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 Links

Here are some links 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.