4

I have written a code, and I need a little feed back..

I want to make the Subscript distributive across (1) addition and (2) multiplication to mimic short-hand for added vectors. For example, in $(p+2k)_i=p_i+2k_i$ the LHS is short-hand for RHS, where the index is not allowed to be on the coefficient 2. Finally, I need exception, where the numeric quantity 0 should be allowed to take an index: that is, $0_i$ should not go to $0$.

My code is:

Clear[Subscript];

Subscript[Plus[a_, b___], idx_] := 
  Subscript[a, idx] + Subscript[b, idx];

Subscript[Times[a_, b___], idx_] := 
  Times[Subscript[a, idx], Subscript[b, idx]];

Subscript[a_?NumberQ, idx_] /; a != 0 := a;

What I did was to define Subscript as functions, which is already built-in Symbol in Mathematica. The code seems to really work, but I don't know if I deserve a pat-on-the-back for being clever, or be reprimanded for overwriting a built-in Symbol (or for any other bad coding behavior).

Also, is there any obvious way anyone can see this piece of code going wrong? Thanks!!

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
QuantumDot
  • 19,601
  • 7
  • 45
  • 121
  • 1
    I would use ClearAll[Subscript] in case Attributes has previously been set to something undesirable. Otherwise, there's nothing wrong with defining a function like Subscript, because it has no built-in meaning. However, I don't know what the real intent is, so it's hard to write anything other than a comment. E.g., what about dot products, or Subscript[a + b c,i]? That result doesn't look like something you normally want. – Jens Jan 27 '13 at 21:25
  • @Jens Ah! Very good point about dot products. I suppose that this is restricted to sums of vectors, and products of numbers and vectors. But not square of vectors. Thank you for feedback. – QuantumDot Jan 27 '13 at 22:12

1 Answers1

4
  • In my opinion Subscript is a formatting function and should not be occupied with this kind of definition. With such a definition in place you will have to rely on SubscriptBox any time you want a simple subscript without these rules.

  • Your definitions as written insert a raw sequence into Subscript that I do not believe you want. I shall assume that $(a+b+c)_7$ should be expanded to $a_7+b_7+c_7$ and not $a_7+b_{c,7}$

To correct these I would define:

ss[a_ + b___, idx_] := ss[a, idx] + ss[+b, idx];

ss[a_ * b___, idx_] := ss[a, idx] * ss[1 b, idx];

ss[a_?NumberQ, idx_] /; a != 0 := a;

Format[x : ss[args__]] := Interpretation[Subscript[args], x]

Now:

ss[q + r + s + x y z, 7]

$q_7+r_7+s_7+x_7 y_7 z_7$

% // InputForm
ss[q, 7] + ss[r, 7] + ss[s, 7] + ss[x, 7]*ss[y, 7]*ss[z, 7]

Jens comments that attaching a definition to Subscript in the way you did is perfectly acceptable. If you choose that option then you will probably want to define a new subscript function for the purposes of pure formatting. For example:

MakeBoxes[realSubscript[a_, b__], fmt_] := 
  SubscriptBox[a, RowBox @ Riffle[{b}, ","]]

realSubscript[1, 2]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • The tutorial doesn't agree with you. An operator without built-in meaning can be a very convenient way to define a new function that automatically comes with a notational form. The latter doesn't mean that it's only a formatting construct. – Jens Jan 28 '13 at 03:23
  • @Jens That's fine. I don't always agree with the documentation or the popular opinion. I would (and do) use other operators directly. However I use subscript in labels, etc. often enough that I would not want to have to define a new subscript function though I suppose that's an option for others. – Mr.Wizard Jan 28 '13 at 03:41
  • Sure - you marked the statement as an opinion - I just wanted to mention the alternative opinion. – Jens Jan 28 '13 at 03:44