       FW: Problem with evaluation of delayed rules

• To: mathgroup at smc.vnet.net
• Subject: [mg21565] FW: [mg21505] Problem with evaluation of delayed rules
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Sat, 15 Jan 2000 02:04:07 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Below is a possible solution. It is not entirely satisfactory because it
produces an error message (which you can just ignore) and because the final
form is not quite what you asked for, though in most cases it is
functionally equivalent.

In:=
symbols = {a, b, c, d};
In:=
expr = If[x > 0, Difference[1, 2], Difference[3, 4]];
In:=
sol = expr /. Difference[x_, y_] -> Unevaluated[ symbols[[x]] -
symbols[[y]]]
Part::"pspec": "Part specification \!\(x\) is neither an integer nor a list
\
of integers."
Part::"pspec": "Part specification \!\(y\) is neither an integer nor a list
\
of integers."
Out=
If[x > 0, {a, b, c, d}[] - {a, b, c, d}[],

{a, b, c, d}[] - {a, b, c, d}[]]
In:=
Clear[symbols]
In:=
sol /. x -> 1
Out=
a - b
In:=
sol /. x -> -1
Out=
c - d

The only problem I can see with this is that if your list symbols is very
long Out will also be long. However, there is no need for it to be
displayed at all until a value for x has been substituted.

> From: Eckhard Hennig <hennig at itwm.uni-kl.de>
To: mathgroup at smc.vnet.net
> Organization: ITWM
> Date: Fri, 14 Jan 2000 02:43:25 -0500 (EST)
> To: mathgroup at smc.vnet.net
> Subject: [mg21565] [mg21505] Problem with evaluation of delayed rules
>
> 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:= symbols = {a, b, c, d};
>
> and that we have the following expression that involves a function (here
> "If") with HoldXXX attribute (HoldAll, HoldRest, ...).
>
> In:= 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:= expr /. Difference[x_, y_] :> symbols[[x]] - symbols[[y]]
>
> then this is what I get:
>
> Out= If[x > 0, symbols[] - symbols[], symbols[] - symbols[]]
>
> 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 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 or Out[3new]. Well, the
> difference comes in as soon as you clear the value of "symbols" with
> Clear[symbols] and then evaluate Out 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 /. 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
>
> http://www.itwm.uni-kl.de/as/products/ai
> -----------------------------------------------------------
>
>
>
>

```

• Prev by Date: Re: Problem with evaluation of delayed rules(addendum)
• Next by Date: Re: Problem with evaluation of delayed rules
• Previous by thread: Re: Problem with evaluation of delayed rules
• Next by thread: Re: Problem with evaluation of delayed rules