Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1739

Re: Another AppendTo replacement problem

From ADL <alberto.dilullo@tiscali.it>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Another AppendTo replacement problem
Date 2011-04-17 11:51 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ioek74$mts$1@smc.vnet.net> (permalink)
References <iobv13$bfp$1@smc.vnet.net>

Show all headers | View raw


On 16 Apr, 13:37, Iv=E1n Lazaro <gamins...@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
)

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Another AppendTo replacement problem Iván Lazaro <gaminster@gmail.com> - 2011-04-16 11:37 +0000
  Re: Another AppendTo replacement problem ADL <alberto.dilullo@tiscali.it> - 2011-04-17 11:51 +0000
  Re: Another AppendTo replacement problem "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-04-17 11:51 +0000
    Re: Another AppendTo replacement problem Ray Koopman <koopman@sfu.ca> - 2011-04-17 23:15 +0000

csiph-web