3
$\begingroup$

Why does the code (Mathematica version 10.0.2.0, Linux x86-64):

Subscript[Subscript[S,i],j] Subscript[Subscript[S,i],j] // Simplify

output the expression:

$(S_i)^2_j$

Is this the expected output? If so, why? And how can I prevent Simplify from touching the expression above if this is the correct behaviour?

Edit 1:

To be clear, what I mean is this: the output of the expression looks "ambiguous" to me. If I want to interpret the subscript as an operation on S, then the simplified expression is not equivalent to the original one. Why doesn't Mathematica output:

$(S_{ij})^2$

instead? Can I make Mathematica output this instead?

Edit 2:

This questions is not about the Mathematica code I posted above because I want to interpret the subscripts as a particular operation, and in the code above I can convert to a form that is handled better in calculations than subscripts. What I want to know is why Mathematica outputs the code as $(S_i)^2_j$, when my suggestion of $(S_{ij})^2$ seems more logical and "safer" (in the sense that it preserves more of the structure of the original form).

Edit 3:

To better display my intent, I'll explain that I want to interpret the index as notation for an operation, $d_i$ acting on an object $S$. In Mathematica I write it as d[S,i], but since I may have up to 6 (and potentially more) $d$ operations on one object, I want to display this more succinctly as $S_i$. To this effect I execute:

expr //. d[S_, i_] -> Subscript[S,i]

when displaying my expressions. A nice side effect is that I can also enter expressions in this notation by converting back to the functional form before I do my calculations proper:

expr = displayexpr //. Subscript[S_, i_] -> d[S,i]
(* calculations on expr... *)

Now, my expressions are long and multitudinous, so when I simplified expressions like

Subscript[Subscript[S,i],j] Subscript[Subscript[S,i],j]

I was given the output:

$(S_i)^2_j$

which threw me since at first glance it seems to tell me I have expressions of the form:

Subscript[Subscript[S,i]^2, j]

I can understand what Mathematica is doing, I just wonder if it is the best way to display its expressions...

$\endgroup$
  • 1
    $\begingroup$ The FullForm of your output is Power[Subscript[Subscript[S, i], j], 2]. What would you expect the output to be? $\endgroup$ – Karsten 7. Dec 7 '15 at 22:47
  • $\begingroup$ @Bill: I do need to use the expression, so HoldForm might not be useful. $\endgroup$ – Zorawar Dec 7 '15 at 22:49
  • $\begingroup$ @Karsten7.: OK, I see what the expression is trying to say, but it reads like the j-th component of $S_i$ squared. I guess I want the expression to come out as $(S_{ij})^2$. I'll edit the question to make that more explicit. $\endgroup$ – Zorawar Dec 7 '15 at 22:51
  • 4
    $\begingroup$ Since no one has mentioned it yet: it is highly recommended not to use Subscripts for calculations purposes; only use Subscript for display purposes. They can be used for calculation purposes, but there's some strange subtleties arising with their use that even somewhat experienced users can't predict. It is recommended to use things like S[i,j] instead. In that case, your problem with be resolved automatically, and if you want, you can choose to display quantities like S[i,j] as subscripted symbols later. $\endgroup$ – march Dec 7 '15 at 23:11
  • 1
    $\begingroup$ @march: I have heard this already, yes :) I wasn't planning on using the subscripts in calculations, only to clean up the expressions when looking at them. (I change subscripts to functions before I do calculations). I guess you've answered my question: Simplify should be considered a "calculation". I'll do the Simplify on the unadulterated expressions and then change to subscripts. Thanks! $\endgroup$ – Zorawar Dec 7 '15 at 23:16
2
$\begingroup$

Notation and preliminary remarks

The form of the outputs depends on the objects to which Subscript is applied. In the situation at stake, we should differentiate between the cases

  • $S_{ij}$, Subscript[S, i j]
  • $(S_i)_j$, Subscript[Subscript[S, i], j]
  • $S_{i_j}$, Subscript[S, Subscript[i, j]]

