Re: Re: Subscripts, Doh!!!

• To: mathgroup at smc.vnet.net
• Subject: [mg19296] Re: [mg19269] Re: Subscripts, Doh!!!
• From: Carl Woll <carlw at u.washington.edu>
• Date: Thu, 12 Aug 1999 22:34:49 -0400
• Organization: Physics Department, U of Washington
• References: <7o5ier\$rme@smc.vnet.net> <7oba5o\$3p6@smc.vnet.net> <"199908060358.XAA08108"@smc.vnet.net> <v04210100b3d0c0366e0b@[172.16.41.9]> <199908120524.BAA04506@smc.vnet.net.>
• Sender: owner-wri-mathgroup at wolfram.com

```Colin,

Like you, I like subscripts and I don't like to use the Symbolize function.
You mentioned a few things that go wrong when you use subscripts, and I
have some proposed solutions.

Colin Rose wrote:

> The most common problem occurs when people simultaneously
> try to use:
>
>      x   AND   x_1, x_2  etc
>
> They then set
>
>     x=7,
>
> and get very confused when they get terms such as
>
>      7_1, 7_2, 7_3
>
> These sorts of problems are easily avoided by NOT
> simultaneously using x   WITH   x_1, x_2... .

An alternative solution to avoidance is to give Subscript the attribute
HoldFirst. For example,

In[102]:=
ClearAll[Subscript]
SetAttributes[Subscript,{HoldFirst}]

In[104]:=
t=1
Subscript[t,1]

Out[104]=
1

Out[105]=
t
1

In[106]:=
Table[Power[x,Subscript[t,1]],{Subscript[t,1],1,5}]

Out[106]=
2   3   4   5
{x, x , x , x , x }

Another problem you discussed in a later post:

Colin Rose wrote:

> Yes - it would have been nice if Clear[Subscript[m, 1]] had
> been 'made to work' in v4. At the moment, one has to use
>
>     Unset[Subscript[m, 1]]
>
> or
>
>     Clear[Subscript]       (which clears all subscripted 'variables').

If I want to clear a single definition, then Unset is acceptable. The
problem I have is when I want to clear a lot of subscripted definitions for
a single base  symbol. One approach is to make the definitions upvalues for
the base symbol. A neat alternative is to massage the downvalues of
Subscript directly, as in the following function:

In[107]:=
ClearAll[ClearSubscript]
SetAttributes[ClearSubscript, {HoldAll}]
ClearSubscript[a_] := Module[{b},
DownValues[Subscript] =
Cases[DownValues[Subscript], (b_ :> _) /; FreeQ[b,
HoldPattern[Subscript[a, _]]]];
]

Now for some test cases.

In[110]:=
Subscript[x,1]=1;
Subscript[x,a_Integer]:=a^2
Subscript[y,2]=2;

In[112]:=
??Subscript

System`Subscript
Attributes[Subscript] = {HoldFirst}

Subscript[x, 1] = 1

Subscript[y, 2] = 2

Subscript[x, a_Integer] := a^2

In[113]:=
ClearSubscript[x]
??Subscript

System`Subscript
Attributes[Subscript] = {HoldFirst}

Subscript[y, 2] := 2

Finally, the point about ?m sub 1 brought up by Jason.

> Jason Harris wrote:
>
> >Just using subscripted symbols has its problems though...
> >
> >For instance  \!\(\(?m\_1\)\), that is ? m sub 1 will yield an
> >error. As will something like \!\(Clear[m\_1]\), that is Clear[ m
> >sub 1]. etc.

One could similarly cook up a function that provides just the information
related to a base symbol as follows:

In[159]:=
ClearAll[SubscriptInformation]
SetAttributes[SubscriptInformation, {HoldAll}]
SubscriptInformation[x_] := Module[{dv, b},
dv = DeleteCases[DownValues[Subscript], (b_ :> _) /; FreeQ[b,
HoldPattern[Subscript[x, _]]]];
Information[x];
PrintDownValue /@ dv;
]

Clear[PrintDownValue]
PrintDownValue[Verbatim[HoldPattern][a_] :> b_] := Print[HoldForm[a = b]]

And some tests:

In[164]:=
Subscript[x,1]=1;
Subscript[x,a_Integer]:=a^2
Subscript[y,2]=2;
y=2;

In[166]:=
SubscriptInformation[x]

Global`x
x  = 1
1
2
x          = a
a_Integer

