Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clearing all variables and definitions: i.e. "resetting" mathematica

1,149 views
Skip to first unread message

Brentt

unread,
Sep 29, 2012, 3:09:18 AM9/29/12
to

Hi, is there any way to reset an entire session, clearing all variables and
definitions to what they would be when opening up mathematica. Occasionally
I look through the docs for this functionality but it has always eluded me
(and I must have read most of the docs by now ), so usually will just shut
down mathematica and restart. There has got to be a better way right?


awnl

unread,
Sep 29, 2012, 8:25:14 PM9/29/12
to
When you work the usual way with from the frontend I think a Kernel quit
(either from the Evaluate menu or just by evaluating Quit) will be the
most convenient and reliable way to reset the session. The next
evaluation will automatically start a new, fresh kernel. There are ways
to reset the Kernel without quitting it, but on a modern computer the
restart is so fast that I wouldn't struggle with all the eventualities
that arise when trying to do a reliable full reset.

hth,

albert

Bill Rowe

unread,
Sep 29, 2012, 8:26:28 PM9/29/12
to
On 9/29/12 at 2:56 AM, brentt...@gmail.com (Brentt) wrote:

>Hi, is there any way to reset an entire session, clearing all
>variables and definitions to what they would be when opening up
>mathematica. Occasionally I look through the docs for this
>functionality but it has always eluded me (and I must have read most
>of the docs by now ), so usually will just shut down mathematica and
>restart. There has got to be a better way right?

At the beginning of a session, load the package
Utilities`CleanSlate`. Then later when you want to reset things
to the state that existed when the CleanSlate package was loaded
simply do

CleanSlate[]

I have this package loaded at the start of every session by including

<<Utilities`CleanSlate`

in my init.m file.

Also, you can load this package later in your session if you
like. Then computations done before the CleanSlate package was
loaded are preserved. Only things done after the CleanSlate
package is loaded are reset.


dgo...@aol.com

unread,
Oct 6, 2012, 4:18:49 AM10/6/12
to
About CleanSlate[]:

Mathematica 4 running on windows XP.

The notebook below begins with $HistoryLength=0. It fits a 2-parameter curve (a simple Bezier curve) to some data.

The module "error" finds the total sum-of-squares of distances from the data points to a curve. Then FindMinimum fits the curve to the data by minimizing "error".

Look at the "memory in use" numbers and the call to CleanSlate. Before the first call to FindMinimum, the memory in use is about 1 MB. At this point the CleanSlate utility is loaded, and so this is the state that CleanSlate should return to.

Then FindMinimum is called for the first time. The memory in use builds up to about 25 MB and does not lessen after FindMinimum exits.

Then CleanSlate[] is called. It does not find that 25 MB! The second call to FindMinimum starts with 25 MB ... and stays with 25 MB of memory in use.

Is there any documentation of CleanSlate[]? My system _has_ CleanSlate[], but it's not described in any of the Help stuff.

