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!!
ClearAll[Subscript]
in caseAttributes
has previously been set to something undesirable. Otherwise, there's nothing wrong with defining a function likeSubscript
, 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, orSubscript[a + b c,i]
? That result doesn't look like something you normally want. $\endgroup$