Re: Problem with evaluation of delayed rules
- To: mathgroup at smc.vnet.net
- Subject: [mg21548] Re: Problem with evaluation of delayed rules
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Sat, 15 Jan 2000 02:03:55 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <85mkef$1og@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Eckhard,
the sequence below works fine
symbols = {a, b, c, d};
expr = If[x > 0, Difference[1, 2], Difference[3, 4]];
expr1 = expr /. Difference[x_, y_] :> symbols[[x]] - symbols[[y]];
expr1 /. If[a_, b_, c_] :> If[a, Evaluate[b], Evaluate[c]]
Out[]=
If[x > 0, a - b, c - d]
just add a second rule to force the evaluation with
expr /. Difference[x_, y_] :> symbols[[x]] - symbols[[y]] /.
If[a_, b_, c_] :> If[a, Evaluate[b], Evaluate[c]]
Hope that helps
Jens
Eckhard Hennig wrote:
>
> 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
> -----------------------------------------------------------