Re: How to "unflatten"?

*To*: mathgroup at smc.vnet.net*Subject*: [mg83909] Re: How to "unflatten"?*From*: dh <dh at metrohm.ch>*Date*: Tue, 4 Dec 2007 04:24:21 -0500 (EST)*References*: <fj0rlv$pff$1@smc.vnet.net>

Hi Hauke, here is an example. We create a 2x2x2x2 tensor,destroy its structure and restructure it again: t1=Array[a,{2,2,2,2}] t=Flatten[t1,3] t2=Nest[Partition[#,2]&,t,3] t1==t2 Further, we contract the first with the last index: Sum[arr[[i, All, All, i]], {i, 2}] hope this helps, Daniel Hauke Reddmann wrote: > Excuse a complete noob's question: > > I do calculations with S matrices and need to convert > the tensor form Sab_cd (which would be e.g. the nested > list {{{{,},{,}},{{,},{,}}},{{{,},{,}},{{,},{,}}}} - > I omitted variables a1-a16, since only the structure > of the list, a 2*2*2*2 nest, is relevant) into a > matrix Mab_cd: {{,,,},{,,,},{,,,},{,,,}}. That is trivial: > Mab_cd=Partition[Flatten[Sab_cd],4]. > > But how to reverse the process? Of course even I already > can write a quadruple loop S[[,,,]]=M[,] with direct > handover of elements, but that is so unelegant, especially > as I have to apply this a hundred times in the computation > (and can't write subroutines yet, I'm a noob after all :-) > > Question 2: I could skip the whole converting if I knew > how to do an Einstein sum over two indices inside a tensor: > Sab_cd -> Sab_ca -> sum(a=1,n,Sab_ca) -> Tb_c. > At the moment I do this whith "blocking" multiple indices > into a matrix and then do the matrix product, but this is > more a clever hack.