From: bagarti on
Hello,
Any body please help me,

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.

Thanks.
TaB

From: Bill Rowe on
On 3/31/10 at 5:28 AM, bagarti(a)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.


From: Nasser M. Abbasi on

"bagarti" <bagarti(a)gmail.com> wrote in message
news:hov834$94q$1(a)smc.vnet.net...
> Hello,
> Any body please help me,
>
> 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.
>
> Thanks.
> TaB
>

Symbolic computation is very useful for the analysis and the investigation
of a problem analytically. (funny thing to say, because how else would one
do this other by symbolic computation?)

But when it comes to solving the problem on a large scale, and for speed, I
think you want to switch to numerical computation. This could be as easily
as warping N[] around an expression, or having the initial parameters be
floating point values instead of rational (i.e. for the values of the
variables used, write 1.0/2 or 0.5 instead of 1/2 and write 1.0 instead of
1).

There is a cascading effect here, if one variable is floating point, then
the calculation of the expression which this variable is in will be done
numerically and will be normally faster.

Also, try to use functions such as NIntegrate and NSolve and NDSolve instead
of the symbolic versions.

I wrote a small finite elements program last year in Mathematica, and it was
very slow when the number of elements and the stiffness matrix became large,
this is because at first I kept everything in symbolic form, so all the
terms were in symbolic form until the end when I substitute numerical values
for the variables.

When I started to make some of the changes above, and to assign a numerical
values for the variables early on, the program ran much much faster than
before.

--Nasser




From: Manfred Plagmann on
On Mar 31, 11:27 pm, bagarti <baga...(a)gmail.com> wrote:
> Hello,
> Any body please help me,
>
> 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.
>
> Thanks.
> TaB

Many analytical functions with in Mathematica have numerical counterparts for
example Integrate[] has NIntegrate[]. You can use those to get
numerical results. If your code is very slow this could have many
reasons and without giving an example of what you are doing it will be
hard to help you. Keep in mind that Mathematica is interpreted and not compiled
Fortran code.

I hope this helps.

Manfred

From: dr DanW on
I'm taking a guess that you have a fairly complicated Block[] or
Module[] with an integration that involves some parameters.
Mathematica is hitting that integration and trying to evaluate it
symbolically, which can be time consuming. I think what you want to
do is prevent that integration from evaluating until it gets numerical
values for the parameters. One way to do this is to create a function
out of the integration that requires numerical input. A trivial
example is

int[a_?NumericQ] := NIntegrate[ Sin[x] , {x, 0, a} ]

This way, when you say

b = int[c]

where c has no value, Mathematica will hold it as int[c] and not
attempt a symbolic evaluation. Later, you can say c = 3., and the
numerical integration can be evaluated quite quickly.

Daniel