4

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?

Jinxed
  • 3,753
  • 10
  • 24
Igor Fomenko
  • 573
  • 3
  • 13
  • Don't use subscripts until you're an expert. A subscripted symbol isn't a symbol – Dr. belisarius Mar 18 '15 at 17:53
  • @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... – Jinxed Mar 18 '15 at 19:02
  • 3
    @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. – Sjoerd C. de Vries Mar 18 '15 at 23:13
  • 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. – LLlAMnYP Mar 19 '15 at 00:00
  • 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? – Igor Fomenko Mar 19 '15 at 05:13
  • @SergeyFomin: You should contact Wolfram support for this. The more such notes they get, the more likely they will do something about that. I e.g. would appreciate to have such constructs automatically symbolized, since I really like to have formulas displayed in typeset form in order to better serve my users' needs. – Jinxed Mar 19 '15 at 07:19
  • @SjoerdC.deVries: Sure, but how should the aspiring Mathematica-user know about that behavior? It is hard to find even a mention in the documentation. As I wrote to Sergey: Why not automatically symbolize such constructs if entered in a typeset way? This would solve such problems, while leaving the option to actually use Subscript for advanced purposes and advanced users. – Jinxed Mar 19 '15 at 07:22
  • @Jinxed It's mentioned in our most popular Mathematica Pitfalls question. No substitute for documentation, I know, but Sergey has been on this site for over two and a half years. – Sjoerd C. de Vries Mar 19 '15 at 07:27
  • A possible solution it to use Hold attributes; see: (20195), (22376) – Mr.Wizard Sep 25 '16 at 13:03

2 Answers2

5

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.

Jinxed
  • 3,753
  • 10
  • 24
  • makesymbol[Subscript[a,1]] doesn`t return any result, makesymbol /@ {Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]} return {Null,Null,Null} – Igor Fomenko Mar 19 '15 at 05:22
  • 1
    @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) – Sjoerd C. de Vries Mar 19 '15 at 07:41
  • @SergeyFomin: Did my answer work out for you? – Jinxed Mar 21 '15 at 16:17
3

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?.

Jens
  • 97,245
  • 7
  • 213
  • 499