Re: Sums vs Map efficiency

• To: mathgroup at smc.vnet.net
• Subject: [mg7753] Re: Sums vs Map efficiency
• From: Stephen P Luttrell <luttrell at signal.dra.hmg.gb>
• Date: Mon, 7 Jul 1997 04:41:24 -0400
• Organization: Defence Research Agency
• Sender: owner-wri-mathgroup at wolfram.com

```> Does anyone know how to code the following
> in a tensor (more efficient than sums) form ?
>
> here A1 is a (n,n,n)tensor and (A1,A2,a) are  n*n matrices
>
> gm=Table[
>  Sum[ A1[[i,j,k]]*
>  A2[[i1,k1]]*
> a[[i,i1]]* a[[j,j1]]
> ,{i1,n},{j1,n},{i,n},{j,n}]
> ,{k,n},{k1,n}]//Flatten;
>
> For some reason in that form it seems to take quite a long time to
> process gm. (here a is symbolic a= {{a11,a12...} ... ann}} )
>
> What I am after is an expression involving Map and Thread and
> Transpose  ... ?

Try the following (where U is a vector of 1's):

((Transpose[a].(A1.U).(a.U))(A2.U)).U

This works step-by-step as follows:

1. (A1.U) evaluates Sum[A1[[i,j,k]],{k,n}]
2. (a.U) evaluates Sum[a[[j,j1]],{j1,n}]
3. (A1.U).(a.U) evaluates Sum[(1).(2),{j,n}]
4. (Transpose[a].(A1.U).(a.U) evaluates Sum[a[[i,i1]]*(3),{i,n}]
5. (A2.U) evaluates Sum[A2[[i1,k1]],{k1,n}]
6. (Transpose[a].(A1.U).(a.U))(A2.U) evaluates (4) (5)
7. ((Transpose[a].(A1.U).(a.U))(A2.U)).U evaluates Sum[(6),{i1,n}]

Here is the n=2 case in practice (I did this in Mathematica 3.0):

The input is:

n=2;
U=Table[1,{n}];
A1=Array[\[Alpha]1,{n,n,n}];
A2=Array[\[Alpha]2,{n,n}];
a=Array[\[Alpha],{n,n}];
((Transpose[a].(A1.U).(a.U))(A2.U)).U

which produces the following output:

((\[Alpha][1,1]+\[Alpha][1,2]) (
\[Alpha][1,1] (\[Alpha]1[1,1,1]+\[Alpha]1[1,1,2])+
\[Alpha][2,1] (\[Alpha]1[2,1,1]+\[Alpha]1[2,1,2]))+(
\[Alpha][2,1]+\[Alpha][2,2]) (
\[Alpha][1,1] (\[Alpha]1[1,2,1]+\[Alpha]1[1,2,2])+
\[Alpha][2,1] (\[Alpha]1[2,2,1]+\[Alpha]1[2,2,2]))) (
\[Alpha]2[1,1]+
\[Alpha]2[1,
2])+((\[Alpha][1,1]+\[Alpha][1,2]) (
\[Alpha][1,2] (\[Alpha]1[1,1,1]+\[Alpha]1[1,1,2])+
\[Alpha][2,2] (\[Alpha]1[2,1,1]+\[Alpha]1[2,1,2]))+(
\[Alpha][2,1]+\[Alpha][2,2]) (
\[Alpha][1,2] (\[Alpha]1[1,2,1]+\[Alpha]1[1,2,2])+
\[Alpha][2,2] (\[Alpha]1[2,2,1]+\[Alpha]1[2,2,2]))) (
\[Alpha]2[2,1]+\[Alpha]2[2,2])

If I wrap the last step in Timing it returns 0.01 second on my Pentium
90 PC.

========================================================================

Dr Stephen P Luttrell                  luttrell at signal.dra.hmg.gb