Message Boards Message Boards

0
|
1717 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

ClearAll["`*"] meaning?

Posted 1 year ago

I read in Wolfram documentation that ClearAll["`*"], clears values and definitions of all symbols in the current context. Not sure what they mean by current context. Does this mean, for example, that y, x, t, h, l, and v, would all be cleared, in the following program?

ClearAll["`*"]
h=1; l=10; v=1;
y[x_,t_]=Sum[(8h/(n^2Pi^2))(2Sin[n Pi/4]-Sin[n Pi/2])Sin[n Pi x/l]Cos[n Pi v*t/l],{n,1,40,1}];
y2=Table[Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"},PlotLegends->StringJoin["t=", ToString[t]]],{t,0,2l/v,.02l/v}];
ListAnimate[y2]

If not, which symbols would be cleared by ClearAll["`"] given above?(I.e. which symbols would be considered the "current context")? Also, would this,

ClearAll["`Global"] 

clear every symbol in the program?

POSTED BY: Lewis Jones
Posted 1 year ago

Contexts work sort of like namespaces. They're basically just prefixes to variable names. Whenever you use a new symbol, the "true" name of that symbol is longer than the name you explicitly gave it. The backtick is reserved for indicating context "paths". So, in a fresh session, if you evaluate

Context[]

you should get

Global`

Global is the default context. You can find out what symbols have been defined with Names, and you can also specify a context to search in

Names["Global`*"]

{}

Names does pattern matching, so that * is a wildcard. So this should show you all symbols that have been defined in the Global context (there are none yet).

The system "remembers" any new symbols you use, and automatically assigns them to the current context. If you evaluate the following

a

the system now is aware of the symbol a, even though you didn't give it a value. Now try

Information["a"]

You should get a little panel that tells you the fulll name (Global`a). It would tell you any definitions, but right now there are none.

Now evaluate this

a = 7;
Information["a"]

and this time you should see that 7 has been assigned to a. That brings us to ClearAll. ClearAll removes definitions, but leaves the symbol in the system's "memory".

ClearAll[a];
Information["a"]

Notice that the system still knows about Global `a, but it has forgotten all definitions. ClearAll can work with raw symbols and with patterns. So, you could do this

ClearAll["Global`*"]

to conveniently clear definitions from all symbols in the Global context. And

ClearAll["`*"]

removes all definitions for symbols in the current context (which should still be Global if you've been following along).

You asked about

ClearAll["`Global"] 

That would clear definitions for the symbol Global in the current context. So, it would clear the symbol whose full name is Global `Global. Since we haven't defined such a symbol yet, nothing will actually happen.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract