Re: Variables names
- To: mathgroup at smc.vnet.net
- Subject: [mg15029] Re: [mg15002] Variables names
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Wed, 9 Dec 1998 04:12:16 -0500
- Sender: owner-wri-mathgroup at wolfram.com
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/