How can I optimize Mathematica computation time for functional definitions?

Click For Summary
SUMMARY

The discussion centers on optimizing computation time in Mathematica for functional definitions. Users highlight that using immediate definitions (using "=") instead of delayed definitions (using ":=") significantly reduces evaluation time for functions. The example provided illustrates that pre-evaluating a function with numeric output before redefining it can lead to faster subsequent calculations. Participants seek methods to automate the process of redefining functions to enhance performance further.

PREREQUISITES
  • Understanding of Mathematica syntax and function definitions
  • Familiarity with numeric versus symbolic computation in Mathematica
  • Knowledge of the NullSpace function and its applications
  • Basic principles of integration in Mathematica
NEXT STEPS
  • Learn about Mathematica's immediate and delayed definitions in depth
  • Explore the use of Compile for performance optimization in Mathematica
  • Investigate the use of Memoization to cache results of function calls
  • Study the integration of numeric and symbolic computations in Mathematica
USEFUL FOR

Mathematica users, computational scientists, and anyone looking to optimize function evaluation times in mathematical computations.

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
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   Reactions: jim mcnamara
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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
874
  • · Replies 4 ·
Replies
4
Views
3K