MathGroup Archive 2003

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

Search the Archive

Re: RuleCondition vs Condition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43037] Re: RuleCondition vs Condition
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Sat, 9 Aug 2003 02:57:37 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

A recent message from Andrzej Kozlowski on this subject inspired me to take
a closer look at RuleCondition.  Allan Hayes once provided a detailed
discussion of RuleCondition, but I can't find it. In any case I would like
to provide a good discussion of RuleCondition at 
http://www.verbeia.com/mathematica/tips/Tricks.html

In Allan Hayes's discussion I refer to above, he used Trace to show us that
RuleCondition is used behind the scenes in a trick Trott and Stzebonski
discovered to do "in-place evaluation".  An example of this is in the
following where we evaluate only subexpressions with the head Plus.

In[1]:=  expr=Hold[ 2+3*2+1, Sqrt[9], Sqrt[10+6] ];
            expr /. p_Plus :> With[ {eval=p}, eval/;True ]

Out[2]= Hold[ 9, Sqrt[9], Sqrt[16] ]

---------------
However, I had a hard time seeing where it might be useful to use
RuleCondition in my programming.  The following is along the lines of the
recent message from Andrzej Kozlowski and helps illustrate the difference
between RuleCondition and Condition. 

In[3]:=  Clear[x];
            {Condition[x, 3<5], Condition[x, 5<3], Condition[x, x<1]}

Out[4]= { x/; 3<5, x/; 5<3, x/;x<1 }


In[5]:= {RuleCondition[x, 3<5], RuleCondition[x, 5<3], RuleCondition[x,
x<1]}

Out[5]= { x, Fail, Fail }


The following shows how I could use RuleCondition in programming.  However,
my timing tests show that (f2) which uses RuleCondition is slower than (f1).
That leaves me wondering when I might have a use for RuleCondition.  Can
anyone give an example where it helps to use RuleCondition?

In[6]:=  Clear[f1,f2];
            f1[x_,y_]/; x<y := x+y;
            {f1[3,5], f1[6,3]}

Out[8]= { 8, f1[6,3] }


In[9]:=  f2[x_,y_]:= RuleCondition[ x+y, x<y ];
            { f2[3,5], f2[6,3] }

Out[10]= { 8, f2[6,3] }

-----------------
Thanks,
   Ted Ersek


  • Prev by Date: RE: Re: contour plotting over a non-uniform grid
  • Next by Date: Re: Re: goldbach prime partitions for arbitrary integer n => 4
  • Previous by thread: RuleCondition vs Condition
  • Next by thread: contour plotting over a non-uniform grid