# Subscripted (or superscripted) variables in Mathematica

When I first learned of Mathematica and started to use it, I soon discovered that Mathematica supported subscripted and superscripted variables such as $M_{i j}$. Naively, I first thought that I could refer to a matrix element merely by specifying it's row and column indices such as $me_{1 2}$ or whatever for a matrix named $me$.

But, that is not the case, the notation for such elements is using the Part function or the $[[row]][[col]]$ notation.

Further, as I started a self-study of General Relativity, I thought about using scripted (sub or super) variables in Mathematica to manipulate tensor equations. Not so easy and I thought it would be automatic. I have since downloaded a few tensor calculus packages and find that they did not make use of subscripts or superscripts.

Therefore, since what I few are the obvious uses of subscripting and superscripting variables in Mathematica are not being used, just why is this notation supported. Or, maybe I am missing something and there is a big area of usage I am not aware of.

I do admit that among the ilk of this forum I must consider myself still a beginner as a user of Mathematica.

• "just why is this notation supported" - mostly pretty-printing. For actual computational work, they are avoided as much as possible. – J. M. is away Jul 8 '16 at 16:52
• See also the Mathematica pitfalls question, bullet 3. – Sjoerd C. de Vries Jul 8 '16 at 21:22
• [[row]][[column]] can be written more succinctly as [[row, column]]. As far as displaying subscripts and superscripts look at Format – Bob Hanlon Jul 8 '16 at 21:33
• @BobHanlon Thanks for taking the time to comment on my question. I know of the features you mention but that is not pertinent to my question which was whether there were useful applications of subscripted (or superscripted) variables. I didn't necessarily want to "display" subscripts or superscripts. – K7PEH Jul 9 '16 at 0:43
• ……So, what do you want? If you want Subscript to behave like Part, just define Subscript[m_, i__] := Part[m, i], then it works as expected e.g. M = RandomReal[1, {4, 4}]; Subscript[M, 2, 3]. I'm not familiar with general relativity, but I think what you need to do is similar. – xzczd Jul 9 '16 at 15:01

Naively, I first thought that I could refer to a matrix element merely by specifying it's row and column indices such as $me_{12}$ or whatever for a matrix named $me$.

In fact you can do just that using the subcript form of Part.

SeedRandom[123];
me = RandomInteger[{1, 50}, {3, 3}];
MatrixForm@me


First note that you don't need me[[row]][[col]] as you can specify further dimensions in one Part call. me[[row, col]].

me[[2, 2]]

26


Next note that there is a subscript form for Part. Type the following:

me Ctrl+- Esc [[ Esc 2 , 2 Esc ]] Esc Ctrl+Space

Esc [[ Esc and Esc ]] Esc product special characters for Part. That makes your code easier to read.

Hope this helps.

As of Version 10.0, you can use Indexed.

For example, you could use

Indexed[g, {μ, ν}]


to generically denote the metric tensor. Then, if g has explicitly been set to an array,

g = {{-1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}


then Indexed will extract the appropriate Part:

Indexed[g, {1,1}]

-1


It is too bad that since Indexed is deeply tied to Part, you cannot use the computer scientist and/or general relativist's convention of starting indices from 0.

• "It is too bad that since Indexed is deeply tied to Part, you cannot use the computer scientist and/or general relativist's convention of starting indices from 0." You can modify every build in function as you please: Unprotect[Indexed]; Indexed[g_, arg_] := Extract[g, arg + ConstantArray[1, Length@arg]]; This should work with an index convention starting at 0. – N0va Sep 6 '16 at 12:21

These notations exist to be defined by you (the user) and therefore making your life easier (or harder, depending on the excess of usage). Consider reading: Operators without Built‐in Meanings

The only important thing to note is that when you are working with sub- or superscripted variables, the actual assignment is made to the corresponding function. (Not to the symbol being scripted.)

So when you assign something to ai, the actual assignment is made to Subscript[a, i]. That's why syntax highlighting won't indicate your symbol a as defined. (And this is why I recommend not to use sub or superscripted variables.)

Aside from that you can add multiple definitions to achieve taking part of a List and using partial derivative at the same time:

Subscript[a_List, i__] := Part[a, i]
Subscript[a_, i_] := D[a, i]


Note: you can always use FullForm to find out the actual name of the underlying symbol (for example FullForm[a*] yields SuperStar[a], and Definition to see existing definitions.

I use subscripted variables extensively in analysis of summations etc (e.g. derivation of Cramer-Rao lower bounds for cases with large numbers of variables). As a very simple example, find the value of m that minimises the following sum.

sum = Sum[(Subscript[x, i] - m)^2, {i, 1, n}]
eqn1 = D[sum, m] == 0
eqn2 = eqn1 /. u_Sum :> (u /. Subscript[x, i] -> 0) + (u /. m -> 0)
sol = Solve[eqn2, m]


Unfortunately Mathematica is rather reluctant to make obvious simplifications (e.g. cancelling factors inside and outside the summation) so I have had to employ extensive sets of rules that I've developed myself.

• Could you elaborate on your motivation for using subscripts here, is there an advantage or is it mostly cosmetic? – dionys Jul 9 '16 at 15:02