MathGroup Archive 2002

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

Search the Archive

Re: Evaluation of Conditional Definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33809] Re: Evaluation of Conditional Definitions
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Fri, 19 Apr 2002 02:27:23 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

David,

The problem seems to stem from some internal optimization procedures.
I give a solution using Update and SetDelayed and then some information from
the Help Browser and the Book
First, a solution:

    Clear[g,a,UseDefinition];
    g[a]/;UseDefinition==True:=2  (*uses Update and SetDelayed*)
    expr:= (Update[g];g[a])

    UseDefinition=False;
    expr

        g[a]

    UseDefinition=True;
    expr

        2

    UseDefinition=False;
    expr

        g[a]

    UseDefinition=True;
    expr

        2

The Help Browser gives

- Update[symbol] tells Mathematica that hidden changes have been made which
could affect values associated with a symbol.
- Update[ ] specifies that the value of any symbol could be affected

- Update manipulates internal optimization features of Mathematica. It
should not need to be called except under special circumstances that rarely
occur in practice.
- One special circumstance is that changes in the value of one symbol can
affect the value of another symbol by changing the outcome of Condition
tests. In such cases, you may need to use Update on the symbol you think may
be affected.
- Using Update will never give you incorrect results, although it will slow
down the operation of the system.
-  See The Mathematica Book: Section 2.5.12.

The Book tells us
Some of the trickiest cases occur when you have rules that depend on
complicated /; conditions (see Section 2.3.5). One particularly awkward case
is when the condition involves a "global variable". Mathematica may think
that the evaluation is finished because the expression did not change.
However, a side effect of some other operation could change the value of the
global variable, and so should lead to a new result in the evaluation. The
best way to avoid this kind of difficulty is not to use global variables in
/; conditions. If all else fails, you can type Update[s] to tell Mathematica
to update all expressions involving s. Update[ ] tells Mathematica to update
absolutely all expressions.

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


----- Original Message -----
From: "David Park" <djmp at earthlink.net>
To: mathgroup at smc.vnet.net
<Hartmut.Wolf at T-Systems.de>; "Allan Hayes" <hay at haystack.demon.co.uk>
Subject: [mg33809] Evaluation of Conditional Definitions


> Dear MathGroup,
>
> I want to make a definition that will apply only if a global variable is
set
> to True. The following works, but only with an original input expression
and
> not on reevaluation of an expression.
>
> Clear[g, a, UseDefinition];
> g[a] /; UseDefinition == True := 2
>
> UseDefinition = False;
> expr = g[a]
> g[a]
>
> UseDefinition = True;
> g[a]
> 2
>
> But...
>
> expr
> g[a]
>
> Why does Mathematica stop before applying the definition to g[a]?
>
> I actually want to use this with an UpValue definition but run into the
same
> problem.
>
> Clear[f, a, UseDefinition];
> a /: f[a] /; UseDefinition == True := 2
>
> UseDefinition = False;
> expr = f[a]
> f[a]
>
> UseDefinition = True;
> f[a]
> 2
>
> expr
> f[a]
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>






  • Prev by Date: RE: More accuracy in Disk
  • Next by Date: FW: Evaluation of Conditional Definitions
  • Previous by thread: Evaluation of Conditional Definitions
  • Next by thread: FW: Evaluation of Conditional Definitions