What's the difference between variables with and without a subscript?

If I input $x = a + b$, the $x$, $a$ and $b$ are blue. When I hit Shif+Enter, the $x$ turns black, indicating the kernel now knows this variable. But when I input $x_r = a + b$, and hit Shift+Enter, $x_r$ is still blue.

What's the difference between variables with and without subscript?

• Try wrapping AtomQ and Head around your variables to see the difference. This might be useful: mathematica.stackexchange.com/questions/38622/… May 20, 2014 at 6:08
• $x_r$ is Subscript[x], so your definition is actually more about defining Subscript than about x. May 20, 2014 at 6:13
• Evaluate ?? Subscript to see what Mathematica has done with your definition. It may also suggest to you why subscripted variables are fine for text formatting, but not so good for computation. May 20, 2014 at 10:42

On the simplest level, the difference is that x is a symbol, which is an atomic object to a Mathematica kernel, while subscripting x makes it an non-atomic expression, in this case an object with head Subscript.

 Clear[x]

 {Symbol, True}


The FullForm of a subscripted variable with subscript i is

Subscript[x, i]


Its head is clearly Subscript and it's not an atom.

 Clear[x, i]; AtomQ[Subscript[x, i]]

False


On a deeper level, there is a difference in how assignments to symbols and subscripted objects are handled. In both cases, an expression of the form

{HoldPattern[...] :> a + b}


is recorded by the kernel. However, they are stored in different kinds of internal lists. An assignment to a symbol is stored as an own-value of the symbol; All assignments to subscripted variables are stored as down-values of Subscript.

Clear[x]; x = a + b; OwnValues @ x

{HoldPattern[x] :> a + b}

Clear[Subscript, x, i]; Subscript[x, i] = a + b; DownValues @ Subscript

{HoldPattern[Subscript[x, i]] :> a + b}


If you were use many subscripted variables in a Mathematica session, you would build up a big list of rules in the down-values of Subscript, slowing down references to subscripted variables. But this is probably not the worst problem encountered with Subscript. Consider doing a long computation with subscripted variables. Somewhere in the notebook, you evaluate

Subscript[x, i] = a + b;


Now, much later later and in a distant cell, you want to take a symbolic derivative. Like so:

Clear[x, i]; D[Subscript[x, i][t], {t, 2}]

Derivative[2][a + b][t]


Not what you expected? It is so easy to forget that it is Subscript that must be cleared.

Clear[Subscript]; D[Subscript[x, i][t], {t, 2}]


Derivative[2][Subscript[x, i]][t]

which will be rendered as $\tt{x_i''[t]}$. Looks good now, but how many other subscripted variables have you cleared along with $\tt{x_i}$?

• However, D[Subscript[c, x]*x, x] returns Subscript[c, x] + x*Derivative[0, 1][Subscript][c, x] instead of simply $c_x$, it being a coefficient, as D treats Subscript as a symbolic function. Any idea how to overcome this? Nov 30, 2015 at 14:03