Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Clear and Remove in Mathematica

In Mathematica, the documentation for ClearAll states:

ClearAll[symb1, symb2, ...]
clears values, definitions, attributes, messages, and defaults with symbols.

It also supports a similar format where it can clear any values / definitions which match an input string pattern:

ClearAll["form1", "form2", ...]

But there's also the function Remove, for which the documentation says:

Remove[symbol1, ...]
removes symbols completely, so that their names are no longer recognized by Mathematica.

It also supports the same pattern based string input that ClearAll supports.

To me, it seems like both functions accomplish the same exact thing. Is there any practical difference to using one or the other?

I know that if I give an attribute to a symbol, Clear won't remove it but ClearAll and Remove will. But it seems like Remove and ClearAll are doing the same thing.

like image 398
Mike Bailey Avatar asked Dec 14 '11 21:12

Mike Bailey


1 Answers

ClearAll leaves the symbol in the symbol table:

In[1]:= x=7;

In[2]:= ?x
Global`x

x = 7

In[3]:= ClearAll[x]

In[4]:= ?x
Global`x

Remove removes it from the symbol table:

In[5]:= Remove[x]

In[6]:= ?x

Information::notfound: Symbol x not found.

One reason to use Remove instead of ClearAll is if a symbol hides another symbol further down your $ContextPath. Here's a contrived example:

In[1]:= $ContextPath = { "Global`", "System`" };

In[2]:= Global`Sin[x_] := "hello" 

Sin::shdw: Symbol Sin appears in multiple contexts {Global`, System`}
    ; definitions in context Global`
     may shadow or be shadowed by other definitions.

In[3]:= Sin[1.0]

Out[3]= hello

In[4]:= ClearAll[Sin]

In[5]:= Sin[1.0]

Out[5]= Sin[1.]

In[6]:= Remove[Sin]

In[7]:= Sin[1.0]

Out[7]= 0.841471

Another reason to use Remove is that the notebook interface only includes known symbols when you choose Edit > Complete Selection (or on a Mac, press Command-K).

like image 88
rob mayoff Avatar answered Sep 24 '22 15:09

rob mayoff