Groups | Search | Server Info | Login | Register


Groups > comp.soft-sys.math.mathematica > #16900

Re: How to avoid repeated recalculation of the same function

From psycho_dad <s.nesseris@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to avoid repeated recalculation of the same function
Date 2014-05-04 06:27 +0000
Message-ID <lk4mk0$g4l$1@smc.vnet.net> (permalink)
References <ljvdcg$bal$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


Hey!

Consider the behavior of the two functions f and f1 (I have added the Pause[2] to give the impression of a long calculation):

f[x_]:=(Pause[2];x^2)
f1[x_]:= f1[x]=(Pause[2];x^2)

(Notice the f1[x_]:= f1[x] "trick" used above...)

Now evaluate both of them:
f[x]
f1[x]

both take a long time to evaluate the first time. Now repeat:
f[x]
f1[x]

you will notice that f1[x] was done instantaneously! The reason is that Mathematica has included the value as part of the definition due to the f1[x_]:= f1[x] "trick", so it remembers it after it evaluates it once.

There are also other ways to do what you want, eg hardcoding by hand the result for f[x] after you evaluate it once (assuming it it always the same) etc.

Cheers

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to avoid repeated recalculation of the same function pgeipi10@gmail.com - 2014-05-02 06:18 +0000
  Re: How to avoid repeated recalculation of the same function Richard Fateman <fateman@cs.berkeley.edu> - 2014-05-03 07:39 +0000
  Re: How to avoid repeated recalculation of the same function Joe Gwinn <joegwinn@comcast.net> - 2014-05-03 07:40 +0000
  Re: How to avoid repeated recalculation of the same function psycho_dad <s.nesseris@gmail.com> - 2014-05-04 06:27 +0000
  Re: How to avoid repeated recalculation of the same function Roland Franzius <roland.franzius@uos.de> - 2014-05-04 06:28 +0000

csiph-web