(My complaints about mathematica (mine is V4) begin with: the system doesn't follow elementary documentation rules: What are all the inputs, what are all the outputs ...simple stuff like that ...)

Thanks for whatever help you can give ...

Dave Golber

In[1]:=
$HistoryLength = 0;
In[2]:=
dsq[p_, q_] := Module[{v}, v = p - q; Return[v.v]];
In[3]:=
data = {{-1.`, 0.07271879311326668`}, {-0.9795918367346939`,
0.40795711960412273`}, {-0.9591836734693877`,
0.7493968681691019`}, {-0.9387755102040817`,
1.0969831944328101`}, {-0.9183673469387755`,
1.4455797049857155`}, {-0.8979591836734694`,
1.7933577858764531`}, {-0.8775510204081632`,
2.1407781653548907`}, {-0.8571428571428571`,
2.4892086871775314`}, {-0.8367346938775511`,
2.835059096420477`}, {-0.8163265306122449`,
3.1747813234745452`}, {-0.7959183673469388`,
3.5099065199869566`}, {-0.7755102040816326`,
3.838416824535938`}, {-0.7551020408163265`,
4.160416099694797`}, {-0.7346938775510204`,
4.476709556785695`}, {-0.7142857142857143`,
4.784664350286133`}, {-0.6938775510204082`,
5.087323728777496`}, {-0.673469387755102`,
5.38696253472903`}, {-0.6530612244897959`,
5.6798465046806585`}, {-0.6326530612244898`,
5.964713365415807`}, {-0.6122448979591837`,
6.238632239548857`}, {-0.5918367346938775`,
6.501620953518743`}, {-0.5714285714285714`,
6.75656172280596`}, {-0.5510204081632653`,
7.002600506634537`}, {-0.5306122448979592`,
7.239968897142224`}, {-0.5102040816326531`,
7.467505798949419`}, {-0.4897959183673469`,
7.688899842449843`}, {-0.46938775510204084`,
7.895548159341792`}, {-0.4489795918367347`,
8.097377347460679`}, {-0.42857142857142855`,
8.2893341746341`}, {-0.40816326530612246`,
8.476144299078989`}, {-0.3877551020408163`,
8.652889973777224`}, {-0.3673469387755102`,
8.817374903757845`}, {-0.3469387755102041`,
8.976924242334949`}, {-0.32653061224489793`,
9.119591134252524`}, {-0.30612244897959184`,
9.248576791309134`}, {-0.2857142857142857`,
9.369588023769005`}, {-0.2653061224489796`,
9.482297547178607`}, {-0.24489795918367346`,
9.589139190122037`}, {-0.22448979591836735`,
9.689364983113595`}, {-0.20408163265306123`,
9.781452292825708`}, {-0.1836734693877551`,
9.860153881813591`}, {-0.16326530612244897`,
9.923011791745626`}, {-0.14285714285714285`,
9.976018300997982`}, {-0.12244897959183673`,
10.02452888876957`}, {-0.10204081632653061`,
10.070756998999077`}, {-0.08163265306122448`,
10.114970754465642`}, {-0.061224489795918366`,
10.15451548515944`}, {-0.04081632653061224`,
10.18522338591307`}, {-0.02040816326530612`,
10.202365464896314`}, {0.`,
10.208468465393292`}, {0.02040816326530612`,
10.202365464896314`}, {0.04081632653061224`,
10.18522338591307`}, {0.061224489795918366`,
10.15451548515944`}, {0.08163265306122448`,
10.114970754465642`}, {0.10204081632653061`,
10.070756998999077`}, {0.12244897959183673`,
10.02452888876957`}, {0.14285714285714285`,
9.976018300997982`}, {0.16326530612244897`,
9.923011791745626`}, {0.1836734693877551`,
9.860153881813591`}, {0.20408163265306123`,
9.781452292825708`}, {0.22448979591836735`,
9.689364983113595`}, {0.24489795918367346`,
9.589139190122037`}, {0.2653061224489796`,
9.482297547178607`}, {0.2857142857142857`,
9.369588023769005`}, {0.30612244897959184`,
9.248576791309134`}, {0.32653061224489793`,
9.119591134252524`}, {0.3469387755102041`,
8.976924242334949`}, {0.3673469387755102`,
8.817374903757845`}, {0.3877551020408163`,
8.652889973777224`}, {0.40816326530612246`,
8.476144299078989`}, {0.42857142857142855`,
8.2893341746341`}, {0.4489795918367347`,
8.097377347460679`}, {0.46938775510204084`,
7.895548159341792`}, {0.4897959183673469`,
7.688899842449843`}, {0.5102040816326531`,
7.467505798949419`}, {0.5306122448979592`,
7.239968897142224`}, {0.5510204081632653`,
7.002600506634537`}, {0.5714285714285714`,
6.75656172280596`}, {0.5918367346938775`,
6.501620953518743`}, {0.6122448979591837`,
6.238632239548857`}, {0.6326530612244898`,
5.964713365415807`}, {0.6530612244897959`,
5.6798465046806585`}, {0.673469387755102`,
5.38696253472903`}, {0.6938775510204082`,
5.087323728777496`}, {0.7142857142857143`,
4.784664350286133`}, {0.7346938775510204`,
4.476709556785695`}, {0.7551020408163265`,
4.160416099694797`}, {0.7755102040816326`,
3.838416824535938`}, {0.7959183673469388`,
3.5099065199869566`}, {0.8163265306122449`,
3.1747813234745452`}, {0.8367346938775511`,
2.835059096420477`}, {0.8571428571428571`,
2.4892086871775314`}, {0.8775510204081632`,
2.1407781653548907`}, {0.8979591836734694`,
1.7933577858764531`}, {0.9183673469387755`,
1.4455797049857155`}, {0.9387755102040817`,
1.0969831944328101`}, {0.9591836734693877`,
0.7493968681691019`}, {0.9795918367346939`,
0.40795711960412273`}, {1.`, 0.07271879311326668`}};
In[4]:=
bez[n_, i_, t_] := Binomial[n, i]((1 - t)^(i))( t^(n - i));
In[5]:=
t =.
In[6]:=
b1[t_] = bez[2, 2, t];
In[7]:=
b2[t_] = bez[2, 1, t];
In[8]:=
b3[t_] = bez[2, 0, t];
In[9]:=
Remove[t, y, w]
In[10]:=
curve[t_, y_,
w_] = (b1[t]{-1, 0} + w b2[t]{0, y} + b3[t]{1, 0})/(
b1[t] + w b2[t] + b3[t]);
In[11]:=
error[y_, w_] :=
Module[
{i, j, dist, least, sum, ret, t, numStarts = 50},
If[Mod[count, 100] == 0,
Print["count= ", count, " memory in use = ", MemoryInUse[]]];
count = count + 1;
If[ValueQ[prev] == False || Length[prev] != Length[data],
Print["Building prev"];
prev = Table[Infinity, {Length[data]}];
For[i = 1, i <= Length[data], i++,
least = Infinity;
For[j = -numStarts, j <= numStarts - 1, j++,
dist = dsq[data[[i]], curve[j/numStarts, y, w]];
If[dist < least,
least = dist;
prev[[i]] = N[j/numStarts];
]; (* If this is a better values *)
]; (* Loop over j *)
]; (* Loop over data *)
Print["prev built"];
];
sum = 0;
For[i = 1, i <= Length[data], i++,
ret = FindMinimum[dsq[data[[i]], curve[t, y, w]], {t, prev[[i]]}];
prev[[i]] = t /. ret[[2]];
sum = sum + ret[[1]];
];
Return[sum];
];
>From In[11]:=
General::"spell1": "Possible spelling error: new symbol name \"\!\(sum\)\" is \
similar to existing symbol \"\!\(Sum\)\"."
>From In[11]:=
General::"spell1": "Possible spelling error: new symbol name \"\!\(count\)\" \
is similar to existing symbol \"\!\(Count\)\"."
In[12]:=
<< Utilities`CleanSlate`
In[13]:=
Print["Before first call, memory usage = ", MemoryInUse[]];
>From In[13]:=
"Before first call, memory usage = "\[InvisibleSpace]1175320
In[14]:=
count = 0;
ret = FindMinimum[error[y, w],
{y, 20.0, 21.0}, {w, 1.0, 1.1}];
Print[ret];
>From In[14]:=
"count= "\[InvisibleSpace]0\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]1189904
>From In[14]:=
"Building prev"
>From In[14]:=
"prev built"
>From In[14]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[14]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[14]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[14]:=
General::"stop": "Further output of \!\(FindMinimum :: \"fmgz\"\) will be \
suppressed during this calculation."
>From In[14]:=
"count= "\[InvisibleSpace]100\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]19091072
>From In[14]:=
"count= "\[InvisibleSpace]200\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]24690792
>From In[14]:=
{0.00263245, {y -> 19.414, w -> 1.10807}}
In[17]:=
Print["After call, memory usage = ", MemoryInUse[]];

