MathGroup Archive 1998

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

Search the Archive

RE: An operator called-- LocalSet

The last time I sent this the symbol \[Infinity] in the definition of
DotEqual was incorrectly translated to ASC.

My first attempt had an error at In[4].

Both errors are corrected below.

Ted Ersek

Recall my recent message describing a function I made called LocalRule. 
I indicated that I wrote a similar function called LocalSet.  In this
message I present this function.

Suppose you want to define a function such as in the next two lines. 
Using (lhs=rhs) is efficient because the integral is only done when the
definition is made.  However, if (x) has a numerical global value you
get an error message because you end up integrating with respect to a
constant.  You don't have to worry about global values of (x) if you
use (lhs:=rhs), but then something like ( a=2; Plot[f[x],{x,2,8}] )
will take a very, very long time because the integral is worked out for
every sample!

(*  Efficient, but risk trouble from global values for (x).  *) f[x_]
=Integrate[Log[a Sqrt[x]+x^2],x];

(*  Inefficient, but global values for (x) are irrelevant.   *)  f[x_]
:=Integrate[Log[a Sqrt[x]+x^2],x];

I wrote a function LocalSet  (infix operator \[DotEqual] )  to give the
best of both worlds.
The program is given below.

  b1=Cases[Unevaluated at lhs,_Pattern,{0,Infinity},Heads->True];
  If[b1==={},(Unevaluated at lhs:=Evaluate at rhs),(
     b1=Extract[#,1,Hold]&  /@b1;
     b2=Hold[#:=Evaluate at rhs]& @@{Unevaluated at lhs};
)]    (*  extra parentheses for clarity  *)



At In[2] and In[3] the right hand side evaluates without using the
global value for (x).

f1[x_] \[DotEqual] Integrate[Log[a Sqrt[x]+x^2],x]; ?f1
f1[x_] := -2*x + Sqrt[3]*a^(2/3)*ArcTan[(-a^(1/3) +
 2*Sqrt[x])/(Sqrt[3]*a^(1/3))] - a^(2/3)*Log[a^(1/3) +
 Sqrt[x]] + (a^(2/3)*Log[a^(2/3) -
 a^(1/3)*Sqrt[x] + x])/2 + x*Log[a*Sqrt[x] + x^2]

LocalSet[f2[x_],Integrate[Log[Sqrt[x]],x]]; ?f2
f2[x_] := -x/2 + x*Log[Sqrt[x]]

At In[4]  (x) isn't used as a pattern, so the global value (x=3) is
used. _____________________

g \[DotEqual] x^2+a;
g* Sqrt[5]//InputForm
Sqrt[5]*(9 + a)

I had another version of this program earlier that didn't use
Heads->True inside Cases, and had an extra Hold at the end.  Alan Hayes
recommended such changes to the LocalRule program I recently sent in. 
I implemented those recommendations in this program.

Please let me know if you see any problems with this program or any ways
to improve it.

Ted Ersek

  • Prev by Date: Re: Summing over like elements
  • Next by Date: Postscript Error
  • Previous by thread: Re: Summing over like elements
  • Next by thread: Postscript Error