Re: Re: Want to use Module
- To: mathgroup at smc.vnet.net
- Subject: [mg9863] Re: Re: Want to use Module
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 29 Nov 1997 00:10:53 -0500
- References: <347EC033.60F2@haystack.demon.co.uk>
- Sender: owner-wri-mathgroup at wolfram.com
Re: Want to use Module Wouter Meeussen <vdmcc at vandemoortele.be> Wouter, Your function foo below does not clear the variable temp when foo is returned unevaluated, because Clear itself is not then evaluated. y[x_]:=x Exp[x]+2 foo[x_/;((temp=N at y[x])<100)]:= Module[{it =temp},Clear[temp]; 2/(3+it)] {foo[2],foo[20], temp, Do[{foo[2],foo[20]},{100}]//Timing//First}//OutputForm 9 {0.101122, foo[20], 9.7033 10 , 0.316667 Second} Here is a way round: the condition is inside Module (see Help Browser) I have also rearranged N so that the value of foo is exact. foo2[x_]:= Module[{temp2}, 2/(3+temp2)/;(N[temp2=y[x]]<100)] {foo2[2],foo2[20], temp2, Do[{foo2[2],foo2[20]},{100}]//Timing//First}//OutputForm 2 {--------, foo2[20], temp2, 0.233333 Second} 2 5 + 2 E Allan -- Allan Hayes Mathematica Training and Consulting Leicester, UK hay at haystack.demon.co.uk http://www.haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44 (0)116 271 4198 Wouter Meeussen wrote --------------------------------------------------------------- > > 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