[Date Index]
[Thread Index]
[Author Index]
RE: Re: Finding variables in a very long expression
*To*: mathgroup at smc.vnet.net
*Subject*: [mg31456] RE: [mg31410] Re: Finding variables in a very long expression
*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
*Date*: Wed, 7 Nov 2001 05:29:23 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
Dear Allan,
just a little bit, to continue your observations:
In[1]:= a = A[b];
In[2]:= Remove[A, b]
In[3]:= a
Out[3]= Removed["A"][Removed["b"]]
In[4]:=
Information["Removed", LongForm -> True]
>From In[4]:=
"Removed[string] is printed to indicate a symbol that has been removed."
>From In[4]:=
Attributes[Removed] = {Protected}
As you found, "Removed" cannot be substituted. Obviously the cause is,
that "Removed" is not a wrapper as you found out, but only a print form.
As you see, we can have two different forms of "Removed", a tangible and
an elusive variant:
In[5]:=
{a, Removed["z"]} /. HoldPattern[Removed] -> Resurrected
Out[5]= {Removed["A"][Removed["b"]], Resurrected["z"]}
In[6]:=
Block[{Removed = Resurrected}, {a, Removed["z"]}]
Out[6]= {Removed["A"][Removed["b"]], Resurrected["z"]}
In[7]:=
With[{Removed = Resurrected}, Evaluate[{a, Removed["z"]}]]
Out[7]= {Removed["A"][Removed["b"]], Resurrected["z"]}
In[8]:=
Names["Global`*"]
Out[8]= {"a", "Resurrected"}
However we can convert the elusive to the tangible:
In[9]:= ToExpression[ToString[a]] /. Removed -> Identity
Out[9]= A[b]
In[10]:= Names["Global`*"]
Out[10]= {"a", "A", "b", "Resurrected"}
Doing this, the symbols A, b have been created anew.
However a has not changed.
In[11]:= a
Out[11]= Removed["A"][Removed["b"]]
Now let us replace "by force":
In[12]:= aRes = ToExpression[ToString[a]]
Out[12]= Removed[A][Removed[b]]
(The "tangible" variant)
In[13]:=
pos2 = Position[aRes, Removed[_]]
Out[13]= {{0}, {1}}
Now it is possible to replace the "resurrected" symbols in
the old structure a:
In[16]:=
ReplacePart[a, aRes, pos2, Append[#, 1] & /@ pos2]
Out[16]= A[b]
The different expressions for the positions prove, that
the removed symbols (or some direct substitutes for them)
are still being there in a, at there original place,
marked however not wrapped.
Kind regards,
Hartmut Wolf
> -----Original Message-----
> From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Saturday, November 03, 2001 11:29 AM
> Subject: [mg31456] [mg31410] Re: Finding variables in a very long expression
>
>
> Ted:
> You say
> >If I had my way removing a symbol would make the kernel work
> as if the
> symbol
> > never had any values, but that's not the way it works.
>
> Some thoughts on this:
>
> We see Remove apparently wrapped round occurences of removed
> symbols in
> stored values
>
>
> a = A[b];
>
> Remove[b];
>
> a
>
> A[Removed[b]]
>
> Something like this is needed to preserve the stucture of the
> value, and to
> indicate the removal.
>
> But this is more than just wrapping
>
> FullForm[%]
>
> A[Removed["b"]]
>
> AtomQ[%]
>
> False
>
> and replacement does not work:
>
> a/."b"->3
>
> A[Removed[b]]
>
> Moreover, if I simply type and evaluate the result is not the same:
>
> AtomQ[Removed["b"]]
>
> False
>
> So something more is going on that I haven't sorted out yet.
>
> b does not now exist
>
> ?b
>
> Information::notfound: Symbol b not found.
>
> And we can re-create and set a value to b:
>
> b= 5
>
> 5
>
> without affecting the value of a:
>
> a
>
> A[Removed[b]]
>
> If we had just "make the kernel work as if the symbol never
> had any values"
> (which would seem to be what ClearAll[b] would have done)
> then this would
> not have been been possible.
>
>
>
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
>
> "Ersek, Ted R" <ErsekTR at navair.navy.mil> wrote in message
> news:9rq7ia$ill$1 at smc.vnet.net...
> > Jose Flanigan wanted to know how to find all variables in
> an expression.
> > Consider expr below.
> >
> > In[1]:=
> > expr=Sqrt[x*y]+x*z+w Sqrt[x]z^(1/n)+Pi/3;
> > w=v;
> >
> > The simple line below almost works, but the list includes
> Pi which isn't a
> > variable. The third argument of Cases here is a level
> specification. You
> > can read about Cases and level specification at my web site
> (URL below).
> >
> > In[3]:=
> > Union[Cases[expr, _Symbol, {0, -1}]]
> >
> > Out[3]=
> > {n, Pi, v, x, y, z}
> >
> >
> > The next line eliminates symbols such as Pi.
> >
> > In[4]:=
> > Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]
> >
> > Out[4]=
> > {n, v, x, y, z}
> >
> >
> > --- The Plot Thickens ---
> > At the 1999 Developer Conference Robby Villegas explained
> that this sort
> of
> > thing gets tricky when you are working with symbols that
> were previously
> > removed. Consider the lines below where the symbol (v) is
> removed. If I
> > had my way removing a symbol would make the kernel work as
> if the symbol
> > never had any values, but that's not the way it works.
> >
> > In[5]:=
> > Remove[v]
> >
> > In[6]:=
> > Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]
> >
> > Out[6]=
> > {n, Removed[v], x, y, z}
> >
> >
> > If you want to make sure your list of variables is free of
> Removed Symbols
> > use the next line (based on Robby Villegas talk).
> >
> > In[7]:=
> > Union[Cases[expr, _Symbol?( !NumericQ[#] &&
> NameQ[ToString[#]]& ), {0,
> > -1} ]]
> >
> > Out[7]=
> > {b, x, y, z}
> >
> >
> > ---
> > Regards,
> > Ted Ersek
> > Check Mathematica Tips, Tricks at
> > http://www.verbeia.com/mathematica/tips/Tricks.html
> >
> >
>
>
>
Prev by Date:
**Re: lists w/o double brackets**
Next by Date:
**Re: Aligning equal signs**
Previous by thread:
**Re: Finding variables in a very long expression**
Next by thread:
**RE: Finding variables in a very long expression (corrected)**
| |