>From In[17]:=
"After call, memory usage = "\[InvisibleSpace]24867112
In[18]:=
CleanSlate[]
>From In[18]:=
" (CleanSlate) Contexts purged: "\[InvisibleSpace]{"Global`"}
>From In[18]:=
" (CleanSlate) Approximate kernel memory recovered: "\[InvisibleSpace]"9 Kb"
Out[0]=
{"CleanSlate`", "Global`", "System`"}
In[1]:=
Print["Before second call, memory usage = ", MemoryInUse[]];
>From In[1]:=
"Before second call, memory usage = "\[InvisibleSpace]24857528
In[2]:=
count = 0;
ret = FindMinimum[error[y, w],
{y, 20.0, 21.0}, {w, 1.0, 1.1}];
Print[ret];
>From In[2]:=
"count= "\[InvisibleSpace]0\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]24863144
>From In[2]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[2]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[2]:=
FindMinimum::"fmgz": "Encountered a vanishing gradient. The result returned \
may not be a minimum; it may be a maximum or a saddle point."
>From In[2]:=
General::"stop": "Further output of \!\(FindMinimum :: \"fmgz\"\) will be \
suppressed during this calculation."
>From In[2]:=
"count= "\[InvisibleSpace]100\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]24972840
>From In[2]:=
"count= "\[InvisibleSpace]200\[InvisibleSpace]" memory in use = "\
\[InvisibleSpace]24971248
>From In[2]:=
{0.00263245, {y -> 19.414, w -> 1.10807}}
In[5]:=
Print["After call, memory usage = ", MemoryInUse[]];

