Subscript (using subscripted symbols)

I wrote a package that makes it very easy to do switch back an forth  between treating subscripts as symbols and not.  The package is largely based  on code samples discussed here and the package can be downloaded from  http://library.wolfram.com/database/MathSource/4268.

The next line is a slight variation of a tip David Bailey sent to  the MathGroup.  Using this code an 'a' with a non-negative integer subscript  evaluates to a symbol where the character 'a' is concatenated with the  symbol.

Clear["Global`*"] ;  a_i_ ? (IntegerQ[#] &&NonNegative[#] &) := Symbol[ToString[a] <>ToString[i]]


Now in the next line the first three in the list are symbols, and all the
rest are subscript expressions.  

Head/@ {a_0, a_1, a_2, a_b, a_ (1/2)}

{Symbol, Symbol, Symbol, Subscript, Subscript}

If this approach is used you must avoid using both {a_1, a_2, a_3} and {a1, a2, a3} as symbols, since the former will evaluate to the  latter.  This is demonstrated in the next cell.

{a_1, a_1 === a1}

{a1, True}


The next line shows that the rule above is stored in DownValues[Subscript].

DownValues[Subscript]

{HoldPattern[a_i_ ? (IntegerQ[#1] &&NonNegative[#1] &)] Symbol[ToString[a] <>ToString[i]]}

Before continuing the above DownValue is cleared.

DownValues[Subscript] = {} ;

Colin Rose gave the following examples where Subscripts can be used in Mathematica Version 4 without using the notation palette.

z = Thread[Subscript[y, Range[12]]]

{y_1, y_2, y_3, y_4, y_5, y_6, y_7, y_8, y_9, y_10, y_11, y_12}

z/.Subscript[y, x_] y^x

{y, y^2, y^3, y^4, y^5, y^6, y^7, y^8, y^9, y^10, y^11, y^12}

%/.y^x_. Subscript[y, x]

{y_1, y_2, y_3, y_4, y_5, y_6, y_7, y_8, y_9, y_10, y_11, y_12}

z/.Subscript[y, t_] Subscript[y, t - 1]

{y_0, y_1, y_2, y_3, y_4, y_5, y_6, y_7, y_8, y_9, y_10, y_11}


In v4, you can generally use subscripted "variables" as if they were
symbols.  The examples below work fine.

expr = y_1 + y_2 ;    Solve[expr2, y_1 ]

{{y_12 - y_2}}

Integrate[expr, y_1]

y_1^2/2 + y_1 y_2

Plot3D[Sin[y_1 + y_2], {y_1, 0, 3}, {y_2, 0, 3}] ;

The most common problem occurs when people simultaneously try to use
(x)  and x_1, x_2, etc .  In that case if one uses (x=7) then  x_1  7_1 , x_2  7_2 .  This sort of problem is easily avoided by NOT simultaneously using x  with x_1, x_2, ...


Carl Woll indicated we can give Subscript the attribute HoldFirst. As in the
example below.

ClearAll[Subscript] ;    SetAttributes[Subscript, HoldFirst] ;

Now we can have symbols  t,  t_1 without conflict.

t = 34 ;  t_1

t_1

Table[t_1, {t_1, 5}]

{1, 2, 3, 4, 5}

ClearAll[Subscript]


Collin Rose also indicated use of Symbolize button in the notation palette
can result in problems as in the following example.

Needs["Utilities`Notation`"]

Symbolize[m_1] ;    Symbolize[m_2] ;    Symbolize[m_3] ;

z1 = {m_1, m_2, m_3} ;  Head/@z1

{Symbol, Symbol, Symbol}

So far we don't have a problem.

z2 = Table[m_i, {i, 3}]

{m_1, m_2, m_3}

Head/@z2

{Subscript, Subscript, Subscript}

We now have two sets of "identical" notation in use.  We have m_2 as a Symbol in z1, and  m_2 as subscript expression in z2.  However, the elements in z1 and z2 look  the same on screen.  One must be careful to make sure this doesn't happen.

The steps used above to make {m_1, m_2, m_3} symbols can be undone by using the RemoveSymbolize button in the  FullNotation palette.  It's under Symbolizations, StandardForm Symbolize.

RemoveSymbolize[m_1 ] ;  RemoveSymbolize[m_2 ] ;  RemoveSymbolize[m_3 ] ;

Allan Hayes suggested the following to symbolize ALL subscript objects.   Below we see that this will cause even Log[3] _ ( a) to have the head Symbol.  It's hard to imagine why one would use such a  form, so this may be of little concern.

Needs["Utilities`Notation`"]

Symbolize[_ __]

Head/@ {a_b, t_2, Log[3] _a}

{Symbol, Symbol, Symbol}

After using the code above to treat anything with any type of a subscript  as a Symbol the use of Table below doesn't return the list {x_1 , x_2 , x_3 , x_4}.

Table[x_i, {i, 4}]

{x_i, x_i, x_i, x_i}


Before we continue we might want to remove the rule that makes anything with
a subscript a symbol.  The line after RemoveSymbolize demonstrates that the
rule was in fact removed.

RemoveSymbolize[_ __]

Head/@Table[a_i, {i, 4}]

{Subscript, Subscript, Subscript, Subscript}


A modification of code Jason Harris sent to the MathGroup is given below.

One variation of code Jason Harris sent to the MathGroup is shown below.   The version below will treat (i) with a non-negative integer subscript as a  symbol, but (i) with any other subscript is treated as a Subscript  expression.  This requires the use of some advanced features of the Notation  palette.  Documentation can be found at Notation:Definition:NotationPatternTag under Add-ons in the Help Browser.

Needs["Utilities`Notation`"]

i_n_ ? (IntegerQ[#] &&NonNegative[#] &) := ToExpression[MakeBoxes[i_n]]    ... 2371;     i_n_ ? (IntegerQ[#] &&NonNegative[#] &) := ToExpression[MakeBoxes[i_n]]


Now (i) with a positive integer subscript is an symbol, while (i) with any
other subscript is a subscript expression.

Head/@{i_2, i_ (-3), i_a, i_1.2, i_π, i_ (2/3)}

{Symbol, Subscript, Subscript, Subscript, Subscript, Subscript}

Below i_n is an expression rather than a Symbol, so the list { i_0 , i_1 , i_ (2  ), i_3 }  is returned.

lst = Table[i_n, {n, 0, 3}]

{i_0, i_1, i_2, i_3}

Head/@lst

{Symbol, Symbol, Symbol, Symbol}

■  How to make the expression : Symbolize[i__ ? IntegerTest]


Short of manually entering the necessary cell expression there are two ways
to make the Symbolize expression above.  Both are explained below.

•  Use keyboard shortcuts:

Type [ESC]symb[ESC][TAB]i

Symbolize[i]

Depress [CTRL]-

Symbolize[i_]


Type  [ESC]pattwraper[ESC][TAB]_?IntegerTest

Symbolize[i__ ? IntegerTest]

•  Use the Notation palette:


 You may need to patch a bug before this will work.

In some versions of the Notation package one of the needed buttons needs  to be fixed before the code below will work.To fix the problem use an ASC  text editor (MS-Word will work on a PC) to edit the file  (FullNotationPalette.nb).  The file is normally in the directory
      WolframResearch\Mathematica\AddOns\ExtraPackages\Utilities.
Once you  have the file open with a test editor delete the line shortly after the  introductory comments containing the word CacheID.  Then use the Find feature  to locate "InsertPatternWrapper" which will bring you to the code segment  below.  Shortly after
     StyleBox["InsertPatternWrapper", ...
     

Change the code  
   ButtonData:>TagBox["\[PlaceHolder]", NotationPatternTag,  ...
   

to
ButtonData:>TagBox["\[SelectionPlaceHolder]", NotationPatternTag, ...
  
After making these minor edits  save the file.

Enter the input cell below.

i__ ? IntegerTest


Select the entered expression, and depress the Symbolize[
■] button in the Notation palette.

Symbolize[i__ ? IntegerTest]


Select the subscript

    _?IntegerTest

Depress the button for "Full Notation Palette".

When the Full Notation Palette comes up select "InsertPatternWrapper" under
"Wrapper Boxes".

Symbolize[i__ ? IntegerTest]


Unfortunately it's rather difficult to use the RemoveSymbolize button to
remove the definition above.


Another modification of code Jason Harris provided is given below.


Once the Notation package is loaded evaluating the next cell will define a
function (SubscriptSymbols) that takes a symbol as an argument, set up things
so that if one evaluates SubscriptSymbols[vari], then (vari) with a
non-negative integer subscript will be treated as a symbol.

IntegerTest[n_] := ToExpression[n, StandardForm, IntegerQ[#] &&NonNegative[#] &] < ... t;}], NotationPatternTag]]]] ; x_i_ ? IntegerQ := ToExpression @ MakeBoxes[ x_i])


The next cell causes (v) with non-negative integer subscript to be treated as
a Symbol.

SubscriptSymbols[v]

In the next cell only v_2 has the Head Symbol.

v_2 = 5 ;

Head/@{v_2, v_a, v_1.5, v_ (2/3)}

{Integer, Subscript, Subscript, Subscript}


Still another modification of code Jason Harris provided is given below.

Needs["Utilities`Notation`"] ;    IntegerTest[n_] := ToExpression[n, StandardFor ... rnTag]]]] ;       (symb_ ? VariableQ) _i_ ? IntegerQ := ToExpression @ MakeBoxes[ symb_i]


After evaluating the cells above the first three symbols below have the head
Symbol while the others have the head Subscript.

Head/@{a_0, b_1, γ_0, a_d, b_1.5, c_ (2/3), π_2}

{Symbol, Symbol, Symbol, Subscript, Subscript, Subscript, Subscript}


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page