Re: Chain Matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg96591] Re: Chain Matrix
• From: WetBlanket <Wyvern864 at gmail.com>
• Date: Mon, 16 Feb 2009 16:41:42 -0500 (EST)
• References: <gn3bvk\$q50\$1@smc.vnet.net> <gn622b\$ied\$1@smc.vnet.net>

```On Feb 14, 3:14 am, Jean-Marc Gulliet <jeanmarc.gull... at gmail.com>
wrote:
> In article <gn3bvk\$q5... at smc.vnet.net>,
>  Antonio Carlos Siqueira <antoniocslim... at gmail.com> wrote:
>
>
>
> > I have a matrix in which all the elements are function of a scalar h,
> > Say q[h]={{q1,q2},{q3,q4}} and what I need is to create a chain matri=
x
> > so I can obtain the dot product of several q[h]. For instance, if  h=
=
> > {h1,h2,h3}
> > I can create a large matrix with
> > m=Map[q,h]
> > Then I do
> > Dot[m[[1]],m[[2],m[[3] ]
>
> > The problem is that there are cases where the Length[h] may reach a
> > few hundreds and I do not want to write Dot[m[[1]], ..., m[[200]] ]
> > So  I have a somewhat trivial question:
> >  Is there any way I can do this automatically? I mean how can I creat=
e
> > a function to create an equivalent matrix from a chain of matrices?
> > I tried some solutions with thread and fold, but I did not get
> > anywhere, any thoughts are appreciated.
>
> If your matrices are stored within a list, as suggested by the syntax
> m[[1]], etc., your could use either
>
>     Dot[Sequence @@ m]
>
> or
>
>     Fold[#1.#2 &, First[m], Rest[m]]
>
> for instance.
>
> In[1]:= m = Array[#, {2, 2}] & /@ {h1, h2, h3}
>
> Out[1]= {{{h1[1, 1], h1[1, 2]}, {h1[2, 1], h1[2, 2]}}, {{h2[1, 1],
>    h2[1, 2]}, {h2[2, 1], h2[2, 2]}}, {{h3[1, 1], h3[1, 2]}, {h3[2, 1]=
,
>     h3[2, 2]}}}
>
> In[2]:= Dot[m[[1]], m[[2]], m[[3]]]
>
> Out[2]= {{(h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      1] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      1], (h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      2] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      2]}, {(h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      1] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2,
>      1], (h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      2] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2, 2]}}
>
> In[3]:= Dot[Sequence @@ m]
>
> Out[3]= {{(h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      1] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      1], (h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      2] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      2]}, {(h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      1] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2,
>      1], (h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      2] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2, 2]}}
>
> In[4]:= % === %%
>
> Out[4]= True
>
> In[5]:= Fold[#1.#2 &, First[m], Rest[m]]
>
> Out[5]= {{(h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      1] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      1], (h1[1, 1] h2[1, 1] + h1[1, 2] h2[2, 1]) h3[1,
>      2] + (h1[1, 1] h2[1, 2] + h1[1, 2] h2[2, 2]) h3[2,
>      2]}, {(h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      1] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2,
>      1], (h1[2, 1] h2[1, 1] + h1[2, 2] h2[2, 1]) h3[1,
>      2] + (h1[2, 1] h2[1, 2] + h1[2, 2] h2[2, 2]) h3[2, 2]}}
>
> In[6]:= % === %%%
>
> Out[6]= True
>
> Regards,
> --Jean-Marc

Please refer to the book "Power Programming with Mathematica" by David
Wagner on page 169.

Gary

```

• Prev by Date: Mathematica Training in Amsterdam
• Next by Date: Re: Length of a held expression
• Previous by thread: Re: Chain Matrix
• Next by thread: Superscript in text