Re: Want to use Module
- To: mathgroup at smc.vnet.net
- Subject: [mg9826] Re: [mg9773] Want to use Module
- From: Wouter Meeussen <vdmcc at vandemoortele.be>
- Date: Fri, 28 Nov 1997 05:35:39 -0500
- Sender: owner-wri-mathgroup at wolfram.com
hi Ted, are you in for a "pas-de deux" ? In[1]:=y[x_]:=x Exp[x]+2 In[2000987]:=foo[x_/;((temp=N at y[x])<100)]:= Module[{it =temp},Clear[temp]; 2/(3+it)] but maybe I "bent the rules" by using Clear[temp]? It took me some time to find out I had to convert y[x] into a number (N[ ..]) if I wanted the condition "/;( stuff < 100)" to evaluate to either False or True. Maybe check with Xah's notebooks to see wether foo[x_/;(condition)]:= ... works faster than foo[x_]/;(condition):= ... A slight variation: In[2000993]:=foo[x_/;(N@(temp=y[x])<100)]:= Module[{it =temp},Clear[temp]; 2/(3+it)] now, temp is left symbolic, only the condition is forced to numeric. This allows you to evaluate stuff like : NestList[foo,1,7] as being different from NestList[foo,1.,7] have fun, wouter. At 12:07 25.11.97 -0500, Ersek_Ted%PAX1A at mr.nawcad.navy.mil wrote: > >I need to write a function like foo[x] below. > >In[1]:= >y[x_]:=x Exp[x]+2; > >In[2]:= >foo[x_]/;(temp=y[x]; temp<100):= 2/(3+temp) > >In[3]:= >foo[2] > >Out[3]= >2/(5 + 2*E^2) > >In[4]:= >foo[20] > >Out[4]= >foo[20] > >In[5]:= >Clear[s]; >foo[s] > >Out[5]= >foo[s] > >Notice foo[x] is only defined when (y[x]<100). (y[20]<100)->False, so >foo[20] is returned unevaluated. (y[s]<100) is not defined, so foo[s] >is returned unevaluated. > >I used (temp=y[x]) so I wouldn't have to evaluate y[x] twice. How can I >do this with (temp) inside a Module, so it is local to (foo[x])? > > Thanks > > Ted Ersek > ersek_ted%pax1a at mr.nawcad.navy.mil > > > NV Vandemoortele Coordination Center Oils & Fats Applied Research Prins Albertlaan 79 Postbus 40 B-8870 Izegem (Belgium) Tel: +/32/51/33 21 11 Fax: +/32/51/33 21 75 vdmcc at vandemoortele.be