MathGroup Archive 2000

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

Search the Archive

Re: Problem with evaluation of delayed rules(addendum)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21558] Re: Problem with evaluation of delayed rules(addendum)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 15 Jan 2000 02:04:02 -0500 (EST)
  • References: <85mkef$1og@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Eckhard,
I should have used HoldPattern in my earlier reply:

symbols = {a, b, c, d};

expr = If[x > 0, Difference[1, 2], Difference[3, 4]];

The touble arises if we have, for example,

Difference[__] = 5;

The earlier code then gives

expr /. Cases[expr,
    Difference[x_, y_] :>
      (Difference[x, y] -> symbols[[x]] - symbols[[y]]),
    Infinity]

    If[x > 0, Difference[1, 2], Difference[3, 4]]

But with HoldPattern to prevent the evaluation of Difference[..] we get

expr /. Cases[expr,
    HoldPattern[Difference[x_, y_]] :>
      (HoldPattern[Difference[x, y]] -> symbols[[x]] - symbols[[y]]),
    Infinity]

    If[x > 0, a - b, c - d]


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


"Eckhard Hennig" <hennig at itwm.uni-kl.de> wrote in message
news:85mkef$1og at smc.vnet.net...
> Hi, I would greatly appreciate any elegant solutions to the following type
> of problem.
>
> Assume that we have a list of symbols, e.g.
>
> In[1]:= symbols = {a, b, c, d};
>
> and that we have the following expression that involves a function (here
> "If") with HoldXXX attribute (HoldAll, HoldRest, ...).
>
> In[2]:= expr = If[x > 0, Difference[1, 2], Difference[3, 4]];
>
> In the above expression, I want to replace all objects of the form
> Difference[i, j] by differences of the two entries i and j from the list
> "symbols". If I use a delayed rule as follows
>
> In[3]:= expr /. Difference[x_, y_] :> symbols[[x]] - symbols[[y]]
>
> then this is what I get:
>
> Out[3]= If[x > 0, symbols[[1]] - symbols[[2]], symbols[[3]] -
symbols[[4]]]
>
> Due to the HoldXXX attribute of the "If" function, the right-hand side of
> the delayed rule remains unevaluated. However, I need the result of the
rule
> to be evaluated BEFORE it is inserted into expr, i.e. the result I want
for
> In[3] is:
>
> Out[3new]= If[x > 0, a - b, c - d]
>
> Does there exist any (simple) approach to forcing Mathematica 3.0/4.0 to
> simplify the result of a delayed rule as soon as the rule applies to a
> subexpression of a held expression (please note the conditions below)? One
> may argue that it doesn't make a difference in the end whether result of
> rewriting expr is given in the form of Out[3] or Out[3new]. Well, the
> difference comes in as soon as you clear the value of "symbols" with
> Clear[symbols] and then evaluate Out[3] for some x.
>
> Condition 1: Temporarily removing any HoldXXX attributes from expr or any
of
> its subexpressions is not a solution because I only want the results of
> rules to be simplified. No further simplification of held subexpressions
> must be performed after rule application. For example,
>
>   Out[3] /. If -> MyIf /. MyIf -> If
>
> yields Out[3new], but this approach does not count as a solution.
>
> Condition 2: The solution must not be limited to a predefined set of
> functions with HoldXXX attribute, say If and Which.
>
> Best regards,
>
>   Eckhard
>
> -----------------------------------------------------------
> Dipl.-Ing. Eckhard Hennig      mailto:hennig at itwm.uni-kl.de
> Institut fuer Techno- und Wirtschaftsmathematik e.V. (ITWM)
> Erwin-Schroedinger-Strasse,  67663 Kaiserslautern,  Germany
>   Voice: +49-(0)631-205-3126    Fax: +49-(0)631-205-4139
>     http://www.itwm.uni-kl.de/as/employees/hennig.html
>
>      ITWM - Makers of Analog Insydes for Mathematica
>         http://www.itwm.uni-kl.de/as/products/ai
> -----------------------------------------------------------
>
>
>
>



  • Prev by Date: Re: Re: Verifying PrimeQ for n >10^16
  • Next by Date: FW: Problem with evaluation of delayed rules
  • Previous by thread: NDSolve: Distinguishing equations
  • Next by thread: Re: Plotting A verticle line?