Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.5K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Clear values of functions that remember values they have found?

Posted 9 years ago

How can i clear values saved by "Function That Remember Values They Have Found" without clearing the definition of the function? I have problems with memory usage. For example:

In:

f[x_] := f[x] =x^2;

f[0];

f[1];

?f

Out:

Global`f

f[0]=0

f[1]=1 

f[x_]:=f[x]=x^2

I want to clear all of the saved values (without specifying the values) and have this

In:

?f

Out:

Global`f

f[x_]:=f[x]=x^2

Thank you

POSTED BY: Arkadiy Ukupnik
4 Replies
Posted 9 years ago

You can use DownValues[] to reset. Try the following, one cell at a time:

f[x_] := f[x] =x^2;

f /@ Range[10]

?f

DownValues[f] = Take[DownValues[f], -1];

?f
POSTED BY: J. M.
Posted 9 years ago

A very elaborated answer which demonstrates how to use Mathematica's built-in SQL database to store the cache completely externally:

http://stackoverflow.com/a/5291299/590388

You can also be interested in Leonid Shifrin's Cache.m package. Citing the home page (emphasis is mine):

This package allows one to create a "copy" of a given function of a single argument (Downvalue-based or a pure function), with an internal cache of the fixed size . This cache will automatically store results of the most recent function calls, and use the stored values rather than recompute the r.h.s. in case if the function is called again on the same argument. This may speed-up computations considerably in cases when a function is both computationally intensive and is frequently called on the same values of the argument.

POSTED BY: Alexey Popkov

Thank you for help!

POSTED BY: Arkadiy Ukupnik

Hm. I think i've found a decision.

f[x_] := f[x] = x^3;
DumpSave["dump.mx", f];
f[0];
f[1];
f[2];
f[3];
?f
Print["======="]
Clear[f]
?f
Print["======="]
<< dump.mx
?f

Out:

Global`f

f[0]=0

f[1]=1

f[2]=8

f[3]=27

f[x_]:=f[x]=x^3


=======

Global`f

=======

Global`f

f[x_]:=f[x]=x^3
POSTED BY: Arkadiy Ukupnik
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard