[Date Index]
[Thread Index]
[Author Index]
RE: Re: Conditionals with multiple tests?(2) ++ evaluating CompoundExpression
*To*: mathgroup at smc.vnet.net
*Subject*: [mg24296] RE: [mg24170] Re: Conditionals with multiple tests?(2) ++ evaluating CompoundExpression
*From*: Wolf Hartmut <hwolf at debis.com>
*Date*: Fri, 7 Jul 2000 00:11:36 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
Please see below at mark:
> -----Original Message-----
> From: Allan Hayes [SMTP:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Thursday, June 29, 2000 4:51 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg24170] Re: Conditionals with multiple tests?(2)
>
> Re the role of CompoundExpression[e1,e2, ..., en]. I Copy from my reply to
> a
> personal message from A. E. Siegman:
>
> For example
>
> c = Which[True, a=2; b=3; 2 a b, . . . ]
>
> Will assign to c the value of Which[True, a=2; b=3; 2 a b, . . . ]; which,
> because of True, will be the value of
>
> a=2; b=3; 2 a b.
>
> (the FullForm of a=2; b=3; 2 a b is CompoundExpression[ a=2, b=3, 2 a
> b]).
>
> The value of an expression e is what it is eventually transformed into by
> the evaluation process in the presence of all the currently stored
> assignments.
>
> In our case, a=2; b=3; 2 a b evaluates via the following steps
> - evaluate a=2, giving
> CompoundExpression[ 2, b=3, 2 a b] and storing the assignment a
> =2;
> - evaluate b=3, giving
> CompoundExpression[ 2, 3, 2 a b] and storing the assignment b =3
> - evaluate 2 a b uses the previous stored assignments to a and b,
> giving
> CompoundExpression[ 2, 3, 12]
> - change to 12 (the value of the original a=2; b=3; 2 a b )
>
> However, you are more interested in the assignments for p1,p2 ..stored
> during the evaluation.
>
> **note added for this posting **:
>
> There is an extra bit to the evaluation in general: After the entries in
> CompountExpression[e1, e2, ,en] have been evaluated to give
> CompountExpression[e1*, e2*, ,en*] , this changes to en* AND THEN en* is
> put through the evaluation process to give en**. This shows up in the
> following comparison
>
> Clear[a,x]
>
> a; {x, x = 3}
>
> {3, 3}
>
> Clear[x]
>
> {x, x = 3}
>
> {x, 3}
>
> %
>
> {3, 3}
>
>
> --
> 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
>
> "Allan Hayes" <hay at haystack.demon.co.uk> wrote in message
> news:8jc62p$d84 at smc.vnet.net...
> > "A. E. Siegman" <siegman at stanford.edu> wrote in message
> > news:8j9dvs$523 at smc.vnet.net...
> > > Let's say I want to assign values to three variables p1, p2, p3 that
> > > will depend on five different (and nonoverlapping) tests test1 to test
> 5.
> > >
> > > One way to do this is obviously
> > >
> > > p1 = Which[test1, value11, test2, value12, . . . ]
> > > p2 = Which[test1, value21, test2, value22, . . . ]
> > > p3 = Which[test1, value31, test2, value32, . . . ]
> > >
> > > But a more compact and (for me anyway) neater approach is
> > >
> > > Which[test1, p1=value11; p2=value21; p3=value31,
> > > test2, p1=value21; p2=value22; p3=value32,
> > > test3, . . .
> > > test4, . . .
> > > test5, . . . ]
> > >
> > > Is this form legal? That is, can one use:
> > >
> > > Which[test1, expr1, test2, expr2, . . .]
> > >
> > > where expr1, expr2, . . . may be compound expressions?
> >
> >
> > A compound expression is a perfectly legitimate expression, so no
> problem
> > there.
> >
> > Assuming that no definitions produce side effects.
> > If all of your tests give either True or False and at least one gives
> True,
> > then the two ways will be equivalent. But otherwise they may not be
> > equivalent:
> >
> > Which[test1, v1, test2, v2 ... testn, vn] seems to evaluate as follows.
> > For i = 1,2,...in turn
> > * evaluate testi to testi*
> > * if testi* is True return the value of vi
> > * if testi* is False delete testi and vi
> > * if testi* is neither True nor False return the current Which[testi*,
> > vi..]
> >
> > * if Which[] is reached return Null.
> >
> > Check
> >
> > Clear["`*"]
> > F = False; T = True; U = vU; a = va; b = vb;
> >
> > {Which[F,a,T,b],
> > Which[F,a,U,b,T,b],
> > Which[F,a,F,b],
> > Which[]
> > }
> >
> > {vb, Which[vU, b, T, b], Null, Null}
> >
> >
> > --
> > 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
> >
>
[Hartmut Wolf]
Dear Allan,
fine analysis again! I wondered how to write a "CompoundExpression" which
gives the same result as the simple expression. It took me some time, now
here it is:
In[11]:= Attributes[myCompound] = HoldAll;
In[12]:= myCompound[x___, y_] := ReleaseHold[x; Hold[y]]
(perhaps we should define something for myCompound[] -- for
CompoundExpression[] it is Null)
In[13]:= Clear[x]
In[14]:= myCompound[Print["hello evaluator\nwhat you'r doing?"], {x, x = 3}]
>From In[14]:=
hello evaluator
what you'r doing?
Out[14]= {x, 3}
In[15]:= myCompound[x = 5, {x, x = 3}]
Out[15]= {5, 3}
In[16]:= Clear[x]
In[17]:= myCompound[{x, x = 3}]
Out[17]= {x, 3}
In[18]:= Clear[x]
In[19]:= Which[False, x = 5, True,
myCompound[Print["what's going on here?"], {x, x = 3}]]
>From In[19]:=
what's going on here?
Out[19]= {x, 3}
In[20]:= %
Out[20]= {3, 3}
Why is that all so? In section 2.5.4 The Mathematica Book tells: <...after
Evaluation of the arguments and having performed the transformations with
the attributes...> "If Mathematica finds a definition that applies, it
performs the corresponding transformation on the expression. The result is
another expression, which must then in turn be evaluated according to the
standard evaluation procedure."
Obviously there are no transformation rules for List (lists are "inert"). So
even in this case here, where the expression changes, no further evaluation
takes place:
In[21]:= Clear[x, y]
In[22]:= {x, y, y = Sequence[], x = 3, y, x}
Out[22]= {x, y, 3, 3}
In[23]:= %
Out[23]= {3, 3, 3}
In[24]:= ?y
>From In[24]:= "Global`y"
>From In[24]:= y = Sequence[]
To get the converse effect, that a simple expression behaves like in
CompoundExpression is simple:
In[25]:= Clear[x]
In[26]:= Identity[{x, x = 3}]
Out[26]= {3, 3}
So Indentity does _not_ deliver the object as it is. (Of course we also
could have used CompoundExpression instead)
Finally here another example
In[38]:= Remove[y]
In[39]:= y /: {a___, y, b___} := {a, b}
In[40]:= Clear[x]
In[41]:= {y, x, x = 3}
Out[41]= {3, 3}
The upvalue for y triggers the extra evaluation, compared with In[22] so we
also have two alternatives here!
Kind regards,
Hartmut
> > "A. E. Siegman" <siegman at stanford.edu> wrote in message
> > news:8j9dvs$523 at smc.vnet.net...
> > > Let's say I want to assign values to three variables p1, p2, p3 that
> > > will depend on five different (and nonoverlapping) tests test1 to test
> 5.
> > >
> > > One way to do this is obviously
> > >
> > > p1 = Which[test1, value11, test2, value12, . . . ]
> > > p2 = Which[test1, value21, test2, value22, . . . ]
> > > p3 = Which[test1, value31, test2, value32, . . . ]
> > >
> > > But a more compact and (for me anyway) neater approach is
> > >
> > > Which[test1, p1=value11; p2=value21; p3=value31,
> > > test2, p1=value21; p2=value22; p3=value32,
> > > test3, . . .
> > > test4, . . .
> > > test5, . . . ]
> > >
> > > Is this form legal? That is, can one use:
> > >
> > > Which[test1, expr1, test2, expr2, . . .]
> > >
> > > where expr1, expr2, . . . may be compound expressions?
> > >
> > > (I would say that The Mathematica Book is not at all clear on this
> > > point, as regards either Which[] or If[].)
> > >
> > > If not, is there a legal way to implement the basic objective?
> > >
> >
> >
> >
>
>
Prev by Date:
**Re: Re: 1/Trig function - help**
Next by Date:
**Re: With[{software=Mathematica}, Frustration]**
Previous by thread:
**"Best" ComplexityFunction Setting ?**
Next by thread:
**Problem with antiderivtive of ArcSec**
| |