Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

here is the silly question of mine,

if mathematica takes 3 seconds to evaluate the function f[3] then I plug the this value f[3] into a new function g[f[3],f[3]], Let's ignore the evaluation time for the new function g, how long does it take for g[f[3],f[3]], 3 seconds or 6 seconds?

if it is 6 seconds, how can I make mathematica temporarily to remember the value of f[3]

thanks

share|improve this question
up vote 8 down vote accepted

Use With to define temporary constants.

With[{a=f[3]}, g[a,a]]

Memoization is also worth mentioning but keep in mind that once a value is remembered, it won't be forgotten (until to explcitly clear it). I.e. when you use memoization, Mathematica will remember permanently (not temporarily).

share|improve this answer

With keeps the code clear for longer cases but sometimes I'd do:

g[#,#] & @ f[3]
share|improve this answer
Clear["Global`*"]
ClearSystemCache[];
$RecursionLimit = Infinity;
fibonacci[1] = 1; fibonacci[2] = 1;
fibonacci[i_] := fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
fibonacci[20000]; // AbsoluteTiming
fibonacci[20000]; // AbsoluteTiming
(*{0.135008, Null}*)
(*{0., Null}*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.