1
$\begingroup$

I need to call some function defined in a subsection of some section of mathematica's .nb file. The code of its evaluation and displaying looks like

f[x_,y_,z_,t_] := Integrate[...] f[x,y,z,t] 

I need to use this function in another subsection of the same section. First, in order to check whether it is called correctly in this subsection, I just need to display it, so I write

f[x,y,z,t] 

I expect immediate displaying, since the function is already defined and evaluated. But sometimes the time for displaying is comparable with the time of evaluating the integral from the first subsection! What are general reasons for this and how to avoid this?

P.S. I don't attach the precise code since I don't understand what to attach (i.e., what is relevant).

$\endgroup$
1
  • 2
    $\begingroup$ You may also use memoization, i.e. f[x_]:=f[x]=... will store every value of f for every argument that has already been plugged in. Remember that this may crowd your memory if you do call f for a large number of different arguments whithout emptying the memory in the meantime. But for usual cases this is fine $\endgroup$ Commented Jul 30, 2017 at 8:07

1 Answer 1

7
$\begingroup$

You're explicitly re-evaluating the function f[x,y,z,t], so it's computing the integral a second time. This is because SetDelayed (:=) holds its right-hand side in an unevaluated form. The simplest way to recall the result is to store it in another symbol, i.e. int = f[x,y,z,t] where you can then call int and the result will be displayed. Or you can do f[x_,y_,z_,t_] := Evaluate[Integrate[...]].

You can also have a look at the article in the documentation Functions That Remember Values They Have Found.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.