Re: delayed rule evaluation order
- To: mathgroup at smc.vnet.net
- Subject: [mg68022] Re: [mg67967] delayed rule evaluation order
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 20 Jul 2006 06:05:02 -0400 (EDT)
- References: <11710934.1153306359682.JavaMail.root@eastrmwml07.mgt.cox.net> <acbec1a40607190514x62d0f07byeb91cf8b496bca33@mail.gmail.com> <3BA57237-C0B6-4C71-A8B0-3481DCDBC464@mimuw.edu.pl> <acbec1a40607190919p3efa8b30x4ae0cb6278444931@mail.gmail.com> <330E5DE6-73D2-4844-BACD-8D96B736EA76@mimuw.edu.pl> <F2BDC55B-557A-4968-B933-18548988AD0B@mimuw.edu.pl> <674A693A-FA92-4E9F-86A0-EA8214BD9FAC@mimuw.edu.pl>
- Sender: owner-wri-mathgroup at wolfram.com
I can see now what went wrong. The problem is with the use of ExactNumberQ. Here is an approach that works: f[1] = 1; f[n_Integer] /; n > 1 = n f[n - 1]; InputForm[z = Trace[f[2]]]; v = z /. {blah_Integer :> SetPrecision[blah, 3]}; v/.p_SetPrecision:> With[{eval = p}, eval /; True] {f(2.00),{2.00>1.00,True},2.00 f(-1.00+2.00),{{-1.00+2.00,1.00},f( 1.00),1.00},2.00 1.00,2.00} Of course if you have Rationals in your expression and other non- exact numbers you will have to deal with these too in a similar way. Andrzej Kozlowski On 19 Jul 2006, at 19:08, Andrzej Kozlowski wrote: > O maybe that is not so ideal after all, since f got evaluated. You > can of course combine it with the Block technique I used earlier. > > Andrzej > > > On 19 Jul 2006, at 19:03, Andrzej Kozlowski wrote: > >> Here is another way, which you may like better. It uses the blah >> approach and the Trott-Strzebonski partial evaluation technique. >> >> f[1] = 1; >> f[n_Integer] /; n > 1 = n f[n - 1]; >> InputForm[z = Trace[f[2]]]; >> >> v = z /. {blah_?ExactNumberQ :> SetPrecision[blah, 3]}; >> >> >> >> v/.p_SetPrecision:> With[{eval = p}, eval /; True] >> >> >> {2.00,{2.00>1.00,True},2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} >> >> This must be finally what you wanted, isn't it? >> >> Andrzej Kozlowski >> >> >> >> On 19 Jul 2006, at 18:50, Andrzej Kozlowski wrote: >> >>> It's much easier to answer questions if the persons who pose them >>> explain clearly what they mean. >>> >>> The most obvious way to modify my code seems to me to be: >>> >>> >>> f[1] = 1; >>> f[n_Integer] /; n > 1 = n f[n - 1]; >>> InputForm[z = Trace[f[2]]]; >>> >>> >>> Block[{f=g},z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f >>> >>> >>> {f(2.00),{True,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >>> 2.00,2.00} >>> >>> Now f is not evaluated. The only thing you might still complain >>> about is that {2 > 1, True} evaluated to {True,True}. If you >>> really care about this you, can prevent it in various ways, for >>> example: >>> >>> >>> Block[{f=g}, >>> z/.{HoldForm[a_]/; >>> FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f >>> >>> >>> {f(2.00),{2>1,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >>> 2.00,2.00} >>> >>> or >>> >>> >>> Block[{f=g,Greater=greater}, >>> z/.{HoldForm[a_]/; >>> FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.{g- >>> >f,greater->Greater} >>> >>> >>> {f(2.00),{2.00>1.00,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >>> 2.00,2.00} >>> >>> >>> There are many other possibilities. >>> If there is still anything you do not like than it probably means >>> you still have not explained completely what you want. >>> >>> Andrzej Kozlowski >>> >>> On 19 Jul 2006, at 18:19, Chris Chiasson wrote: >>> >>>> >>>> What I want it to do is be able to replace numbers that aren't >>>> direct >>>> arguments of HoldForm (maybe they are nested a few levels deep, >>>> etc). >>>> >>>> In the example you sent me, f is evaluated - which is >>>> undesirable for me. >>>> >>>> On 7/19/06, Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote: >>>>> *This message was transferred with a trial version of >>>>> It seems to me that a variant of my first suggestion works fine: >>>>> >>>>> In[1]:= >>>>> f[1]=1; >>>>> f[n_Integer]/;n>1=n f[n-1]; >>>>> InputForm[z=Trace[f[2]]]; >>>>> InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] >>>>> >>>>> Out[4]//InputForm= >>>>> {HoldForm[2.`2.9999999999999973], >>>>> {HoldForm[True], HoldForm[True]}, >>>>> HoldForm[2.`2.9999999999999973], >>>>> {{HoldForm[1.`2.9999999999999973], >>>>> HoldForm[1.`2.9999999999999973]}, >>>>> HoldForm[1.`2.9999999999999973], >>>>> HoldForm[1.`2.9999999999999973]}, >>>>> HoldForm[2.`2.9999999999999973], >>>>> HoldForm[2.`2.9999999999999973]} >>>>> >>>>> Or is this not what you wanted? >>>>> >>>>> Andrzej Kozlowski >>>>> >>>>> >>>>> >>>>> On 19 Jul 2006, at 14:14, Chris Chiasson wrote: >>>>> >>>>> > Thanks to Kozlowski's, Hanlon's and Pein's solutions (haven't >>>>> received >>>>> > any others so far), I am now using this type of replacement: >>>>> > >>>>> > Hold[5.55555555]/.{blah_?InexactNumberQ:> >>>>> > junk[SetPrecision[blah,3]]}/.junk->Evaluate >>>>> > >>>>> > Hold[5.56] >>>>> > >>>>> > However, this still does not totally work on Trace's output: >>>>> > >>>>> > In[1]:= >>>>> > f[1]=1; >>>>> > f[n_Integer]/;n>1=n f[n-1]; >>>>> > InputForm[z=Trace[f[2]]] >>>>> > >>>>> > Out[3]//InputForm= >>>>> > {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm >>>>> [2*f[-1 >>>>> > + 2]], >>>>> > {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, >>>>> > HoldForm[2*1], HoldForm[2]} >>>>> > >>>>> > In[4]:= >>>>> > InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah, >>>>> 3]]}/.junk- >>>>> > >Evaluate] >>>>> > >>>>> > Out[4]//InputForm= >>>>> > {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate >>>>> [SetPrecision[2, >>>>> > 3]] > Evaluate[SetPrecision[1, 3]]], >>>>> > HoldForm[True]}, HoldForm[2.`2.9999999999999996], >>>>> > {{HoldForm[1.`2.9999999999999996], HoldForm >>>>> [1.`2.9999999999999996]}, >>>>> > HoldForm[1.`2.9999999999999996], >>>>> > HoldForm[1.`2.9999999999999996]}, HoldForm >>>>> [2.`2.9999999999999996], >>>>> > HoldForm[2.`2.9999999999999996]} >>>>> > >>>>> > Notice the leftover Evaluate and SetPrecision commands. Does >>>>> anyone >>>>> > have ideas on how to get this to work? >>>>> > >>>>> > On 7/19/06, Bob Hanlon <hanlonr at cox.net> wrote: >>>>> >> Hold[{2, 3}] /. >>>>> >> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >>>>> >> >>>>> >> Hold[8] >>>>> >> >>>>> >> >>>>> >> Bob Hanlon >>>>> >> >>>>> >> ---- Chris Chiasson <chris at chiasson.name> wrote: >>>>> >> > Hold[{2, 3}] /. {{x_, y_} :> x^y} >>>>> >> > >>>>> >> > the result is Hold[Power[2,3]] >>>>> >> > >>>>> >> > I would like the result to be Hold[8] >>>>> >> > >>>>> >> > The original context is post-processing of a large Trace >>>>> output >>>>> >> (via >>>>> >> > SetPrecision to get rid of digits and improve readability). >>>>> >> > >>>>> >> > Any ideas? >>>>> >> > >>>>> >> > -- >>>>> >> > http://chris.chiasson.name/ >>>>> >> > >>>>> >> >>>>> >> -- >>>>> >> >>>>> >> Bob Hanlon >>>>> >> hanlonr at cox.net >>>>> >> >>>>> >> >>>>> >> >>>>> > >>>>> > >>>>> > -- >>>>> > http://chris.chiasson.name/ >>>>> >>>>> >>>> >>>> >>>> -- >>>> http://chris.chiasson.name/ >>> >> >