Introduction to Mathematica: Understanding the Kernel

The kernel is Mathematica’s computational engine. It runs in the background, and only executes a Mathematica™ command when instructed to do so. Generally, a command for the kernel is contained in an input cell, and is sent to the kernel using a <Shift><Enter> keystroke, or by pressing the <Enter> key on the numeric keypad (with or without <Shift>). There are some implications of this separation of the kernel from the interface that are typically not obvious to the first-time user of Mathematica™, but which are critical to understand to be successful at using the package.

Watch the example to the right. In the line marked In, notice that 1+1, followed by <Shift><Enter> sends that calculation to the kernel, which then returns, in Out the result of that calculation.

In In, we send to the kernel “a”, which is in blue (meaning that it is an undefined symbol). The kernel doesn’t know what to do with it, so it simply sends back “a”. In In, we assign the value of 1 to a, which turns black, meaning that it is now a symbol that has an assigned value. Out gives us the result of that calculation, which is 1. And in In we repeat our request of the kernel to tell us what a is, and it now tells us 1. But notice that Out has not changed.

It is critical here to recognize that the equals sign, =, is not the passive statement that the left- and right-hand sides happen to be equal. It is a “Set” command, meaning “set the value of the symbol on the left to be the result of the calculation of the expression on the right.” So we can employ a second Set command, in In to change the value of a. This does not affect any prior calculation, but it is remembered by the kernel for any later calculations. If you ever need to unset a variable, you can use Set with a period on the right-hand side, as in In.

Now look at the “b” sequence. In particular notice that below Out, I entered b=0, but did not press <Shift><Enter>. This means that even though this statement that b=0 is in the document, it has not been sent to the kernel, and so b has not in fact been set, as is shown in Out. Going back and executing the b=0 line out of order (In) does send it to the kernel, and so querying the kernel about b afterward does give us 0.

Generally, you will only have one kernel running on your computer at a time. This means that if you have multiple Mathematica™ notebooks open, and you send commands to the kernel from each of them, that same kernel is executing all of those commands. So if you send a=1 to the kernel from one notebook, and then do some kind of calculation with a in another notebook, the kernel will still know that a=1. What matters from a computational perspective is what commands are sent to the kernel and in what order, not which notebook different commands are in.

You can put multiple commands to send to the kernel in a single input cell. These will be executed sequentially, with their resulting output cells appearing in order below that single input cell. If you wish to suppress the output of a particular command for some reason, you can do so by appending a semicolon to the end of that line.

There are many cases where your kernel will get loaded up with definitions that you want to get rid of. Maybe you have been debugging some Mathematica™ code, and need to get rid of the old, buggy definitions. Maybe you are starting a new calculation. In those cases, you may want to quit the kernel and restart it. You can do that through the menu (Evaluation/Quit Kernel/Local followed by Evaluation/Start Kernel/Local) or by sending the Quit[] command to the kernel from a notebook.