0

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.

Physkid
  • 680
  • 5
  • 13
  • 1
    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} – Kuba Mar 09 '17 at 13:14
  • But a isn't the same as Subscript[a,x] so why does Mathematica sees it this way-and hence, arriving at a conflict? – Physkid Mar 09 '17 at 13:16
  • 1
    " '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. – Kuba Mar 09 '17 at 13:17
  • 1
    You will save yourself a lot of hassle in the future if you heed the words of item 3 in this post regarding subscripts: http://mathematica.stackexchange.com/a/18395/1783 – bill s Mar 09 '17 at 13:39
  • 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. – Michael E2 Mar 09 '17 at 14:26
  • You may also want to read this post: http://mathematica.stackexchange.com/questions/50726/can-a-symbol-own-ownvalues-that-dont-evaluate-when-its-the-head-of-an-expressi#comment149900_50726 – xzczd Mar 09 '17 at 15:22

1 Answers1

2

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.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 1
    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. – bill s Mar 09 '17 at 19:21
  • Perhaps you can share "the problem" that you are referring to? – Carl Woll Mar 09 '17 at 19:28
  • Number 3 here: http://mathematica.stackexchange.com/a/18395/1783 – bill s Mar 10 '17 at 00:26
  • @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? – Fred Simons Mar 11 '17 at 12:39
  • @FredSimons I use TraditionalForm a lot, and in TraditionalForm, a and "a" look different because of SingleLetterItalics. – Carl Woll Mar 11 '17 at 15:47
  • @Carl. I see. I rarely use TraditionalForm, so I did not think of that. Thanks. – Fred Simons Mar 12 '17 at 07:46