Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #13580 > unrolled thread
| Started by | firlefranz <cornelius.franz@gmx.net> |
|---|---|
| First post | 2013-02-23 12:01 +0000 |
| Last post | 2013-03-05 09:15 +0000 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Compiling numerical iterations firlefranz <cornelius.franz@gmx.net> - 2013-02-23 12:01 +0000
Re: Compiling numerical iterations Ray Koopman <koopman@sfu.ca> - 2013-02-24 04:31 +0000
Re: Compiling numerical iterations firlefranz <cornelius.franz@gmx.net> - 2013-02-26 06:09 +0000
Re: Compiling numerical iterations Ray Koopman <koopman@sfu.ca> - 2013-02-27 08:04 +0000
Re: Compiling numerical iterations David Bailey <dave@removedbailey.co.uk> - 2013-03-01 02:25 +0000
Re: Compiling numerical iterations Peter Klamser <klamser@googlemail.com> - 2013-02-27 08:04 +0000
Re: Compiling numerical iterations Peter Klamser <klamser@googlemail.com> - 2013-03-05 09:15 +0000
| From | firlefranz <cornelius.franz@gmx.net> |
|---|---|
| Date | 2013-02-23 12:01 +0000 |
| Subject | Compiling numerical iterations |
| Message-ID | <kgab2i$c7f$1@smc.vnet.net> |
Hi,
to speed up some calculations I'd like to use this relatively new method of compiling equations or export them to C. But since I'm very new in these things, I have really problems to use this c-compiling function.
Can someone show me on this simple part of code (calculation of a correlation function of a 1D random walk), how it should be implemented?
num = 1000;
tab = Table[0, {i, 1, num}];
corr = Table[0, {i, 1, num/10}];
For[l = 1, l <= 1000, l++,
a = 0;
For[i = 1, i <= num, i++,
tab[[i]] = tab[[i]] + a/10;
If[RandomReal[{-1, 1}] < 0, a = a + 1, a = a - 1];
]
]
For[k = 1, k <= num/10, k++,
For[n = 1, n <= num/10*9, n++,
corr[[k]] = corr[[k]] + tab[[n]]*tab[[n + k - 1]]/num*10/9];
]
ListPlot[{corr}, Joined -> False]
Best regards
Cornelius
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2013-02-24 04:31 +0000 |
| Message-ID | <kgc52o$gal$1@smc.vnet.net> |
| In reply to | #13580 |
On Feb 23, 4:01 am, firlefranz <cornelius.fr...@gmx.net> wrote:
> Hi,
>
> to speed up some calculations I'd like to use this relatively new method of compiling equations or export them to C. But since I'm very new in these things, I have really problems to use this c-compiling function.
> Can someone show me on this simple part of code (calculation of a correlation function of a 1D random walk), how it should be implemented?
I see little need to compile. Here is your code
(with 'l' changed to'j' to avoid confusion with '1'),
with times for the two outer loops:
SeedRandom[1234567890]
num = 10^3
tab = Table[0, {num}];
corr = Table[0, {num/10}];
AbsoluteTiming@For[j = 1, j <= 1000, j++,
a = 0;
For[i = 1, i <= num, i++,
tab[[i]] = tab[[i]] + a/10;
If[RandomReal[{-1,1}] < 0, a = a + 1, a = a - 1]]]
AbsoluteTiming@For[k = 1, k <= num/10, k++,
For[n = 1, n <= num/10*9, n++,
corr[[k]] = corr[[k]] + tab[[n]]*tab[[n + k - 1]]/num*10/9]]
corr1 = corr;
1000
{22.882292, Null}
{2.836433, Null}
Here are the corresponding times for reorgnized code
that gives the same 'corr':
SeedRandom[1234567890]
AbsoluteTiming@Length[ tab = FoldList[Plus,0,
Total@Sign@Table[Most@RandomReal[{-1,1},num],{1000}]] ]
AbsoluteTiming@Length[ corr = Table[Take[tab,{k,num*9/10-1+k}],
{k,num/10}].Drop[tab,-num/10]/(num*10*9) ]
corr == corr1
{0.262409,1000}
{0.007661,100}
True
Most@RandomReal[{-1,1},num] should be RandomReal[{-1,1},num-1],
but I generated 'num' randoms and then dropped the last one
to keep them the same as in your code.
[toc] | [prev] | [next] | [standalone]
| From | firlefranz <cornelius.franz@gmx.net> |
|---|---|
| Date | 2013-02-26 06:09 +0000 |
| Message-ID | <kghjis$s3p$1@smc.vnet.net> |
| In reply to | #13580 |
Thanks a lot! To be honest, some of the commands Ray is using, I've never seen before. I stopped using mathematica before version 5 came out.
Coming back to Peters statement of exporting the code from Mathematica to C. How this can be done starting from my or Ray's code? There is an automated C-code-gernerator implemented in Mathematica 9, am I right?
As a step forward to the first little peace of code, here is another try, which is not really optimized. The be concrete, I try to simulate the autocorrelation function of a random walk, which is doing a step at none equally distant time steps. This has to been done for a long random walk for many particles. Instead of doing an average over many random walks and calculate one autocorrelation function I want to simulate many correlation functions and make an average over them. Since the time steps are non equal, I wrote a sub-function, which creates a new time axis and taking the necessary value for the random walk from the first table.
Here is what I come up with. It's running in a reasonable time for one particle, but for a real statistic ensemble, I have to do it over 1.000.000 particles for a long time. Optimizing this or (probably better) exporting it to C would hopefully help a lot. So how to export it?
Clear["Global`*"]
SeedRandom[1234567890];
zeitmax = 100;(* muss ein Integer sein *)
numteilchen = 1;
tauj = 1;
corr = Table[0, {i, 1, zeitmax/10}];
SucheIndex[zeitliste_, zeit_, maxindex_] :=
Module[{i},
For[i = 1, i <= maxindex, i++,
If[zeitliste[[i]] > zeit, Break[]];
];
i - 1
];
For[j = 1, j <= numteilchen, j++,
(* Zeitachse generieren von 0 bis zeitmax *)
t = 0;
i = 1;
tabzeit = {};
time = AbsoluteTiming[While[True,
tabzeit = Append[tabzeit, t];
dt = -tauj*Log[1 - RandomReal[]];
If[t > zeitmax, Break[]];
t = t + dt;
i++;
];
];
Print[time];
maxidx = i;
(* Random Walk *)
time = AbsoluteTiming[
tabwalk = Table[0, {i, 1, maxidx}];
a = 0;
For[i = 1, i <= maxidx, i++,
tabwalk[[i]] = a;
If[RandomReal[{-1, 1}] > 0, a = a + 1, a = a - 1];
];
];
Print[time];
(*tabwalk=Table[Subscript[b, i],{i,1,maxidx}];*)
(* Korrelationsfunktion berechnen *)
time = AbsoluteTiming[
For[k = 1, k <= zeitmax/10, k++,
For[n = 1, n <= zeitmax/10*9, n++,
corr[[k]] =
corr[[k]] +
tabwalk[[SucheIndex[tabzeit, n - 1, maxidx]]]*
tabwalk[[SucheIndex[tabzeit, n + k - 2, maxidx]]]/
zeitmax*10/9/numteilchen;
(*Print[corr//N];*)
];
];
];
Print[time];
Print[corr // N];
];
Table[{tabzeit[[i]], tabwalk[[i]]}, {i, 1, maxidx}]
corr // N
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2013-02-27 08:04 +0000 |
| Message-ID | <kgkem2$4cl$1@smc.vnet.net> |
| In reply to | #13616 |
On Feb 25, 10:09 pm, firlefranz <cornelius.fr...@gmx.net> wrote:
> Thanks a lot! To be honest, some of the commands Ray is using, I've never seen before. I stopped using mathematica before version 5 came out.
>
> Coming back to Peters statement of exporting the code from Mathematica to C. How this can be done starting from my or Ray's code? There is an automated C-code-gernerator implemented in Mathematica 9, am I right?
>
> As a step forward to the first little peace of code, here is another try, which is not really optimized. The be concrete, I try to simulate the autocorrelation function of a random walk, which is doing a step at none equally distant time steps. This has to been done for a long random walk for many particles. Instead of doing an average over many random walks and calculate one autocorrelation function I want to simulate many correlation functions and make an average over them. Since the time steps are non equal, I wrote a sub-function, which creates a new time axis and taking the necessary value for the random walk from the first table.
>
> Here is what I come up with. It's running in a reasonable time for one particle, but for a real statistic ensemble, I have to do it over 1.000.000 particles for a long time. Optimizing this or (probably better) exporting it to C would hopefully help a lot. So how to export it?
>
> << code omitted >>
Before spending more time on your real problem, you might want to
work at understanding why the code I posted for your example problem
necessarily gives the same corr as your code does, and then why the
following, which is faster, is also correct, even tho its corr can
not be compared to the other two because it generates the random
walks differently.
num = 10^3;
AbsoluteTiming@Length[ tab = FoldList[Plus,0,
RandomInteger[BinomialDistribution[1000,1/2],num-1]-500] ]
AbsoluteTiming@Length[ corr = #.#[[1]]& @
Most@Partition[tab,num*9/10,1]/(num*10*9/4) ]
{0.010728, 1000}
{0.010363, 100}
There are other ways to speed things up even more,
but that will suffice for now.
[toc] | [prev] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2013-03-01 02:25 +0000 |
| Message-ID | <kgp3i8$ctl$1@smc.vnet.net> |
| In reply to | #13616 |
On 26/02/2013 06:09, firlefranz wrote:
> Thanks a lot! To be honest, some of the commands Ray is using, I've never seen before. I stopped using mathematica before version 5 came out.
>
Remember that clicking on any function name and pressing F1, will give
you some help.
Also, if you are confused about what certain operators do, or what their
precedence is in a particular expression, you can always resort to
FullForm. Since you want to look at the expression before evaluation, do
this:
FoldList[Plus,0,
Total@Sign@Table[Most@RandomReal[{-1,1},num],{1000}]] //Hold//FullForm
Hold[FoldList[Plus, 0,
Total[Sign[Table[Most[RandomReal[List[-1, 1], num]], List[1000]]]]]]
The other thing to remember, is that repeated clicking at a point in an
expression (or use of Edit/Extend selection) will pick out successively
larger complete expressions - whether the expression is FullForm or not.
You can copy/paste such an expression somewhere else, and test it to see
what it does.
David Bailey
http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [next] | [standalone]
| From | Peter Klamser <klamser@googlemail.com> |
|---|---|
| Date | 2013-02-27 08:04 +0000 |
| Message-ID | <kgkemh$4cs$1@smc.vnet.net> |
| In reply to | #13580 |
Hi Cornelius,
your code is not slow because you use Mathematica but because you are not
writing in functional and Mathematica style.
Use /@, @@, pure Functions like #1^2& etc. to realize your algorithm.
Plus @@ {a, b, c, d} is much faster then procedural programming.
Kind regards from Peter
2013/2/26 firlefranz <cornelius.franz@gmx.net>:
> Thanks a lot! To be honest, some of the commands Ray is using, I've never seen before. I stopped using mathematica before version 5 came out.
>
> Coming back to Peters statement of exporting the code from Mathematica to C. How this can be done starting from my or Ray's code? There is an automated C-code-gernerator implemented in Mathematica 9, am I right?
>
>
> As a step forward to the first little peace of code, here is another try, which is not really optimized. The be concrete, I try to simulate the autocorrelation function of a random walk, which is doing a step at none equally distant time steps. This has to been done for a long random walk for many particles. Instead of doing an average over many random walks and calculate one autocorrelation function I want to simulate many correlation functions and make an average over them. Since the time steps are non equal, I wrote a sub-function, which creates a new time axis and taking the necessary value for the random walk from the first table.
>
> Here is what I come up with. It's running in a reasonable time for one particle, but for a real statistic ensemble, I have to do it over 1.000.000 particles for a long time. Optimizing this or (probably better) exporting it to C would hopefully help a lot. So how to export it?
>
> Clear["Global`*"]
> SeedRandom[1234567890];
> zeitmax = 100;(* muss ein Integer sein *)
> numteilchen = 1;
> tauj = 1;
> corr = Table[0, {i, 1, zeitmax/10}];
>
> SucheIndex[zeitliste_, zeit_, maxindex_] :=
> Module[{i},
> For[i = 1, i <= maxindex, i++,
> If[zeitliste[[i]] > zeit, Break[]];
> ];
> i - 1
> ];
>
> For[j = 1, j <= numteilchen, j++,
> (* Zeitachse generieren von 0 bis zeitmax *)
> t = 0;
> i = 1;
> tabzeit = {};
> time = AbsoluteTiming[While[True,
> tabzeit = Append[tabzeit, t];
> dt = -tauj*Log[1 - RandomReal[]];
> If[t > zeitmax, Break[]];
> t = t + dt;
> i++;
> ];
> ];
> Print[time];
> maxidx = i;
>
> (* Random Walk *)
> time = AbsoluteTiming[
> tabwalk = Table[0, {i, 1, maxidx}];
> a = 0;
> For[i = 1, i <= maxidx, i++,
> tabwalk[[i]] = a;
> If[RandomReal[{-1, 1}] > 0, a = a + 1, a = a - 1];
> ];
> ];
> Print[time];
> (*tabwalk=Table[Subscript[b, i],{i,1,maxidx}];*)
>
> (* Korrelationsfunktion berechnen *)
> time = AbsoluteTiming[
> For[k = 1, k <= zeitmax/10, k++,
> For[n = 1, n <= zeitmax/10*9, n++,
> corr[[k]] =
> corr[[k]] +
> tabwalk[[SucheIndex[tabzeit, n - 1, maxidx]]]*
> tabwalk[[SucheIndex[tabzeit, n + k - 2, maxidx]]]/
> zeitmax*10/9/numteilchen;
> (*Print[corr//N];*)
> ];
> ];
> ];
> Print[time];
> Print[corr // N];
> ];
> Table[{tabzeit[[i]], tabwalk[[i]]}, {i, 1, maxidx}]
> corr // N
>
[toc] | [prev] | [next] | [standalone]
| From | Peter Klamser <klamser@googlemail.com> |
|---|---|
| Date | 2013-03-05 09:15 +0000 |
| Message-ID | <kh4d31$6ve$1@smc.vnet.net> |
| In reply to | #13580 |
... I always use <ctrl>. to pick complete parts of an expression,
because the timing of clicking is very sensitive ...
You can see, that it is no good to use expressions like For ... etc.
to generate fast code in Mathematica.
For statements ect.. are bad style in Mathematica like GOTO Label in former
days, when we learned to use PASCAL, MODULA etc.. Even if you can
still use Label in Mathematica...
says Peter
2013/3/1 David Bailey <dave@removedbailey.co.uk>:
> On 26/02/2013 06:09, firlefranz wrote:
>> Thanks a lot! To be honest, some of the commands Ray is using, I've never seen before. I stopped using mathematica before version 5 came out.
>>
>
> Remember that clicking on any function name and pressing F1, will give
> you some help.
>
> Also, if you are confused about what certain operators do, or what their
> precedence is in a particular expression, you can always resort to
> FullForm. Since you want to look at the expression before evaluation, do
> this:
>
> FoldList[Plus,0,
> Total@Sign@Table[Most@RandomReal[{-1,1},num],{1000}]] //Hold//FullForm
>
> Hold[FoldList[Plus, 0,
> Total[Sign[Table[Most[RandomReal[List[-1, 1], num]], List[1000]]]]]]
>
> The other thing to remember, is that repeated clicking at a point in an
> expression (or use of Edit/Extend selection) will pick out successively
> larger complete expressions - whether the expression is FullForm or not.
> You can copy/paste such an expression somewhere else, and test it to see
> what it does.
>
> David Bailey
> http://www.dbaileyconsultancy.co.uk
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web