[Date Index]
[Thread Index]
[Author Index]
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[[1]] ... a[[8]] 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[1001]:= s /: Head[s] = Real
for a symbol s, but then f[s] still won't match a definition for _Real
In[1002]:= f[_Real] := "real argument"
In[1003]:= f[_?NumberQ] := "numeric argument"
In[1004]:= f[_Symbol] := "symbol as argument"
In[1005]:= f[_] := "something different"
In[1006]:= f[s]
Out[1006]= "symbol as argument"
However if you define
In[1007]:= s /: NumberQ[s] = True
then
In[1008]:= f[s]
Out[1008]= "numeric argument"
So you have to set up your definitions accordingly.
(2) Second, expressions like a[[1]] 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[[1]], etc. refer to symbols.
In[2001]:= a = Table[ToExpression["a" <> ToString[i]], {i, 8}]
Out[2001]= {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[[5]], then you have to evaluate the lhs of
Set (else the value would be assigned to a[[5]] itself, not to the referred a5.
In[2002]:= {Evaluate[a[[5]]] = Pi, a5}
Out[2002]= {\[Pi], \[Pi]}
Yet you can't repeat the game
In[2003]:= {Evaluate[a[[5]]] = 2 Pi, a5}
>From In[2003]:=
Set::"wrsym": "Symbol \!\(\[Pi]\) is Protected."
Out[2003]= {2 \[Pi], \[Pi]}
On meta-level programming however you still can redefine a5 (with programmed
indexed reference):
In[2004]:= i = 5;
{ToExpression[RowBox[{"a" <> ToString[i], "=", "4 Pi"}]], a5}
Out[2004]= {4 \[Pi], 4 \[Pi]}
In[2005]:= 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[2]:= << Utilities`Notation`
In[3]:= On[General::"newsym"]
In[4]:= 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[6]:=
Do[Symbolize[
ToExpression[
TagBox[SubscriptBox["a", ToString[j]], NotationBoxTag,
TagStyle -> "NotationTemplateStyle"]]], {j, 1, 8}]
>From In[6]:= General::"newsym": "Symbol
\!\(a\[UnderBracket]Subscript\[UnderBracket]1\) is new."
... et cetera, until ...
>From In[6]:= 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[10]:=
Do[With[{sym = ToExpression[SubscriptBox["a", ToString[j]]]},
TagSet[sym, NumberQ[sym], True]], {j, 1, 8}]
Test:
In[11]:= \!\(NumberQ\ /@ \ {a\_7, a\_8, a\_9, a\_10}\)
>From In[11]:= General::"newsym": "Symbol \!\(a\) is new."
Out[11]= {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[12]:=
a = Table[ToExpression[SubscriptBox["a", ToString[j]]], {j, 1, 8}]
Out[12]=
\!\({a\_1, a\_2, a\_3, a\_4, a\_5, a\_6, a\_7, a\_8}\)
and work through a[[i]]
In[13]:= \!\({Evaluate[a[\([5]\)]] = Pi, a\_5}\)
Out[13]= {\[Pi], \[Pi]}
(To input line 13 write "{Evaluate[a[[5]] = 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**
| |