Re: Variables names
- To: mathgroup at smc.vnet.net
- Subject: [mg15086] Re: Variables names
- From: Leszek Sczaniecki <lsczan at concentric.net>
- Date: Thu, 10 Dec 1998 03:13:15 -0500
- Organization: Concentric Internet Services
- References: <74ld6o$sdk@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Way to complicated. Try this M = Table[ToExpression["A"<>ToString[i]<>ToString[j]], {i, 5}, {j, 5}]; X = Table[ToExpression["x"<>ToString[i]], {i, 5}]; X.M.X // Expand -Leszek Andrzej Kozlowski wrote: > On Sat, Dec 5, 1998, Kim Allemand <kim.allemand at epfl.ch> wrote: > > >Hi, > >I have a square matrix Q of dimension N and would like to generate the > >quadratic function $\sum_{i,j}q_{ij}x_i x_j$. The problem is how to say > >to Mathamatica that the variables names are x1, x2, etc. Is it possible > >to generate variables names in an automatic way? A possibility would be > >to generate manually a vector NN={x1, x2, x3, ....} of dimension N and > >then to pick NN[[i]] for the ith variable name, but imagine when > >N=1000... !! > >Thanks, Kim > > > > Well, if you don't mind rather messy notation (x111 for x_3 etc) than > the following will do it: > > reindex[i_]:=ToExpression[ToString[i]<>"1"] reindex[i_,0]:=i > reindex[i_,n_]:=reindex[reindex[i,n-1]] > indexlist[a_,n_]:=NestList[reindex,reindex[a],n-1] > > quadratic[q_,x_,n_]:= > > Flatten[Outer[Times,indexlist[x,n],indexlist[x,n]]].Flatten[Array[q,{n,n}]] > > In[32]:= > quadratic[q,x,6] > > Out[32]= > 2 > x1 q[1, 1] + x1 x11 q[1, 2] + x1 x111 q[1, 3] + > > x1 x1111 q[1, 4] + x1 x11111 q[1, 5] + > > 2 > x1 x111111 q[1, 6] + x1 x11 q[2, 1] + x11 q[2, 2] + > > x11 x111 q[2, 3] + x11 x1111 q[2, 4] + > > x11 x11111 q[2, 5] + x11 x111111 q[2, 6] + > > 2 > x1 x111 q[3, 1] + x11 x111 q[3, 2] + x111 q[3, 3] + > > x111 x1111 q[3, 4] + x111 x11111 q[3, 5] + > > x111 x111111 q[3, 6] + x1 x1111 q[4, 1] + > > 2 > x11 x1111 q[4, 2] + x111 x1111 q[4, 3] + x1111 q[4, 4] + > > x1111 x11111 q[4, 5] + x1111 x111111 q[4, 6] + > > x1 x11111 q[5, 1] + x11 x11111 q[5, 2] + > > x111 x11111 q[5, 3] + x1111 x11111 q[5, 4] + > > 2 > x11111 q[5, 5] + x11111 x111111 q[5, 6] + > > x1 x111111 q[6, 1] + x11 x111111 q[6, 2] + > > x111 x111111 q[6, 3] + x1111 x111111 q[6, 4] + > > 2 > x11111 x111111 q[6, 5] + x111111 q[6, 6] > > Andrzej Kozlowski > Toyama International University > JAPAN > http://sigma.tuins.ac.jp/ --------------52E3342EADC96964CB461EBB Content-Type: text/html; charset="us-ascii" <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <tt>Way to complicated. Try this</tt><tt></tt> <p><tt>M = Table[ToExpression["A"<>ToString[i]<>ToString[j]], {i, 5}, {j, 5}];</tt> <br><tt>X = Table[ToExpression["x"<>ToString[i]], {i, 5}];</tt> <br><tt>X.M.X // Expand</tt><tt></tt> <p><tt>-Leszek</tt><tt></tt> <p>Andrzej Kozlowski wrote: <blockquote TYPE=CITE>On Sat, Dec 5, 1998, Kim Allemand <kim.allemand at epfl.ch> wrote: <p>>Hi, <br>>I have a square matrix Q of dimension N and would like to generate the <br>>quadratic function $\sum_{i,j}q_{ij}x_i x_j$. The problem is how to say <br>>to Mathamatica that the variables names are x1, x2, etc. Is it possible <br>>to generate variables names in an automatic way? A possibility would be <br>>to generate manually a vector NN={x1, x2, x3, ....} of dimension N and <br>>then to pick NN[[i]] for the ith variable name, but imagine when <br>>N=1000... !! <br>>Thanks, Kim <br>> <p>Well, if you don't mind rather messy notation (x111 for x_3 etc) than <br>the following will do it: <p>reindex[i_]:=ToExpression[ToString[i]<>"1"] reindex[i_,0]:=i <br>reindex[i_,n_]:=reindex[reindex[i,n-1]] <br>indexlist[a_,n_]:=NestList[reindex,reindex[a],n-1] <p>quadratic[q_,x_,n_]:= <p>Flatten[Outer[Times,indexlist[x,n],indexlist[x,n]]].Flatten[Array[q,{n,n}]] <p>In[32]:= <br>quadratic[q,x,6] <p>Out[32]= <br> 2 <br>x1 q[1, 1] + x1 x11 q[1, 2] + x1 x111 q[1, 3] + <p> x1 x1111 q[1, 4] + x1 x11111 q[1, 5] + <p> 2 <br> x1 x111111 q[1, 6] + x1 x11 q[2, 1] + x11 q[2, 2] + <p> x11 x111 q[2, 3] + x11 x1111 q[2, 4] + <p> x11 x11111 q[2, 5] + x11 x111111 q[2, 6] + <p> 2 <br> x1 x111 q[3, 1] + x11 x111 q[3, 2] + x111 q[3, 3] + <p> x111 x1111 q[3, 4] + x111 x11111 q[3, 5] + <p> x111 x111111 q[3, 6] + x1 x1111 q[4, 1] + <p> 2 <br> x11 x1111 q[4, 2] + x111 x1111 q[4, 3] + x1111 q[4, 4] + <p> x1111 x11111 q[4, 5] + x1111 x111111 q[4, 6] + <p> x1 x11111 q[5, 1] + x11 x11111 q[5, 2] + <p> x111 x11111 q[5, 3] + x1111 x11111 q[5, 4] + <p> 2 <br> x11111 q[5, 5] + x11111 x111111 q[5, 6] + <p> x1 x111111 q[6, 1] + x11 x111111 q[6, 2] + <p> x111 x111111 q[6, 3] + x1111 x111111 q[6, 4] + <p> 2 <br> x11111 x111111 q[6, 5] + x111111 q[6, 6] <p>Andrzej Kozlowski <br>Toyama International University <br>JAPAN <br><a href="http://sigma.tuins.ac.jp/">http://sigma.tuins.ac.jp/</a></blockquote> </html> --------------52E3342EADC96964CB461EBB--