Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #1719 > unrolled thread
| Started by | Iván Lazaro <gaminster@gmail.com> |
|---|---|
| First post | 2011-04-16 11:36 +0000 |
| Last post | 2011-04-17 11:53 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Another AppendTo replacement problem Iván Lazaro <gaminster@gmail.com> - 2011-04-16 11:36 +0000
Re: Another AppendTo replacement problem Peter Pein <petsie@dordos.net> - 2011-04-17 11:54 +0000
Re: Another AppendTo replacement problem Albert Retey <awnl@gmx-topmail.de> - 2011-04-17 11:53 +0000
| From | Iván Lazaro <gaminster@gmail.com> |
|---|---|
| Date | 2011-04-16 11:36 +0000 |
| Subject | Another AppendTo replacement problem |
| Message-ID | <iobuv2$bec$1@smc.vnet.net> |
Hi dear group!
I've read multiple times in this forum about the slow performance of
AppendTo with big lists. I have now a problem with it, but have not
been able to replace it properly. This is a toy version of my problem.
The code below have to be run multiple times, but it is really slow. I
wonder if somebody have an idea about this AppendTo problem.
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]], Eigenvalues[matrA[[k]]], {k, 1, 2}}]];
}, {Nbase, 1, NumBasis}, {k, 1, 2}];
M = Sort[Results, #1[[2]] < #2[[2]] &][[1, 1]];
Thanks in advance!.
[toc] | [next] | [standalone]
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Date | 2011-04-17 11:54 +0000 |
| Message-ID | <ioekco$n27$1@smc.vnet.net> |
| In reply to | #1719 |
Am 16.04.2011 13:36, schrieb Iván Lazaro:
> Hi dear group!
>
> I've read multiple times in this forum about the slow performance of
> AppendTo with big lists. I have now a problem with it, but have not
> been able to replace it properly. This is a toy version of my problem.
> The code below have to be run multiple times, but it is really slow. I
> wonder if somebody have an idea about this AppendTo problem.
>
>
> 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]], Eigenvalues[matrA[[k]]], {k, 1, 2}}]];
> }, {Nbase, 1, NumBasis}, {k, 1, 2}];
>
> M = Sort[Results, #1[[2]]< #2[[2]]&][[1, 1]];
>
> Thanks in advance!.
>
Hi Iván,
there is no need to build a list while calculating, because there is
already M. With a few nested anonymous functions and heavy mapping:
NumBasis=10^4;
SeedRandom[1]; (* to make result comparable *)
matr=RandomComplex[{-1-I,1+I},{2,2}];
M=RandomComplex[{-1-I,1+I},{NumBasis,2,2}];
AbsoluteTiming[
M=Orthogonalize/@M;
First[Results=
SortBy[
Transpose[{M,
(Total[Eigenvalues[#1]]&) /@
Map[(#1/Tr[#1.#1]&)[matr.#1]&,
MapThread[KroneckerProduct,{M,Conjugate[M]},2],
{2}][[All,2]]
}],
Abs[#1[[2]]]&]
]
]
gives
{0.4700006,
{
{{ 0.552668 - 0.232809 I, -0.550734 - 0.58056 I},
{-0.760505 + 0.248977 I, -0.375566 - 0.467538 I}},
0.297748 + 0.654164 I
} }
hth,
Peter
[toc] | [prev] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-04-17 11:53 +0000 |
| Message-ID | <ioekbn$n1e$1@smc.vnet.net> |
| In reply to | #1719 |
Hi,
I've seen your other post but for me the code in the first worked while
the code in the second gave me a lot of error message. Since I think the
difference might not be relevant for the AppendTo-problem, I'm just
using the code in the first post...
> I've read multiple times in this forum about the slow performance of
> AppendTo with big lists. I have now a problem with it, but have not
> been able to replace it properly. This is a toy version of my problem.
> The code below have to be run multiple times, but it is really slow. I
> wonder if somebody have an idea about this AppendTo problem.
You can use a well known trick to first build your list as something
that performs like a linked list and than use Flatten to get the form
you actually want. Since the entries in your list are lists, I'm using a
different Head, but the rest can be found in many other posts:
NumBasis = 10000;
M = RandomComplex[{-1 - I, 1 + I}, {NumBasis, 2, 2}];
M = Map[Orthogonalize, M];
matr = RandomComplex[{-1 - I, 1 + I}, {2, 2}]
Timing[
q = matrA = ma = Table[0, {i, 2}]; results2 = list[];
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,
results2 =
list[results2, {M[[Nbase]], Eigenvalues[matrA[[k]]], {k, 1, 2}}]
],
{Nbase, 1, NumBasis}, {k, 1, 2}
];
results2 = List @@ Flatten[results2];
]
actually I think something like this would probably be somewhat easier
to understand, but it seems to be slightly slower (but still a lot
faster than the AppendTo-Version):
Timing[
q = matrA = ma = Table[0, {i, 2}];
results3 = Flatten[Table[
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,
{M[[Nbase]], Eigenvalues[matrA[[k]]], {k, 1, 2}},
Unevaluated[Sequence[]]
],
{Nbase, 1, NumBasis}, {k, 1, 2}
], 1];
]
hth,
albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web