Re: Interaction of Remove and Global variables in a Module

*To*: mathgroup at smc.vnet.net*Subject*: [mg120112] Re: Interaction of Remove and Global variables in a Module*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Fri, 8 Jul 2011 04:55:54 -0400 (EDT)*References*: <201107071128.HAA15173@smc.vnet.net>*Reply-to*: drmajorbob at yahoo.com

Short answer? Never use Remove. You don't need it, and it doesn't do what you think. Longer answer? Consider the following (input cell followed by output cell, far less messy than all that In[] and Out[] business!): f[b_] := (a[t_] = b*t^2;) ?f Global`f f[b_]:=(a[t_]=b t^2;) f[4] ?f Global`f f[b_]:=(a[t_]=b t^2;) Remove[a] ?f Global`f f[b_]:=(Removed[a][t_]=b t^2;) Removed[a][t] Removed[a][t] Remove[a] doesn't simply remove all definitions and attributes for the symbol a. It also replaces a with Removed[a] in all OTHER symbol definitions that mentioned a. Hence, when f is used after removing a, a is not redefined, because THE DEFINITION OF f HAS CHANGED. Solution?? Never use Remove. I never do, and I haven't missed it one bit. Clear, ClearAll, or a = . are all you need. Bobby On Thu, 07 Jul 2011 06:28:13 -0500, blamm64 <blamm64 at charter.net> wrote: > This is what I get for querying SetDelayed > > In[1]:= ?SetDelayed > lhs:=rhs assigns rhs to be the delayed value of lhs. rhs is maintained > in an unevaluated form. When lhs appears, it is replaced by rhs, > evaluated afresh each time. >> > > Note particularly the above reads AFRESH EACH time. It appears then > the following is inconsistent behavior based on the above description: > > In[2]:= f[b_]:=Module[{t},a[t_]=b*t^2;] > In[3]:= a[t] > Out[3]= a[t] > In[4]:= f[3] > In[5]:= a[t]//InputForm > Out[5]//InputForm= > 3*t^2 > In[6]:= f[5] > In[7]:= a[t]//InputForm > Out[7]//InputForm= > 5*t^2 > In[8]:= Remove[a] > In[9]:= f[4] > In[10]:= a[t]//InputForm > Out[10]//InputForm= > a[t] > > Apparently AFRESH is not an accurate description of how SetDelayed > operates in this case, or I am missing something about this particular > interaction of Module, Remove, and global variables inside Modules. > > However, if I go back, after executing the last line above (<a> has > been Removed), and place the cursor in the input line where <f> is > defined and hit Enter, which I thought would be identical to just > evaluating <f> AFRESH again, and then execute the <f[4]> line again, > then the global <a> definition is re-constituted. > > The documentation for Remove reads the name is no longer recognized by > Mathematica. My understanding is that if the same name is defined > AFRESH, it will once again be recognized. > > So if anyone would let me know what I am missing, regarding why the > definition of <a> is not created AFRESH each time <f> is evaluated, I > would appreciate it. > > Please don't construe the definition of <f> as my way of > 'parameterizing' a function definition, I just use that definition to > convey the apparent inconsistency. > > -Brian L. > -- DrMajorBob at yahoo.com

**References**:**Interaction of Remove and Global variables in a Module***From:*blamm64 <blamm64@charter.net>