       Re: Indexed variables

• To: mathgroup at smc.vnet.net
• Subject: [mg24702] Re: Indexed variables
• From: Hartmut Wolf <hwolf at debis.com>
• Date: Fri, 4 Aug 2000 01:19:27 -0400 (EDT)
• Organization: debis Systemhaus
• References: <8lsup5\$26a@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Barbara Da Vinci schrieb:
>
> Hello, MathGroup
> Is there anybody who knows as to thrust indexed
> variables in Mathematica without assigning them a
> value ? I need a[] ... a[] to be real headed
> expressions (variables, if you like) present in some
> equations as unknowns.
> I tried a=Table[0,{k,1,8}] but it hampers me because
> of zero value.

Ciao Barbara,

it's not clear to me what you what to achieve, perhaps the following might be
overly complex, yet I hope to give you some help.

(1) First, although you could define

In:= s /: Head[s] = Real

for a symbol s, but then f[s] still won't match a definition for _Real

In:= f[_Real] := "real argument"
In:= f[_?NumberQ] := "numeric argument"
In:= f[_Symbol] := "symbol as argument"
In:= f[_] := "something different"

In:= f[s]
Out= "symbol as argument"

However if you define

In:= s /: NumberQ[s] = True

then

In:= f[s]
Out= "numeric argument"

So you have to set up your definitions accordingly.

(2) Second, expressions like a[] cannot be made to variables in the sense of
Mathematica symbols (although in some circumstances, e.g. with differentiation,
the may appear as variables in a mathematical sense).

However, you may make the expressions a[], etc. refer to symbols.

In:= a = Table[ToExpression["a" <> ToString[i]], {i, 8}]
Out= {a1, a2, a3, a4, a5, a6, a7, a8}

In many circumstances you can just use a[[i]]. But if you want to assign a value
to the variable, e.g. referred by a[], then you have to evaluate the lhs of
Set (else the value would be assigned to a[] itself, not to the referred a5.

In:= {Evaluate[a[]] = Pi, a5}
Out= {\[Pi], \[Pi]}

Yet you can't repeat the game

In:= {Evaluate[a[]] = 2 Pi, a5}
>From In:=
Set::"wrsym": "Symbol \!\(\[Pi]\) is Protected."
Out= {2 \[Pi], \[Pi]}

On meta-level programming however you still can redefine a5 (with programmed
indexed reference):

In:= i = 5;
{ToExpression[RowBox[{"a" <> ToString[i], "=", "4 Pi"}]], a5}
Out= {4 \[Pi], 4 \[Pi]}

In:= Clear[a]

(3) Perhaps a pretty way would be to use the Notation package and work with
subscripted expressions (which *can* be made to symbols)

In:= << Utilities`Notation`
In:= On[General::"newsym"]
In:= Off[General::"stop", General::"spell1"]

If you now want to Symbolize expressions a\_i in a programmed fashion, you also
have to do that at meta-level:

In:=
Do[Symbolize[
ToExpression[
TagBox[SubscriptBox["a", ToString[j]], NotationBoxTag,
TagStyle -> "NotationTemplateStyle"]]], {j, 1, 8}]
>From In:= General::"newsym": "Symbol
\!\(a\[UnderBracket]Subscript\[UnderBracket]1\) is new."
... et cetera, until ...
>From In:= General::"newsym": "Symbol
\!\(a\[UnderBracket]Subscript\[UnderBracket]8\) is new."

The a\[UnderBracket]Subscript\[UnderBracket]1 etc. are then the "real" symbols
(which are referred to by a\_1 i.e. Subscript[a,1] etc.)

Define the upvalues:

In:=
Do[With[{sym = ToExpression[SubscriptBox["a", ToString[j]]]},
TagSet[sym, NumberQ[sym], True]], {j, 1, 8}]

Test:

In:= \!\(NumberQ\  /@ \ {a\_7, a\_8, a\_9, a\_10}\)
>From In:= General::"newsym": "Symbol \!\(a\) is new."
Out= {True, True, False, False}

You see, here it was only the reference to Subscript[a,9] -- which had not been
set up as a symbol -- that generated the symbol a.

Now, like before, you could define

In:=
a = Table[ToExpression[SubscriptBox["a", ToString[j]]], {j, 1, 8}]
Out=
\!\({a\_1, a\_2, a\_3, a\_4, a\_5, a\_6, a\_7, a\_8}\)

and work through a[[i]]

In:= \!\({Evaluate[a[\(\)]] = Pi, a\_5}\)
Out= {\[Pi], \[Pi]}

(To input line 13 write "{Evaluate[a[] = Pi, [#]}" and at the place indicated
here by "[#]" input subscripted a through the palette)

Kind regards,
Hartmut Wolf

```

• Prev by Date: RE: Re: Manipulating Slot objects in Compile
• Next by Date: RE: Re: With[{software=Mathematica}, Frustration]
• Previous by thread: Re: Indexed variables
• Next by thread: Mathematica won't solve simple diff. eqn. system