Re: Is it possible to access internal variables? [CompoundExpression]

• To: mathgroup at smc.vnet.net
• Subject: [mg34794] Re: Is it possible to access internal variables? [CompoundExpression]
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Fri, 7 Jun 2002 01:08:57 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Dear Hartmut,

The description of the evaluation of CompoundExpression in
http://library.wolfram.com/mathgroup/archive/1999/Feb/msg00460.html
was corrected in
http://library.wolfram.com/mathgroup/archive/1999/Feb/msg00440.html

(although the latter was sent a day later, for some reason it appears
earlier in the MathGroup numbering).

Here is a sketch of the evaluation ( e* denotes the value of e)

CompoundExpression[e1, Evaluate[e2], e3]
CompoundExpression[e1, e2*, e3] (*evaluate Evaluate[.].. in order*)
CompoundExpression[e1*, e2*,  e3](*restart; evaluate entries in order*)
CompoundExpression[e1*, e2**,  e3]
CompoundExpression[e1*, e2**,  e3*]
e3*
e3**   (*evaluate e3* since it is not the same as
CompoundExpression[e1*, e2**,  e3*]
*)

Clear[pr,a];

CompoundExpression[
Print[pr = Print], Evaluate[pr[Print[2]]], {a, a = 4}
]

2            (* from evaluating Evaluate[...] to pr[Null] *)

Print       (* from evaluating Print[pr = Print] *)

Null        (* from evaluating pr[Null] *)

{4,4}        (* from the evaluation of {a, 4} , after evaluating a=4*)

SIMULATION with CE

SetAttributes[CE, HoldAll]
CE[x__]:= Last[{x}]
CE[]:=Null

Clear[pr,a];

CE[
Print[pr = Print], Evaluate[pr[Print[2]]], {a,a=4}
]

2

Print

Null

{4,4}

--
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

