Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

RE: An operator called-- LocalSet

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13858] RE: [mg13835] An operator called-- LocalSet
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Wed, 2 Sep 1998 01:30:55 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

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.
_____________________

LocalSet=DotEqual;
SetAttributes[DotEqual,HoldAll];
 
DotEqual[lhs_,rhs_]:=Module[{b1,b2},(
  b1=Cases[Unevaluated at lhs,_Pattern,{0,Infinity},Heads->True];
  If[b1==={},(Unevaluated at lhs:=Evaluate at rhs),(
     b1=Extract[#,1,Hold]&  /@b1;
     b1=Thread[b1,Hold];
     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]:=
g \[DotEqual] x^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: Re: Summing over like elements
  • Next by Date: Postscript Error
  • Previous by thread: Re: Summing over like elements
  • Next by thread: Postscript Error