f



Hints for using subscripted variables in Mathematica packages

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 (10-15 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 user-defined 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 user-defined 
Mathematica package. I should mention that Mathematica V9 has been used.

Regards
Robert Kragler

-- 
Prof. Dr. Robert Kragler
Hasenweg 5
D-88090 Immenstaad, Germany
Phone : +49 (7545) 2833 or 3500
Email : kragler@hs-weingarten.de
URL :   http://portal.hs-weingarten.de/web/kragler



0
Dr
5/23/2014 6:42:45 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

0 Replies
1434 Views

Similar Articles

[PageSpeed] 54

Reply:

Web resources about - Hints for using subscripted variables in Mathematica packages - comp.soft-sys.math.mathematica

Heat capacity - Wikipedia, the free encyclopedia
Heat capacity (usually denoted by a capital C , often with subscripts), or thermal capacity , is the measurable physical quantity that characterizes ...

4.1 Policy Evaluation
4.1 Policy Evaluation

Our Dependency on Foreign Keys, Part One - acrossthemargin
by: William Shunn (The header art featured is by the innovative digital artist, Hayrettin Karaerkek, entitled "The Puppeteer.") Part one, of ...

Index of Compute! September 1983 articles
Index of Compute! September 1983 articles Compute! Issue 40 - September 1983 Smart Products by Kathy Yakal Games That Teach by John Blackford ...

MATLAB Documentation - MathWorks Deutschland
MATLAB is a high-level language and interactive environment for numerical computation, visualization, and programming.

MyLovelyMac.com - 3rd Generation Apple TV on Ebay
... Apple TV available If you were thinking about buying this shiny magic box, this is definetly the time to do so. Every one who is subscripted ...

Children's Books
e interrupt this New York Times Book Review for a special bulletin: There are fairies at the bottom of the garden. They are armed and dangerous. ...

Extending APL
After APL\360 became popular, other computer vendorsbrought out their own versions of APL. Often, they addedfeatures allowing APL programs to ...

This 18kt Gold HTC One Will Cost You $4,400
The newly-released iPhone 5s comes in three distinct colors - a first for the Cupertino company - one of which has been dubbed as the “Gold” ...

Noakes on VO2 max: Its uses and abuses
[caption id="attachment_192" align="alignright" width="250" caption="930 pages of science!"][/caption]One of the first books site consulting ...

Resources last updated: 1/26/2016 9:57:04 PM