"Wolf, Hartmut" <Hartmut.Wolf at t-systems.com> wrote in message
>
> Hello Bobby,
>
> Two things: (1) as Hob Hanlon has pointed out, I missed to assign to v at
> definiton of h[x_], which of course explains everything. Aside from being
> this my standard blunder mixing up Append with AppendTo in my mind from my
> first day on with Mathematica, I did not look close enough, and finally:
> there are no mysteries. Sorry for disturbing you.
>
> (2) What considers CompoundExpression: this is no bug for shure; instead
we
> simply have to recognize the precise semantics of (e1; e2; ...; en)
>
> The expressions e1, e2, up to en are executed in order, then the result of
> en is assigned to CompoundExpression[e1, e2, ..., en] and then this is
> evaluated again. Normaly, this has no visible effect since the execution
> environment of en when evaluated within the CompoundExpression is the same
> as that of the CompoundExpression when it returns; yet this is not enough.
> Here, since FindMinimum fails it returns itself unevaluated (a standard
> behaviour), this now becomes the value of CompoundExpression and such
> FindMinimum will start anew.
>
> This all conforms to the execution sequence as specified for Mathematica.
We
> certainly should have internalized this precisely and permanently (as a
> morning prayer). However, we are human beings, and build up our own rules
in
> our heads and take many shortcuts. It's there where we often become
> irritated. We just have concede that
>
>  In[1]:= expr1
>
>  In[2]:= expr2
>
> is *not* the same as
>
>  In[1]:= expr1;
>          expr2
>
> As I mentioned I've seen a prior thread to this theme with a prominent
> contribution of Allan Hayes, e.g.
> http://library.wolfram.com/mathgroup/archive/1999/Feb/msg00460.html or see
> http://library.wolfram.com/mathgroup/archive/2000/Jul/msg00115.html .
>
> BTW esp. with Block, things are quite the same, and quite common here,
since
> the execution
> environment within Block almost always differs form that one outside: we
> have to anticipate the transformations to the _result_ of Block, as here
to
> the _result_ of CompoundExpression.
>
> --
> Hartmut
>
>
> > -----Original Message-----
> > From: DrBob [mailto:majort at cox-internet.com]
To: mathgroup at smc.vnet.net
> > Sent: Tuesday, June 04, 2002 5:59 PM
> > Cc: 'David Park'; BobHanlon at aol.com
> > Subject: [mg34794] RE:  Is it possible to
> > access internal variables?
> >
> >
> > If compound expressions are not evaluated in order, how can Modules or
> > Blocks work?  Aren't they compound expressions?  What makes g and h
> > different?
> >
> > These are serious bugs.
> >
> > I feel like we're walking on quicksand, now.
> >
> > Bobby
> >
> > -----Original Message-----
> > From: Wolf, Hartmut [mailto:Hartmut.Wolf at t-systems.com]
To: mathgroup at smc.vnet.net
> > Sent: Tuesday, June 04, 2002 3:45 AM
> > Subject: [mg34794]  Is it possible
> > to access
> > internal variables?
> >
> >
> > Hello Bob,
> >
> > there is something mysterious with your proposal that disturbs me
> > somewhat.
> > See my records:
> >
> > In[1]:= f[x_] := (x - 3)(x - 4)
> >
> > In[2]:= g[x_] := Module[{t = f[x]}, v = Append[v, {x, t}]; t];
> > In[3]:= v = {}; FindMinimum[g[x], {x, 2, 1, 3}]
> > >From In[3]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > >From In[3]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > Out[3]=
> > FindMinimum[g[x], {x, 2, 1, 3}]
> >
> > In[4]:= v
> > Out[4]=
> > {{2., 2.}, {2., 2.}, {2.03136, 1.9069}, {2.05074, 1.85035}, {2.18642,
> >     1.4755}, {3., 0.}, {2., 2.}, {2., 2.}, {2.03136, 1.9069},
> > {2.05074,
> >     1.85035}, {2.18642, 1.4755}, {3., 0.}}
> >
> > In[5]:= g[x]
> > Out[5]= (-4 + x) (-3 + x)
> >
> > In[6]:= ?g
> > Global`g
> > g[x_] := Module[{t = f[x]}, v = Append[v, {x, t}]; t]
> >
> > Apart from the fact that FindMinimum is restarted and that the first
> > value
> > appears twice, this seems to be what you intended. It doesn't
> > help Arny
> > though, as the minimum value -- what he supposed to be that x occuring
> > in
> >
> > But there seems to be something special with g, if I try with h it
> > doesn't
> > work:
> >
> > In[7]:= h[x_] := Module[{t = f[x]}, Append[v, {x, t}]; t];
> > In[8]:= v = {}; FindMinimum[h[x], {x, 2, 1, 3}]
> > >From In[8]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > >From In[8]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > Out[8]=
> > FindMinimum[h[x], {x, 2, 1, 3}]
> >
> > In[9]:= v     (* v is empty *)
> > Out[9]= {}
> >
> > In[10]:= h[x]
> > Out[10]= (-4 + x) (-3 + x)
> >
> > In[11]:= ?h
> > Global`h
> > h[x_] := Module[{t = f[x]}, Append[v, {x, t}]; t]
> >
> >
> > Now observe:
> >
> > In[12]:= FindMinimum[Print[x]; f[x], {x, 2, 1, 3}]
> > >From In[12]:= x
> > >From In[12]:= 2.
> > >From In[12]:= 2.03136
> > >From In[12]:= 2.05074
> > >From In[12]:= 2.18642
> > >From In[12]:= 3.
> > >From In[12]:= FindMinimum::"regex": "Reached the point
> > \!\({3.5000000000000018`}\) which is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > Out[12]= FindMinimum[Print[x]; f[x], {x, 2, 1, 3}]
> >
> > The Argument of FindMinimum is evaluated first with symbolic x (to get
> > at
> > the derivative I suppose), later, at the second call, it and
> > such also t
> > =
> > (-4 + x) (-3 + x) get a numeric value, and therefore the values are
> > introduced into v when appending {x,t}, so the start value
> > gets doubled.
> >
> >
> > The restart of the minimization seems to be triggered by the compound
> > expression v = {}; FindMinimum[...] in the main loop (not within Find
> > Minimum)
> >
> > In[19]:= h5[x_] := Module[{t = f[x]}, Print[x]; t];
> >
> > In[20]:= v = {}; FindMinimum[h5[x], {x, 2, 1, 3}]
> > >From In[20]:= x
> > >From In[20]:= 2.
> > >From In[20]:= 2.03136
> > >From In[20]:= 2.05074
> > >From In[20]:= 2.18642
> > >From In[20]:= 3.
> > >From In[20]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > >From In[20]:= x
> > >From In[20]:= 2.
> > >From In[20]:= 2.03136
> > >From In[20]:= 2.05074
> > >From In[20]:= 2.18642
> > >From In[20]:= 3.
> > >From In[20]:=
> > FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\)
> > which
> > is \
> > outside the region \!\({\({1.`, 3.`}\)}\)."
> > Out[20]= FindMinimum[h5[x], {x, 2, 1, 3}]
> >
> > Any expression e.g. (3; FindMinimum[...]) does that too; conversely if
> > you
> > initialize v in an extra line, there will be no restart. This may have
> > something to do with peculiarities of compound expression, sometimes
> > starting a re-evaluation. (This had been reported by Allan Hayes some
> > years
> > ago.) See:
> >
> > In[65]:= x = 5;
> >
> > In[66]:= x =.; Print[x]; {x, x = 3, x}
> > >From In[66]:= x
> > Out[66]= {3, 3, 3}
> >
> > In[67]:= x =.
> > In[68]:= {x, x = 3, x}
> > Out[68]= {x, 3, 3}
> >
> > Anyways, as shown above, Arny's desire to get at 'internal values' of
> > FindMinimum seems to be hopeless. My advice would be to check for the
> > error
> > message and then possibly increase the search range (by a certain
> > tolerance)
> > and restart (only a finite number of times).
> >
> > --
> > Hartmut
> >
> >
> > > -----Original Message-----
> > > From: BobHanlon at aol.com [mailto:BobHanlon at aol.com]
To: mathgroup at smc.vnet.net
> > > Sent: Sunday, June 02, 2002 7:15 AM
> > > Subject: [mg34794]  Is it possible to access
> > internal
> > > variables?
> > >
> > >
> > >
> > > In a message dated 6/1/02 6:18:58 AM,
> > > someone at somewhere.sometime writes:
> > >
> > > >I am minimizing a function which only has real values
> > > >between 1 and -1,
> > > most
> > > >of the time... occasionally its range is different and
> > > unknown, (but anyway
> > > >not far from 1 and -1).  I could write a routine using Check
> > > to catch errors
> > > >and then expand the range over which FindMinimum is allowed
> > > to search,
> > > >but
> > > >since FindMinimum seems to be getting the appropriate values
> > > anyway - it
> > > >tells me the values in its error message -  I was wondering
> > > if there weren't
> > > >some way to get at those values and use them without
> > > bothering to write
> > > >said routine.  I was thinking of analysing 'MessageList' or
> > > '\$MessageList',
> > > >but was thinking there might be some easier way.
> > > >
> > > >Aren't variables within packages accessible via their long
> > > names, e.g.
> > > >`package`private`variablename or something like that?  Does
> > > anyone have
> > > >any suggestions?
> > > >
> > >
> > > f[x_] := (x-3)(x-4);
> > >
> > > g[x_] := Module[{t = f[x]}, v=Append[v, {x,t}]; t];
> > >
> > > g is the same function as f except that calls to g are recorded in v
> > >
> > > v={}; FindMinimum[g[x], {x, 2}]
> > >
> > > {-0.25, {x -> 3.5}}
> > >
> > > FindMinimum called g at the following values of x:
> > >
> > > v[[All,1]]
> > >
> > > {2., 2., 2.0313609375, 2.0507430627940644,
> > >
> > >   2.1864179398525154, 3.136142079261673, 3.500000000000001,
> > >
> > >   3.838316500294484, 3.5, 3.3180710396308366,
> > >
> > >   3.4999904004703097, 3.5, 3.499999994722301}
> > >
> > >
> > > Bob Hanlon
> > > Chantilly, VA  USA
> > >
> >
> >
> >
>

```

• Prev by Date: RE: Use of ShowProgress Option output
• Next by Date: No "Arrow3D" ?
• Previous by thread: RE: Is it possible to access internal variables? [CompoundExpression]
• Next by thread: Re: Is it possible to access internal variables? [CompoundExpression]