Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Evaluation of Conditional Definitions(2)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33958] Re: Re: Evaluation of Conditional Definitions(2)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 24 Apr 2002 01:22:29 -0400 (EDT)
  • References: <aa3gic$804$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

The method referred to below seems to have been sent to Hartmut and David
directly, but not posted to the group. I post it below with an addition.

David,
Here is another way, one that lets us use either Set or SetDelayed.
The statement, from the Help Browser,

- Using Update will never give you incorrect results, although it will slow
down the operation of the system.

though technically correct, it looks in need of a little more explanation,
since the result is changed.


    Clear[g,a,UseDefinition];
    g[a]/;UseDefinition==rue:=2
    expr/;(Update[g];True)=g[a];   (*uses Set, if you dont't want to specify
on the left use Update[]*)

    UseDefinition=False;
    expr

        g[a]

    UseDefinition=True;
    expr

        2

    UseDefinition=False;
    expr

        g[a]

    UseDefinition=True;
    expr

        2



--
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
news:aa3gic$804$1 at smc.vnet.net...
> Dear David and Allan,
>
> this is a very ingenious method to force the Update of g when accessing
> dependend expressions. It might however become a bit nasty to follow all
> involved dependencies when assigning values, e.g. for the case where you
> have many conditional definitions all dependend on the same global
variable.
> Here I report on an observation I made, where update is forced by only
> introducing a condition on that global variable:
>
> In[1]:= Clear[f, a, UseDefinition, expr]
> In[2]:= f[a] /; UseDefinition := 2
>
> In[3]:= UseDefinition = False;
> In[4]:= expr := f[a] /; UseDefinition
>         expr := f[a]
>
> (* it only works if the Condition is attached to the rhs *)
>
> In[6]:= Information["expr", LongForm -> False]
>         Global`expr
>         expr := f[a] /; UseDefinition
>         expr := f[a]
>
> In[7]:= expr
> Out[7]= f[a]
>
> In[8]:= UseDefinition = True;
> In[9]:= expr
> Out[9]= 2
>
> In[10]:= UseDefinition = False;
> In[11]:= expr
> Out[11]= f[a]
>
> In[12]:= UseDefinition = True;
> In[13]:= expr
> Out[13]= 2
>
> In[14]:= UseDefinition = False;
> In[15]:= expr
> Out[15]= f[a]
>
> Of course all this might be accidential behaviour and unreliable. What
> bothers me is: a precise understanding of the exact evaluation sequence is
> fundamental for mastering programming. Quite obviously mine has shown to
> have holes.
>
>
>
> David, possibly for your application it might suffice to assign NValues to
> your symbol. So you might easily decide when you want to have kept the
> results of a calculation in symbolical or when in numerical form:
>
> In[126]:= Clear[f, g, a]
>
> In[127]:= N[f[a], ___] = 2
>           N[g[a], ___] = 3
> Out[127]= 2
> Out[128]= 3
>
> In[129]:= NValues /@ {f, g}
> Out[129]= {{HoldPattern[N[f[a], ___]] :> 2},
>            {HoldPattern[N[g[a], ___]] :> 3}}
>
> In[130]:= expr := f[a] + g[a]
>
> In[131]:= expr
> Out[131]= f[a] + g[a]
>
> In[141]:= expr + 5 // N
> Out[141]= 10.
>
> Observe that the result cannot be a precise number, yet of any precision.
>
>
> Another application:
>
> In[142]:=
> a = {{Cos[\[CurlyPhi]], Sin[\[CurlyPhi]]},
>      {-Sin[\[CurlyPhi]], Cos[\[CurlyPhi]]}}
>
> In[144]:= N[\[CurlyPhi]] = \[Pi]/3;
>
> In[145]:= HoldForm[Transpose[a].{1, 0}] // TraditionalForm
> Out[145]//TraditionalForm=
> \!\(\*FormBox[TagBox[\(a\^T . {1, 0}\), HoldForm], TraditionalForm]\)
>
> In[146]:= % // ReleaseHold
> Out[146]=
> {Cos[\[CurlyPhi]], Sin[\[CurlyPhi]]}
>
> In[147]:= % // N
> Out[147]= {0.5, 0.866025}
>
> In[149]:= \!\(\@3/2 // N\)
> Out[149]= 0.866025
>
> yours, Hartmut
>
>
>
> > -----Original Message-----
> > From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> > Sent: Friday, April 19, 2002 8:28 AM
> > To: mathgroup at smc.vnet.net
> > Subject: [mg33958]  Re: Evaluation of Conditional Definitions(2)
> >
> >
> > David,
> > Here is another way, one that lets us use either Set or SetDelayed.
> > The statement, from the Help Browser,
> >
> > - Using Update will never give you incorrect results,
> > although it will slow
> > down the operation of the system.
> >
> > though technically correct, looks in need of a little more
> > explanation,
> > since the result is changed.
> >
> >
> >     Clear[g,a,UseDefinition];
> >     g[a]/;UseDefinition==rue:=2
> >     expr/;(Update[g];True)=g[a];   (*uses Set*)
> >
> >     UseDefinition=False;
> >     expr
> >
> >         g[a]
> >
> >     UseDefinition=True;
> >     expr
> >
> >         2
> >
> >     UseDefinition=False;
> >     expr
> >
> >         g[a]
> >
> >     UseDefinition=True;
> >     expr
> >
> >         2
> >
> > 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
> >
> >
> >
> >
>
>




  • Prev by Date: RE: Pattern matching
  • Next by Date: Help... Algorithm
  • Previous by thread: RE: Re: Evaluation of Conditional Definitions(2)
  • Next by thread: Placing parameters in memory