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


Groups > comp.soft-sys.math.mathematica > #1757 > unrolled thread

Re: Another AppendTo replacement problem

Started byBill Rowe <readnews@sbcglobal.net>
First post2011-04-17 11:52 +0000
Last post2011-04-20 08:28 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Another AppendTo replacement problem Bill Rowe <readnews@sbcglobal.net> - 2011-04-17 11:52 +0000
    Re: Another AppendTo replacement problem Ray Koopman <koopman@sfu.ca> - 2011-04-20 08:28 +0000

#1757 — Re: Another AppendTo replacement problem

FromBill Rowe <readnews@sbcglobal.net>
Date2011-04-17 11:52 +0000
SubjectRe: Another AppendTo replacement problem
Message-ID<ioek97$mvg$1@smc.vnet.net>
On 4/16/11 at 7:36 AM, gaminster@gmail.com (Iv=C3=A1n Lazaro) 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]];

Here is one way to avoid AppendTo

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,
     Results = {Results, {M[[Nbase]],
        Total[Eigenvalues[matrA[[k]]]]}}];}, {Nbase, 1,
NumBasis}, {k,
    1, 2}];

M = SortBy[Partition[Flatten@Results, 2], Last][[1, 1]];

Here, I am saving the results as a nested list. Since you know
each loop adds a two element list, the desired array can be
created by partitioning the flattened nested list easily. The
end result will be the same but execute much faster.

[toc] | [next] | [standalone]


#1795

FromRay Koopman <koopman@sfu.ca>
Date2011-04-20 08:28 +0000
Message-ID<iom5g5$c$1@smc.vnet.net>
In reply to#1757
On Apr 18, 6:50 am, Bill Rowe <readn...@sbcglobal.net> wrote:
> The results I get indicate simply using nested lists then using
> Partition to reconstruct the array appropriately is faster. That is:
>
> In[1]:= Timing[z = h[]; Do[z = h[z, {i, -i}], {i, 1*^6}]; Length@z]
> Timing[Length[zz = List @@ Flatten[z, Infinity, h]]]
>
> Out[1]= {1.6739,2}
>
> Out[2]= {0.622828,1000000}
>
> In[3]:= Timing[s = {}; Do[s = {s, {i, -i}}, {i, 1*^6}]; Length@z]
> Timing[Length[ss = Partition[Flatten[s], Length[Last@s]]]]
>
> Out[3]= {1.61893,2}
>
> Out[4]= {0.392044,1000000}
>
> In[5]:= ss == zz
>
> Out[5]= True

The timing seems to be version-dependent.

In[1]:= {$Version, $ReleaseNumber}
Out[1]= {5.2 for Mac OS X (June 20, 2005), 0}

In[2]:= Timing[z = h[]; Do[z = h[z, {i, -i}], {i, 1*^6}]; Length@z]
        Timing[Length[zz = List @@ Flatten[z, Infinity, h]]]

Out[2]= {4.24 Second,2}
Out[3]= {0.74 Second,1000000}

In[4]:= Timing[s = {}; Do[s = {s, {i, -i}}, {i, 1*^6}]; Length@s]
        Timing[Length[ss = Partition[Flatten[s], Length[Last@s]]]]

Out[4]= {3.96 Second,2}
Out[5]= {1.2 Second,1000000}

In[6]:= ss == zz
Out[6]= True

In[1]:= {$Version, $ReleaseNumber}
Out[1]= {6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007), 1}

In[2]:= Timing[z = h[]; Do[z = h[z, {i, -i}], {i, 1*^6}]; Length@z]
        Timing[Length[zz = List @@ Flatten[z, Infinity, h]]]

Out[2]= {5.36, 2}
Out[3]= {0.76, 1000000}

In[4]:= Timing[s = {}; Do[s = {s, {i, -i}}, {i, 1*^6}]; Length@s]
        Timing[Length[ss = Partition[Flatten[s], Length[Last@s]]]]

Out[4]= {5.24, 2}
Out[5]= {1.17, 1000000}

In[6]:= ss == zz
Out[6]= True

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web