Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to do numerical computations?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108806] Re: How to do numerical computations?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 1 Apr 2010 06:00:39 -0500 (EST)

On 3/31/10 at 5:28 AM, bagarti at gmail.com (bagarti) wrote:

>I have written some mathematica code which contains some complicated
>functions.The functions involve integrations and iterations in the
>intermediate steps. When I run the code it takes a hell lot of time.
>I do not know what mathematica is doing inside, actually I have
>another code in fortran that takes only a few minutes to do all the
>calculations but there are other problems like it dosent work for
>some parameter range.

>Is there a method in which I can ask mathematica to give me only the
>numerical values just like any executable program does.

The way to get Mathematica to return only numerical values is to
use functions that begin with a upper case N. For example,
NIntegrate does a numerical integration while Integrate will do
a symbolic integration. Note, doing N[Integrate[..]] is not
equivalent to doing NIntegrate[...] in general.

But I don't know that this helps or making these changes would
improve the speed of execution. Improving the execution speed is
very much dependent on the details of you code.

One thing to be careful about. Coming from a background in a
procedural language like Fortran, you probably tend to want to
use explicit For loops. While Mathematica supports For, code
written using For typically runs much slower than other means of
getting exactly the same answer. Compare the results below:

In[1]:= Timing[For[sum = n = 0, n < 10^6, n++, sum += n]; sum]

Out[1]= {2.99546,499999500000}

In[2]:= Timing[Plus @@ Range[10^6 - 1]]

Out[2]= {0.275468,499999500000}

In[3]:= Timing[Total[Range[10^6 - 1]]]

Out[3]= {0.22101,499999500000}

In[5]:= Timing[Sum[n, {n, 10^6 - 1}]]

Out[5]= {0.250184,499999500000}

In[6]:= Timing[Sum[n, {n, k}] /. k -> 10^6 - 1]

Out[6]= {0.063701,499999500000}

All methods give the same answer but the code with the explicit
=46or loop runs more than 10 times slower than the next slowest
method of summing a range of values.



  • Prev by Date: Re: a little nasty problem?
  • Next by Date: Re: How to do numerical computations?
  • Previous by thread: Re: How to do numerical computations?
  • Next by thread: Re: How to do numerical computations?