4
$\begingroup$

I try to make designation

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

but receive mistake

$RecursionLimit::reclim: Recursion depth of 1024 exceeded.

What`s the problem?

$\endgroup$
9
  • $\begingroup$ Don't use subscripts until you're an expert. A subscripted symbol isn't a symbol $\endgroup$ Mar 18, 2015 at 17:53
  • $\begingroup$ @belisarius: Given Mathematica's formatting palettes etc., users are actually lead into using that quite common mathematical notation, especially when it comes to vectors, matrices etc... $\endgroup$
    – Jinxed
    Mar 18, 2015 at 19:02
  • 3
    $\begingroup$ @Jinxed That's true, but it doesn't make the advice less valid. Subscripted variables should be usually avoided. If one really cannot live without them use the Notation package. $\endgroup$ Mar 18, 2015 at 23:13
  • $\begingroup$ So what is subscripted stuff used for (except typsetting, of course)? I've browsed around for links, but all I found was warnings for beginners never to use them or to use the Notation package. $\endgroup$
    – LLlAMnYP
    Mar 19, 2015 at 0:00
  • $\begingroup$ Its a little strange situation: its possible to Superscript (not the Power) but impossible to Subscript (not index). It`s not good. In any case. Many thanks for all. Where to find wish list for mma? $\endgroup$ Mar 19, 2015 at 5:13

2 Answers 2

5
$\begingroup$

The reason is, that while assigning, the a in Subscript[a,_] is replaced by the whole list of subscripts, and so on and so on, until the recursion limit is reached: Subscripts are no symbols by themselves.

To avoid this, you can either use another variable to assign to (b e.g.), or define the subscripts as proper symbols:

Needs["Notation`"]

makesymbol[obj_]:=With[{},      
  If[NameQ@ToString@Unevaluated@obj,Remove@obj]; (* remove possibly existing symbol first *)
  Symbolize@ParsedBoxWrapper@ToBoxes@obj;]       (* then create the new symbol *)

With this function, you can create a symbol like so:

makesymbol[Subscript[a,1]]

Mathematica will now treat it the same as any other (simpler) symbol.

For your case:

Be sure to put the formatted variables into the list here, not the Subscript-form:

makesymbol /@ {Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]}

and then your assignment will work without problems.

$\endgroup$
3
  • $\begingroup$ makesymbol[Subscript[a,1]] doesn`t return any result, makesymbol /@ {Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]} return {Null,Null,Null} $\endgroup$ Mar 19, 2015 at 5:22
  • 1
    $\begingroup$ @SergeyFomin It's not supposed to return a result. However, after executing the above commands you are now able to use the subscripted variables treated with it as real variables. Look at the FullForm of a_1 with the subscript entered using the keyboard (using ctrl + _ on my US International keyboard) $\endgroup$ Mar 19, 2015 at 7:41
  • $\begingroup$ @SergeyFomin: Did my answer work out for you? $\endgroup$
    – Jinxed
    Mar 21, 2015 at 16:17
3
$\begingroup$

My favorite approach when it's unavoidable to use such formatting is this simple trick:

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

(* ==> {Subscript["a", 1], Subscript["a", 2], Subscript["a", 3]} *)

Just make the subscripted names into strings. You can then do things like a[[1]] or even Subscript["a", 1] = 10 without getting in trouble. The quotation marks also remind you that you're dealing with something more subtle: The value in the last assignment is stored in DownValues[Subscript]. This is the big difference in using Subscript versus regular symbol names, and one should never forget it.

As usual, you can find most of this by searching the following giant Q&A: What are the most common pitfalls awaiting new users?.

$\endgroup$

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.