# Mathematica

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

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.

## Substitution

The substitution operator is /. which is terrible to search for. Earch under its alias, ReplaceAll.

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

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

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.

## Typing symbols

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

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

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

Their recommended install is

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

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

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

As of version 6.0.x this works:

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

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

See also 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.

## 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, the notorious problem of worksheet style interfaces (shared with, e.g. jupyter). We 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*"]

## Evaluation

Keywords to understand are Hold, and Delayed.

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

## Building libraries/packages

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

## References

Camino, Juan F., John William Helton, Robert E. Skelton, and Jieping Ye. 2003. “Matrix Inequalities: A Symbolic Procedure to Determine Convexity Automatically.” Integral Equation and Operator Theory 46 (4): 399–454.
Mora, Teo. 1994. Theoretical Computer Science 134 (1): 131–73.
Oliveira, Maurício C. de. 2012. Linear Algebra and Its Applications 437 (7): 1734–48.

### No comments yet. Why not leave one?

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