Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Optimizing For loop statement
Replies: 1   Last Post: Feb 22, 2013 1:05 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Bailey

Posts: 712
Registered: 11/7/08
Re: Optimizing For loop statement
Posted: Feb 22, 2013 1:05 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.