which display respectively as

enter image description here

We can see that the line on which the index $j$ is written differs for each output.


About the question

  1. First part, and edits 1 and 2 of the question

Considering the square of the above expressions,

Subscript[S, i j] Subscript[S, i j]
Subscript[Subscript[S, i], j] Subscript[Subscript[S, i], j]
Subscript[S, Subscript[i, j]] Subscript[S, Subscript[i, j]]

we have the displays

enter image description here

With the introduced notation, the form $(S_{ij})^2$ mentioned in the question (see Edit 1 and Edit 2) is related to the expression Subscript[S, i j] Subscript[S, i j] and not to

Subscript[Subscript[S, i], j] Subscript[Subscript[S, i], j]

The latter is related instead to the form $((S_i)_j)^2$ and the associated output (see screenshot above) can then be considered as the expected output.

  1. Edit 3 of the question

I can understand what Mathematica is doing, I just wonder if it is the best way to display its expressions

I think it amounts to how one would define "best way". It may be only a matter of taste in the end. Here the set of rules used for the display of subscripts is complicated, but (a) it is self-consistent when several subscripts and indices are used in different orders and (b) it reduces the number of parentheses used to avoid having a too cumbersome notation (see next section below).

This being said, if you prefer you can define your own set of display rules by using Format as suggested by march in his answer, or by using MakeBoxes.

Considering for instance the example taken from your comment,

I think for Subscript[Subscript[S, i]^2, j] they should display $(S^2_i)_j$

you could use

Format[Subscript[Subscript[S_, i_]^2, j_]] := 
   DisplayForm@SubscriptBox[ RowBox[ {"(", SubscriptBox[S, i]^2, ")"} ], j ]

to get $(S^2_i)_j$ when evaluating Subscript[Subscript[S_, i_]^2, j_].


Further comments

Why do we have one set of parentheses for the output of $((S_i)_j)^2$? We may think of having instead (a) no parentheses, or (b) two sets.

(a) We saw at the beginning that the position of the index $j$ depends on the object to which Subscript applies, so we may be able in principle to infer the form of the input by looking at the position of the index.

Parentheses are used here to avoid ambiguities with another case for which the down position of the index $j$ is the same,

Subscript[Subscript[S, i], j] Subscript[Subscript[S, i], j]
Subscript[Subscript[S, i] Subscript[S, i], j]

enter image description here

(b) The idea of not having several sets of parentheses is to avoid the notation being too cumbersome I think, so by default only one set of parentheses is displayed.

We may onsider for instance the square of an expression with three indices, e.g. Subscript[Subscript[Subscript[S, i], j], k], for which we have only one set.

The rule seems to be the following. When a power applies to a Subscript expression, the first argument of this expression is enclosed between parentheses, unless it does not contain itself an indice, in which case the parentheses are removed.

For instance the two last expressions above can be rewritten

Subscript[Subscript[S, i], j]^2
Subscript[Subscript[S, i]^2, j]

The first will display a set of parentheses around Subscript[S, i], and the second around S. Since S is not an expression with indices, the parentheses are removed. This is what we see in the last screenshot, and it can be checked that this is what happens in the previous one.

