Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Hints for using subscripted variables in Mathematica packages
Posted:
May 23, 2014 2:42 AM


Hi,
perhaps some of you have 'struggled' in the past with subscripted or subsuperscripted variables. To my opinion and observation most Mathematica users do not exploit the potential of the Mathematica notation. As I had noticed Mathematica code and output would (sometimes) look much nicer and more readable if one uses the FrontEnd features provided by Mathematica's notation features. The Notation package was introduced in Mathematica long ago (1015 years) by Jason Harris from WRI; with the help of this package one is able to create very sophisticated notations quite common in scientific notation, for example, in mathematics or physics .
However, the correct usage of subscripted variables has its price. If one introduces on the FrontEnd level subscripted variables only by Subscript[a, b] these quantities do not have (required) type 'Symbol' but are treated as expressions with head 'Subscript'. Only loading Notation package a *Notation Palette* pops up with several buttons, e.g. *Notation[... <=> ...]* . Important in this context for the definition of subscripted symbols is *Symbolize[ \[SelectionPlaceholder] ].* Instead of the placeholder one may define *Symbolize[ **Subscript[a_, b_] ] * with the effect that each subsequently defined subscripted variable will be considered as a symbol. If *FullForm* is applied to, say *Subscript[A, 1]*, then the internal representation *A\[UnderBracket]Subscript\[UnderBracket]1* is shown (which gives rise to head Symbol so that this subscripted quantity is treated as any other symbol such as x, y1, ... in Mathematica).
Of course, this is nothing new. However, there are some pitfalls if subscripted variables are used inside userdefined Mathematica *packages*. I struggled with this problem for some time and  thanks to the help of Jason Harris / WRI (who is the expert for these kind of questions)  I found a solution which I would like to share with other Mathematica users.
If you want to define a Mathematica package *ABC.m *(with _context_, say *XYZ*) making usage of subscripted variables one has to do the following (I will only give the skeleton of the relevant commands) :
BeginPackage["XYZ`","Notation`"]; (* import of Notation package which is autoloaded *)
Unprotect[Evaluate[Context[ ]<>"*"]];
(*  declaration part of XYZ  *)
(*  *) Off[General::spell1]; Off[Symbolize::boxSymbolExists]; (* subscripted symbols : *) Symbolize[ParsedBoxWrapper[SubscriptBox["_","_"]]]; (* this is the internal representation of Symbolize suggested by Jason Harris for Mma package*) On[Symbolize::boxSymbolExists];
(* makes a subscripted variable to a quantity with head Symbol *) Subscript[a_,b_]:= SubscriptBox[ ToBoxes[a], ToBoxes[b] ]//ToExpression
varList = { u, v, w, x, y, z }; (* define some subscripted variables Subscript[A,u] etc. *) subscriptList = Table[ Subscript[ A,varList[[k]] ],{k,varList//Length}]; (*  *)
(* myToString[a_List] inserts lists with subscripted symbols into a usage string (suggested by Jason Harris/WRI *)
myToString @ a_List := StringJoin[Riffle[myToString /@ a, ", "]] myToString @ a_Symbol /; (Head@ToBoxes@a == SubscriptBox) := myToStringBoxes @ ToBoxes @ a myToString @ a_ := ToString @ a myToStringBoxes @ SubscriptBox[a_, b_] := StringJoin[{"\<\!\(\*SubscriptBox[", a, ",", b, "]\)\>"}]
(*  *)
varList::usage= "varList=List of variables : varList={u,v,w,x,y,z} default; or {" <> *myToString[VList]* <> "} is a modified list of variables. Both lists may be modified by the user to change variables and deduced quantities but ONLY after the Notation package has been invoked."; ... (* further usage messages *)
(*  Implementation part of XYZ  *)
Begin["`Private`"]; ... (* procedures etc. defined in context Private` *)
End[ ]; (* end of private context *)
Protect[Evaluate[Context[ ]<>"*"]]; EndPackage[ ]; (* end of package context *)
I hope that this skeleton example of a package will help to clarify the question how subscripted variables may be used in the framework of a userdefined Mathematica package. I should mention that Mathematica V9 has been used.
Regards Robert Kragler
 Prof. Dr. Robert Kragler Hasenweg 5 D88090 Immenstaad, Germany Phone : +49 (7545) 2833 or 3500 Email : kragler@hsweingarten.de URL : http://portal.hsweingarten.de/web/kragler



