An operator called-- LocalSet

• To: mathgroup at smc.vnet.net
• Subject: [mg13835] An operator called-- LocalSet
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Sat, 29 Aug 1998 04:41:13 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Recall my recent message describing a function I made called LocalRule.
I indicated 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.
_____________________

LocalSet=DotEqual;
SetAttributes[DotEqual,HoldAll];

DotEqual[lhs_,rhs_]:=Module[{b1,b2},(
If[b1==={},(Unevaluated at lhs:=Evaluate at rhs),(
b1=Extract[#,1,Hold]&  /@b1;
b2=Hold[#:=Evaluate at rhs]& @@{Unevaluated at lhs};
Block@@Join[b1,b2]
)]
)]    (*  extra parentheses for clarity  *)

_____________________

In[1]:=
Clear[f1,f2,a,g];
x=3;

_____________________

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

In[2]:=
f1[x_] \[DotEqual] Integrate[Log[a Sqrt[x]+x^2],x]; ?f1

"Global`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]

____________________

In[3]:=
LocalSet[f2[x_],Integrate[Log[Sqrt[x]],x]]; ?f2

"Global`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. _____________________

In[4]:=
gUx^2+a;
g* Sqrt[5]//InputForm

Out[4]//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: Improving Plot evaluation
• Next by Date: RE: An operator called-- LocalRule
• Previous by thread: Improving Plot evaluation
• Next by thread: integral of a real function is real ?