How can I optimize Mathematica computation time for functional definitions?

In summary, the conversation discusses ways to increase the speed of computations in Mathematica, specifically when dealing with complicated functions and integrals. The main suggestion is to use the immediate definition "=" instead of the delayed definition ":=" to reduce the subsequent overhead. However, even with this method, there are still ways to further optimize the code, such as manually redefining the function in terms of its numerical result. The conversation ends with a request for ideas on automating this process for multiple iterations.
  • #1
member 428835
Hi PF!

I am using the following code in Mathematica
Code:
mat = {{1, Cos[2], Cos[4], Sin[2], 
    Sin[4}, {1, 1, 1, 0, 0}};
Li[x_] := 
  Transpose[
   NullSpace[
     mat].{{1}, {Cos[x}, {Cos[
       2 x]}, {Sin[x]}, {Sin[2 x]}}];
Li1[x_] := Li[x].{{1}, {0}, {0}};
f[x_] := 
  Li1[x]/Sqrt[Integrate[Li1[s]^2, {s, -1, 1}]];
Then when I want to compute something simple, say
Code:
Integrate[f[x]^2, {x, -1, 1}]
It takes Mathematica a very long time to compute. However, if I first do
Code:
f[x]//N
and then redefine ##f## in terms of the result mathematica prints and ask for the integral, Mathematica computes the integration in much less time. Does anyone know how to define ##f## to begin with in this "numeric" fashion to decrease run time?
 
Physics news on Phys.org
  • #2
The main reason for the slow speed is because
Code:
f[x_]:= (some stuff here)
is a functional definition, so that whenever ##f(\alpha)## is called for a particular argument ##\alpha##, the sequence of instructions given in (some stuff here) will be evaluated from scratch. So, if (some stuff here) contains something relatively complicated such as an integral, you can see why the complexity builds up tremendously fast (especially when you're trying to integrate ##f(x)## itself!). That is to say, the right hand side will only be evaluated when the function is called.

Conversely if you instead adopt the definition
Code:
f[x_]= (some stuff here)
where the colon has been omitted, then the right-hand-side is immediately evaluated. This greatly reduces the subsequent overhead.

I personally almost always use the "=" (immediate definition) instead of ":=" (delayed definition) unless I need to keep the right hand side in unevaluated form for some reason.
 
  • Like
Likes jim mcnamara
  • #3
Fightfish said:
The main reason for the slow speed is because
Code:
f[x_]:= (some stuff here)
is a functional definition, so that whenever ##f(\alpha)## is called for a particular argument ##\alpha##, the sequence of instructions given in (some stuff here) will be evaluated from scratch. So, if (some stuff here) contains something relatively complicated such as an integral, you can see why the complexity builds up tremendously fast (especially when you're trying to integrate ##f(x)## itself!). That is to say, the right hand side will only be evaluated when the function is called.
Good to know, thanks!

Fightfish said:
Conversely if you instead adopt the definition
Code:
f[x_]= (some stuff here)
where the colon has been omitted, then the right-hand-side is immediately evaluated. This greatly reduces the subsequent overhead.

I personally almost always use the "=" (immediate definition) instead of ":=" (delayed definition) unless I need to keep the right hand side in unevaluated form for some reason.
This way is quicker but is still taking a significant amount of time to evaluate integrals. For example, if after running
Code:
f[x]//N
I redefine ##f## as
Code:
f[x_]=
0.9073859955296714 (0.534409621782702 - 
   0.534409621782702 Cos[x] + Sin[2 x])
subsequent operations on ##f## are computed almost instantly. But I have to manually redefine ##f## in this fashion. Since I will be performing many iterations it would be nice to automate this process. Do you have any ideas how to do this?

Thanks for your interest!
 

Related to How can I optimize Mathematica computation time for functional definitions?

1. How does Mathematica calculate computation time?

Mathematica calculates computation time by measuring the amount of time it takes for the computer to execute a specific task or set of tasks. This includes time spent on calculations, memory allocation, and other operations.

2. What factors can affect Mathematica computation time?

Some factors that can affect Mathematica computation time include the complexity of the calculation, the size of the dataset, the processing power of the computer, and the efficiency of the code used.

3. Can Mathematica computation time be improved?

Yes, there are several ways to improve Mathematica computation time. These include optimizing code, using parallel processing, and utilizing specialized functions and algorithms for specific tasks.

4. How can I measure the computation time of a specific Mathematica function?

You can measure the computation time of a specific Mathematica function by using the Timing function. This will return the time in seconds it took for the function to execute.

5. Is there a way to set a time limit for Mathematica computations?

Yes, you can set a time limit for Mathematica computations by using the TimeConstraint function. This will stop the computation after a specified amount of time has passed, which can be useful for preventing long calculations from running indefinitely.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
216
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
968
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
388
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top