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


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

Optimizing For loop statement

Started by"tarpanelli@libero.it" <tarpanelli@libero.it>
First post2013-02-21 03:26 +0000
Last post2013-02-22 06:05 +0000
Articles 2 — 2 participants

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


Contents

  Optimizing For loop statement "tarpanelli@libero.it" <tarpanelli@libero.it> - 2013-02-21 03:26 +0000
    Re: Optimizing For loop statement David Bailey <dave@removedbailey.co.uk> - 2013-02-22 06:05 +0000

#13570 — Optimizing For loop statement

From"tarpanelli@libero.it" <tarpanelli@libero.it>
Date2013-02-21 03:26 +0000
SubjectOptimizing For loop statement
Message-ID<kg445l$r5b$1@smc.vnet.net>
Hello, here below you can find a piece of Mathematica code:
...
For[i=3,i<=n,i++,
s=sigma[a,b,c]
vol=ReplacePart[vol,{{i,1}->x[[i]],{i,2}->s}];
Clear[b];
b=Interpolation[vol];
];
 
where sigma is a module which take as input parameters two scalars (a,c) and one interpolating function (b).
What I need is to update the data (vol), on which the interpolating function is built, according to the new result of the module sigma which takes, itsself, the interpolating function as input parameter.
 
I see that the For loop statement is really time-consuming, and I would like to know if it is possible to ptimize it.
thanks a lot
Paolo

[toc] | [next] | [standalone]


#13576

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-02-22 06:05 +0000
Message-ID<kg71rq$4ml$1@smc.vnet.net>
In reply to#13570
On 21/02/2013 03:26, tarpanelli@libero.it wrote:
> Hello, here below you can find a piece of Mathematica code:
> ...
> For[i=3,i<=n,i++,
> s=sigma[a,b,c]
> vol=ReplacePart[vol,{{i,1}->x[[i]],{i,2}->s}];
> Clear[b];
> b=Interpolation[vol];
> ];
>
> where sigma is a module which take as input parameters two scalars (a,c) and one interpolating function (b).
> What I need is to update the data (vol), on which the interpolating function is built, according to the new result of the module sigma which takes, itsself, the interpolating function as input parameter.
>
> I see that the For loop statement is really time-consuming, and I would like to know if it is possible to ptimize it.
> thanks a lot
> Paolo
>
>
People will tell you not to use For because it is slower than using 
functional constructs, but that is not the main problem here. You don't 
want to use ReplacePart to update vol, use something like:

vol[[i]]={x[[i]],s};

Also, you don't need to Clear b before giving it a value! Clearing could 
well be expensive too.

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [standalone]


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


csiph-web