In[167]:=
SubscriptInformation[y]

Global`y
y = 2
y  = 2
2

So what do you think? Are there any major problems with the above ideas?

Carl Woll
Dept of Physics
U of Washington

--------------FB7FAA6A81D74D9318487496

<HTML>
Colin,

<P>Like you, I like subscripts and I don't like to use the Symbolize function.
You mentioned a few things that go wrong when you use subscripts, and I
have some proposed solutions.

<P>&nbsp;Colin Rose wrote:
<BLOCKQUOTE TYPE=CITE>The most common problem occurs when people simultaneously
<BR>try to use:

<P>&nbsp;&nbsp;&nbsp;&nbsp; x&nbsp;&nbsp; AND&nbsp;&nbsp; x_1, x_2&nbsp;
etc

<P>They then set

<P>&nbsp;&nbsp;&nbsp; x=7,

<P>and get very confused when they get terms such as

<P>&nbsp;&nbsp;&nbsp;&nbsp; 7_1, 7_2, 7_3

<P>These sorts of problems are easily avoided by NOT
<BR>simultaneously using x&nbsp;&nbsp; WITH&nbsp;&nbsp; x_1, x_2... .</BLOCKQUOTE>
An alternative solution to avoidance is to give Subscript the attribute
HoldFirst. For example,

<P><TT>In[102]:=</TT>
<BR><TT>ClearAll[Subscript]</TT>
<BR><TT>SetAttributes[Subscript,{HoldFirst}]</TT><TT></TT>

<P><TT>In[104]:=</TT>
<BR><TT>t=1</TT>
<BR><TT>Subscript[t,1]</TT><TT></TT>

<P><TT>Out[104]=</TT>
<BR><TT>1</TT><TT></TT>

<P><TT>Out[105]=</TT>
<BR><TT>t</TT>
<BR><TT>&nbsp;1</TT><TT></TT>

<P><TT>In[106]:=</TT>
<BR><TT>Table[Power[x,Subscript[t,1]],{Subscript[t,1],1,5}]</TT><TT></TT>

<P><TT>Out[106]=</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp; 3&nbsp;&nbsp; 4&nbsp;&nbsp;
5</TT>
<BR><TT>{x, x , x , x , x }</TT>

<P>Another problem you discussed in a later post:

<P>&nbsp;Colin Rose wrote:
<BLOCKQUOTE TYPE=CITE>Yes - it would have been nice if Clear[Subscript[m,
<BR>been 'made to work' in v4. At the moment, one has to use

<P>&nbsp;&nbsp;&nbsp; Unset[Subscript[m, 1]]

<P>or

<P>&nbsp;&nbsp;&nbsp; Clear[Subscript]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(which clears all subscripted 'variables').</BLOCKQUOTE>
If I want to clear a single definition, then Unset is acceptable. The problem
I have is when I want to clear a lot of subscripted definitions for a single
base&nbsp; symbol. One approach is to make the definitions upvalues for
the base symbol. A neat alternative is to massage the downvalues of Subscript
directly, as in the following function:

<P><TT>In[107]:=</TT>
<BR><TT>ClearAll[ClearSubscript]</TT>
<BR><TT>SetAttributes[ClearSubscript, {HoldAll}]</TT>
<BR><TT>ClearSubscript[a_] := Module[{b},</TT>
<BR><TT>&nbsp;DownValues[Subscript] =</TT>
<BR><TT>&nbsp; Cases[DownValues[Subscript], (b_ :> _) /; FreeQ[b, HoldPattern[Subscript[a,
_]]]];</TT>
<BR><TT>]</TT><TT></TT>

<P>Now for some test cases.<TT></TT>

<P><TT>In[110]:=</TT>
<BR><TT>Subscript[x,1]=1;</TT>
<BR><TT>Subscript[x,a_Integer]:=a^2</TT>
<BR><TT>Subscript[y,2]=2;</TT><TT></TT>

<P><TT>In[112]:=</TT>
<BR><TT>??Subscript</TT><TT></TT>

<P><TT>From In[112]:=</TT>
<BR><TT>System`Subscript</TT>
<BR><TT>Attributes[Subscript] = {HoldFirst}</TT>
<BR><TT>&nbsp;</TT>
<BR><TT>Subscript[x, 1] = 1</TT>
<BR><TT>&nbsp;</TT>
<BR><TT>Subscript[y, 2] = 2</TT>
<BR><TT>&nbsp;</TT>
<BR><TT>Subscript[x, a_Integer] := a^2</TT><TT></TT>

