Re: Another AppendTo replacement problem
- To: mathgroup at smc.vnet.net
- Subject: [mg118218] Re: Another AppendTo replacement problem
- From: ADL <alberto.dilullo at tiscali.it>
- Date: Sun, 17 Apr 2011 07:51:20 -0400 (EDT)
- References: <iobv13$bfp$1@smc.vnet.net>
On 16 Apr, 13:37, Iv=E1n Lazaro <gamins... at gmail.com> wrote: > I made a mistake in the code. Now it's fine. Sorry. > > NumBasis = 10000; > q = matrA = ma = Table[0, {i, 2}]; > M = RandomComplex[{-1 - I, 1 + I}, {NumBasis, 2, 2}]; > M = Map[Orthogonalize, M]; > matr = RandomComplex[{-1 - I, 1 + I}, {2, 2}] > Results = {}; > > Do[{ma[[k]] = > KroneckerProduct[M[[Nbase, k]], Conjugate[M[[Nbase, k]]]]; > matrA[[k]] = Chop[matr.ma[[k]]]; > matrA[[k]] = matrA[[k]]/Tr[matrA[[k]].matrA[[k]]] // Chop; > If[k == 2, > AppendTo[ > Results, {M[[Nbase]], Total[Eigenvalues[matrA[[k]]]]}]]; > }, {Nbase, 1, NumBasis}, {k, 1, 2}]; > > M = Sort[Results, #1[[2]] < #2[[2]] &][[1, 1]]; > > Thanks in advance! You might try with Reap and Sow. In my system it is four/five times faster: reapVersion := ( NumBasis = 10000; q = matrA = ma = Table[0, {i, 2}]; M = RandomComplex[{-1 - I, 1 + I}, {NumBasis, 2, 2}]; M = Orthogonalize /@ M; matr = RandomComplex[{-1 - I, 1 + I}, {2, 2}]; Results = Last[Reap[ Do[{ma[[k]] = KroneckerProduct[M[[Nbase,k]], Conjugate[M[[Nbase,k]]]]; matrA[[k]] = Chop[matr . ma[[k]]]; matrA[[k]] = Chop[matrA[[k]]/Tr[matrA[[k]] . matrA[[k]]]]; If[k == 2, Sow[{M[[Nbase]], Total[Eigenvalues[ matrA[[k]]]]}]]; }, {Nbase, 1, NumBasis}, {k, 1, 2}] ]]; M=Sort[Results][[1,1]]; M )