$\endgroup$
  • $\begingroup$ I think this is indeed the logic being used. However, to the designers I would make the point that if they happy to leave the position of the j index as discriminating the three cases above, even when it is subtle, they should do the same when it is squared: letting the j index alone discriminate between them. The brackets on $(S_i)^2_j$ makes it seem something else is being displayed -- namely that the square is acting first and then the index (i.e. $((S_i)^2)_j$. I think the departure from consistency is what threw me. But maybe that's just me :) $\endgroup$ – Zorawar Dec 8 '15 at 16:36
  • $\begingroup$ I think for Subscript[Subscript[S, i]^2, j] they should display $(S_i^2)_j$ and for Subscript[Subscript[S^2, i], j] they should display $(S^2)_{ij}$ (but with the j lower down than the i). But, again, maybe that's just me :) $\endgroup$ – Zorawar Dec 8 '15 at 17:02
1
$\begingroup$

Perhaps you want

Subscript[S, i, j] Subscript[S, i, j]

form

or

Subscript[S, i j] Subscript[S, i j]

form2

$\endgroup$
  • $\begingroup$ Thanks for the suggestion (in particular, I didn't know you could do the second form!), but I'd like to keep the two subscript operations distinct so that I can convert the subscripts to functions and then do calculations on objects that are not subscripts :) I'll make this clearer in the question... $\endgroup$ – Zorawar Dec 8 '15 at 0:34
  • 1
    $\begingroup$ @Zorawar. To keep the subscripts distinct for computation purposes. you want the 1st form. The 2nd form is only useful formatting symbolic output. The subscripts are interpreted as i*j in computation. $\endgroup$ – m_goldberg Dec 8 '15 at 0:53
  • $\begingroup$ @Zorawar One can create an output similar to the 2nd form, but without a multiplication of subscripts, using Subscript[S, Row[{i, j}]]. $\endgroup$ – Karsten 7. Dec 8 '15 at 0:59
  • $\begingroup$ @Karsten7. Good point, but again not useful for computation. $\endgroup$ – m_goldberg Dec 8 '15 at 0:59
  • $\begingroup$ But always convertible to a form more suitable for computation, whereas Subscript[S, i j] can't be converted after evaluation if i and j have for example a numerical value. $\endgroup$ – Karsten 7. Dec 8 '15 at 1:06
1
$\begingroup$

My suggestion is to use quantities like S[i, j] for calculations (including applying Simplify) and use Subscripts only for display purposes. If you have a simplified expression expr involving quantities like S[i, j] that you would like to format nicely, do

expr /. A_[i_, j_] :> Subscript[A, i j]

at the end of the calculation. (This particular choice of Subscript format was suggested by m_goldberg in the answer above.)

Note that you can also choose to format everything with the Head S as a subscript, so that it will always display as a subscripted symbol, but under the hood, it will still be S[i, j]. We can do this as follows:

Format[S[a__]] := DisplayForm@SubscriptBox["S", StringJoin @@ ToString /@ {a}]

Update in response to Edit 3 of the question

Edit 3 of the question suggests to me that Formatting is the correct way to go. Since in Edit 3, a d notation is used, I suggest using:

Format[d[A_, j__]] := DisplayForm@SubscriptBox[ToString@A, StringJoin @@ ToString /@ {j}]

I didn't note this before, but if you execute this before any calculations, anything of the form d[A_, j__] will display as a subscripted symbol.

$\endgroup$
  • $\begingroup$ Actually, interestingly, simplifying my expressions before converting to index form (see my edit no. 3) gives exactly the same expression as if I simplified after converting to index form. That is, for my purposes, when I simplify makes no difference. (Of course, other operations may be not save to do after converting to index form.) It seems I just have to get used to the way Mathematica decides to display its expressions. $\endgroup$ – Zorawar Dec 8 '15 at 17:12
  • $\begingroup$ @Zorawar. Your Edit 3 suggests to me that you shouldn't go back and forth between the different forms. I suggest you use Formatting instead, as in my answer. Since you use this d notation, I have updated the post with that formatting choice. $\endgroup$ – march Dec 8 '15 at 17:22
  • $\begingroup$ I don't go back and forth during any intermediate steps. That would be asking for trouble! Only ever at the first step of entering inputs, and the last step of display outputs. But, the Format option might be the right way to go. Thanks. $\endgroup$ – Zorawar Dec 8 '15 at 18:16
  • $\begingroup$ @Xavier. Yes, thanks for heads-up! Fixed. $\endgroup$ – march Dec 19 '15 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.