<P><TT>In[113]:=</TT>
<BR><TT>ClearSubscript[x]</TT>
<BR><TT>??Subscript</TT><TT></TT>

<P><TT>From In[113]:=</TT>
<BR><TT>System`Subscript</TT>
<BR><TT>Attributes[Subscript] = {HoldFirst}</TT>
<BR><TT>&nbsp;</TT>
<BR><TT>Subscript[y, 2] := 2</TT>

<P>Finally, the point about ?m sub 1 brought up by Jason.
<BLOCKQUOTE TYPE=CITE>Jason Harris wrote:

<P>>Just using subscripted symbols has its problems though...
<BR>>
<BR>>For instance&nbsp; \!\(\(?m\_1\)\), that is ? m sub 1 will yield an
<BR>>error. As will something like \!\(Clear[m\_1]\), that is Clear[ m
<BR>>sub 1]. etc.</BLOCKQUOTE>
One could similarly cook up a function that provides just the information
related to a base symbol as follows:<TT></TT>

<P><TT>In[159]:=</TT>
<BR><TT>ClearAll[SubscriptInformation]</TT>
<BR><TT>SetAttributes[SubscriptInformation, {HoldAll}]</TT>
<BR><TT>SubscriptInformation[x_] := Module[{dv, b},</TT>
<BR><TT>&nbsp;dv = DeleteCases[DownValues[Subscript], (b_ :> _) /; FreeQ[b,
HoldPattern[Subscript[x, _]]]];</TT>
<BR><TT>&nbsp;Information[x];</TT>
<BR><TT>&nbsp;PrintDownValue /@ dv;</TT>
<BR><TT>]</TT><TT></TT>

<P><TT>Clear[PrintDownValue]</TT>
<BR><TT>PrintDownValue[Verbatim[HoldPattern][a_] :> b_] := Print[HoldForm[a
= b]]</TT><TT></TT>

<P>And some tests:<TT></TT>

<P><TT>In[164]:=</TT>
<BR><TT>Subscript[x,1]=1;</TT>
<BR><TT>Subscript[x,a_Integer]:=a^2</TT>
<BR><TT>Subscript[y,2]=2;</TT>
<BR><TT>y=2;</TT><TT></TT>

<P><TT>In[166]:=</TT>
<BR><TT>SubscriptInformation[x]</TT><TT></TT>

<P><TT>From In[166]:=</TT>
<BR><TT>Global`x</TT>
<BR><TT>x&nbsp; = 1</TT>
<BR><TT>&nbsp;1</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2</TT>
<BR><TT>x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = a</TT>
<BR><TT>&nbsp;a_Integer</TT><TT></TT>

<P><TT>In[167]:=</TT>
<BR><TT>SubscriptInformation[y]</TT><TT></TT>

<P><TT>From In[167]:=</TT>
<BR><TT>Global`y</TT>
<BR><TT>y = 2</TT>
<BR><TT>y&nbsp; = 2</TT>
<BR><TT>&nbsp;2</TT><TT></TT>

<P>So what do you think?&nbsp;Are there any major problems with the above
ideas?

<P>Carl Woll
<BR>Dept of Physics
<BR>U of Washington
</BODY>
</HTML>

```

• Prev by Date: incompatibilities between releases of Mathematica (was: Mathematica Link for Excel and Excel 2000)