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[1]:= symbols = {a, b, c, d}; In[2]:= expr = If[x > 0, Difference[1, 2], Difference[3, 4]]; In[3]:= 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[3]= If[x > 0, {a, b, c, d}[[1]] - {a, b, c, d}[[2]], {a, b, c, d}[[3]] - {a, b, c, d}[[4]]] In[4]:= Clear[symbols] In[5]:= sol /. x -> 1 Out[5]= a - b In[6]:= sol /. x -> -1 Out[6]= c - d The only problem I can see with this is that if your list symbols is very long Out[3] 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[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 > ----------------------------------------------------------- > > > >