Memory in Mathematica

Linear Algebra With Applications, Math 134, Fall 2024, Carleton College, Joshua R. Davis, jdavis@carleton.edu

Introduction

This tutorial presents a simple and powerful tool, that can greatly expedite your use of Mathematica. But this tool is also the cause of many, many user errors. So, if you plan to do anything complicated in Mathematica, then it is worth your time to read this tutorial carefully.

Basics

The tool is simply this: Whenever Mathematica computes something for you, you can store the answer for future use, under a name of your choice. To illustrate, let’s return to the vector examples from the previous tutorial.

{1, 2, -1} + {x, 1, 3}
{1, 2, -1}.{x, 1, 3}
Cross[{1, 2, -1}, {x, 1, 3}]

These three calculations all use the same two vectors. It’s tedious and error-prone to retype them. We’d rather just give them names, such as v and w, and then use those names like this.

v + w
v.w
Cross[v, w]

When you execute the code cell above, not much happens, because Mathematica doesn’t know what v and w are. It signals this fact by showing those names in blue. Now execute the following cell.

v = {1, 2, -1}
w = {x, 1, 3}

What happens? Mathematica loads the two vectors into its memory with the names v and w. Now it knows what v and w are, so it colors those names black, in all code cells throughout the notebook. Let’s try evaluating the sum, dot product, and cross product of v and w again.

v + w
v.w
Cross[v, w]

Notice that x is still in blue, because Mathematica doesn’t know what x is. Mathematica manipulates x as a variable, doing as much algebra as it can without assuming any particular value for x. The qualitative difference between x and v is very apparent in this next example. (By the way, can you figure out what Mathematica does for the v version? It’s not “standard math”.)

Expand[(v + 3)^2]
Expand[(x + 3)^2]

So what if you want to do algebra with a variable v, but you’ve assigned v a value, so Mathematica refuses to do algebra with it? Then you need to clear the value from v, like this.

Clear[v]
Expand[(v + 3)^2]
Expand[(x + 3)^2]

Choosing names

You have great leeway in how you name your quantities stored in memory. Names can be just about any combination of upper- and lower-case letters and digits. However, there are two rules that you must observe.

First, you can’t start a name with a digit, as this example shows.

secondGraders = 126
2ndGraders = 126

Second, you should never start a name with an upper-case letter. The reason is that all built-in Mathematica names (Pi, Sin, E, Log, Plot, etc.) start with upper-case letters, and it’s a good idea to distinguish your names from the names of built-in quantities. If you try to reuse a name that Mathematica has built-in, then Mathematica actually refuses.

N = 34
N
n = 34
n

In the following example, Mathematica lets you use the name M, but I’m telling you that it really isn’t a good idea to begin your names with upper-case letters, so don’t!

M = 36
M
m = 36
m

Notebook vs. memory

Now we explore how storing values in memory can cause confusion to you, the user. First, evaluate the following code cell.

x = 13

Then evaluate this second code cell.

x = 20

Then go back and evaluate the first code cell, followed by this next code cell. What value of x is reported?

x

The lesson here is that a notebook does not necessarily reflect what’s in Mathematica’s memory. The value of x is not the last value listed (spatially) in the notebook, but rather the last value that was assigned (temporally) from the notebook.

When Mathematica starts up, its memory is clear. No names are storing any values, except for the built-in names with their built-in values. To see what I mean, quit Mathematica and reopen this notebook, right now. When Mathematica starts up again, you will find that all of the names we’ve been using (m, v, etc.) are in blue. If you want to use the values that you set up before quitting, you have to set all of those values again.

For now, just activate this next cell to set a value for t. Then close this notebook without quitting Mathematica, and reopen the notebook. You will find that the name t still has a value, but the other names (m, v, etc.) still don’t.

t = (13 + r)^2

Now let’s hit the same idea from another direction. Keeping this notebook open, go to the File menu and open a new notebook. In that notebook, type “r = -2” (without the quotation marks) and hit Shift-Return or Enter to execute that code. Then come back to this notebook. You will see that r is black in both notebooks. And evaluating this next cell produces a different answer than it did earlier, because r is no longer unknown.

t = (13 + r)^2

Conclusion

The moral of the story is: Mathematica has a memory, consisting of names paired with values. You can set values in this memory by issuing commands from a notebook. If you have multiple notebooks open, then they all interact with the same memory. If you close a notebook, but don’t quit Mathematica, then the memory is still there. But quitting Mathematica clears all of the memory. So:

* If Mathematica isn’t behaving as you expect, make sure that the names are set up as you think they are. Make sure that unknowns don’t accidentally have values stored in them, and Clear them if they do. Make sure that names that should be storing values really are storing them, rather than being unknown. And make sure that the values are what they’re supposed to be.

* Make sure to save/print/write any important results from your Mathematica work, before you quit the program or log out of the computer.