0
$\begingroup$

In Mathematica, I attempted to construct an arbitrary vector $\vec{a}$ using the Subscript:

a = {Subscript[a, x], Subscript[a, y], Subscript[a, z]}

However, Mathematica returns with the following message:

Hold[{Subscript[a, x],Subscript[a, y], Subscript[a, z]}]

All I would like to do is to perform vector operations with components of a vector denoted by subscript index. This comes across as very odd to me given subscripts are non-protected symbols.

$\endgroup$
6
  • 1
    $\begingroup$ It is not really about the Subscript but about the fact that you set a value of a which depends of a, like: a = {a, 1}. Try to rename it: vector = {Subscript[b, 1], 1} $\endgroup$
    – Kuba
    Commented Mar 9, 2017 at 13:14
  • $\begingroup$ But a isn't the same as Subscript[a,x] so why does Mathematica sees it this way-and hence, arriving at a conflict? $\endgroup$
    – Physkid
    Commented Mar 9, 2017 at 13:16
  • 1
    $\begingroup$ " 'a' isn't the same as 'Subscript[a,x]' " - but it is the same a here and there, in Subscript and on the right hand side of =. By the way, Subscript is not special so any head will give the same problem, like in the example from the first comment. $\endgroup$
    – Kuba
    Commented Mar 9, 2017 at 13:17
  • 1
    $\begingroup$ You will save yourself a lot of hassle in the future if you heed the words of item 3 in this post regarding subscripts: mathematica.stackexchange.com/a/18395/1783 $\endgroup$
    – bill s
    Commented Mar 9, 2017 at 13:39
  • $\begingroup$ The message actually returned is "$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of Subscript[a, x]." As Kuba is pointing out, the definition of a as a vector is being recursively substituted for a in the expression Subscript[a, x]. This would result in an infinite loop if it were not for the internal limit $RecursionLimit. Note also, that if x has a value, it would be substituted as well; similarly for y and z. Look up the Notation package in the docs or on this site, if you wish to use such notation in Mathematica. $\endgroup$
    – Michael E2
    Commented Mar 9, 2017 at 14:26

1 Answer 1

2
$\begingroup$

One idea is to automatically change Subscript[a, n] into Subscript[RawBoxes["a"], n] to avoid recursion errors. Both a and RawBoxes["a"] look the same, so you will only notice that they are different by looking at the InputForm/FullForm of an expression (this is an idea I mentioned previously in (137993)). A couple possibilities to do this are:

If you always enter subscripts using control + _, then you can use a MakeExpression rule:

MakeExpression[SubscriptBox["a", n_], StandardForm] := With[
    {v = HoldComplete[RawBoxes["a"]], sub = MakeExpression[n, StandardForm]},
    Thread[Unevaluated @ Subscript[v, sub], HoldComplete]
]

Or, you can add DownValues to Subscript:

Subscript /: Subscript[a, n_] := Subscript[RawBoxes@"a", n]
Subscript /: Set[Subscript[a, n_], rhs_] := Subscript[RawBoxes@"a", n] = rhs

Depending on your workflow, you may need to do both. Note, however, that overriding built-in symbols in this way can lead to trouble. This is why I've written the above code to only work for a, and not for any symbol.

At any rate, now you can do things like:

Solve[a + Subscript[a, 1] == 0, a]
D[a + Subscript[a, 1], a]

{{a -> -Subscript[a, 1]}}

1

as well as your original example:

a = {Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]}
a // InputForm

{Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]}

{Subscript[RawBoxes["a"], 1], Subscript[RawBoxes["a"], 2], Subscript[RawBoxes["a"], 3]}

Notice, no recursion errors.

$\endgroup$
6
  • 1
    $\begingroup$ Carl -- this is really bad advice. Complicated workarounds in order to use subscripts instead of actual symbols is a recipe for disaster. In this scheme you will not even be able to see the problem because a and "a" look alike. $\endgroup$
    – bill s
    Commented Mar 9, 2017 at 19:21
  • $\begingroup$ Perhaps you can share "the problem" that you are referring to? $\endgroup$
    – Carl Woll
    Commented Mar 9, 2017 at 19:28
  • $\begingroup$ Number 3 here: mathematica.stackexchange.com/a/18395/1783 $\endgroup$
    – bill s
    Commented Mar 10, 2017 at 0:26
  • $\begingroup$ @Carl. This is a highly interesting way for working with subscripted variables (which I never do). A small question: you consider subscripted RawBoxes["a"]. It seems to me that you could have used subscripted strings in the same way: Subscript["a", 3]. Is there a particular reason for using RawBoxes? $\endgroup$ Commented Mar 11, 2017 at 12:39
  • $\begingroup$ @FredSimons I use TraditionalForm a lot, and in TraditionalForm, a and "a" look different because of SingleLetterItalics. $\endgroup$
    – Carl Woll
    Commented Mar 11, 2017 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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