>From In[5]:=
"After call, memory usage = "\[InvisibleSpace]24995784


Bill Rowe

unread,
Oct 7, 2012, 1:37:38 AM10/7/12
to
On 10/6/12 at 1:49 AM, dgo...@aol.com wrote:

>Is there any documentation of CleanSlate[]? My system _has_ CleanSlate[], but it's not described in any of the Help stuff.

As far as I know, the documentation for CleanSlate has never
been incorporated into the help system. There are extensive
comments in the file CleanSlate.m that describe its operation.
The following returns the path to CleanSlate.m in a standard
distribution (as of version 8)

ToFileName[{$InstallationDirectory, "AddOns", "ExtraPackages",
"Utilities"}, "CleanSlate.m"]

I think I first found out about CleanSlate in an article in the
Mathematica Journal. But I've been using CleanSlate for so long
I cannot be certain.


dr DanW

unread,
Oct 7, 2012, 1:38:57 AM10/7/12
to
CleanSlate[] marks the state of memory on its first invocation. Subsequent calls to CleanSlate[] reset the memory to that state. This is why it should be the first thing run in your session. I have it at the top of every notebook so Cntl-A Shift Enter gives me a clean run of the notebook.

There are caveats to using CleanSlate in your init.m file in version 6+ since the Paclet manager gets loaded after init.m. Put the line:

DeclarePackage["Utilities`CleanSlate`",{"CleanSlate"}];

in your init.m, and put the line

CleanSlate[Verbose->False];

at the top of your notebooks. DeclarePackage tees up the package to be loaded but it is not actually loaded until CleanSlate is evaluated in your notebook, which occurs after Paclet manager is installed. This way, Paclet manager does not get wiped out by CleanSlate and you do not have ugly kernel crashes when opening Doc Center. I lost a lot of work because of this until I figured out this fix.

Daniel


Bill Rowe

unread,
Oct 8, 2012, 2:52:17 AM10/8/12
to
On 10/7/12 at 1:31 AM, dmaxw...@gmail.com (dr DanW) wrote:

>There are caveats to using CleanSlate in your init.m file in version
>6+ since the Paclet manager gets loaded after init.m. Put the line:

>DeclarePackage["Utilities`CleanSlate`",{"CleanSlate"}];

>in your init.m, and put the line

>CleanSlate[Verbose->False];

>at the top of your notebooks. DeclarePackage tees up the package to
>be loaded but it is not actually loaded until CleanSlate is
>evaluated in your notebook, which occurs after Paclet manager is
>installed. This way, Paclet manager does not get wiped out by
>CleanSlate and you do not have ugly kernel crashes when opening Doc
>Center. I lost a lot of work because of this until I figured out
>this fix.

An alternative would be to force loading of the Paclet manager.
That is at the end of my init.m file I have

<<PacletManager`;
<<Utilities`CleanSlate`;
SetOptions[CleanSlate,Verbose->False];

This way I don't need to add anything to the top of a notebook
when I start a session.


dgo...@aol.com

unread,
Oct 8, 2012, 2:52:27 AM10/8/12
to
On Sunday, October 7, 2012 1:40:32 AM UTC-4, dr DanW wrote:
> CleanSlate[] marks the state of memory on its first invocation. Subsequent calls to CleanSlate[] reset the memory to that state.

The previous writer says it resets the state to the state when the package was loaded.

Which is it?

(And that's why there should be documentation ...)

0 new messages