Re: pure function to generate a list of integrals
- To: mathgroup at smc.vnet.net
- Subject: [mg77331] Re: pure function to generate a list of integrals
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 7 Jun 2007 03:37:55 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f43gpj$2k7$1@smc.vnet.net>
Ruth wrote: > Thanks to everyone. Bill's was the purest solution of them all (if I > understand correctly the meaning of purity). However, my example was to > naive (linear). > > I would like to have a new function with basically the same purity as > Bill's solution > > f = #1 Block[{x}, NIntegrate[x, {x, 0, #}] & /@ #2] &; > > but this time to be able to produce the equivalent of > > {NIntegrate[Sqrt[2x+3], {x, 0, 0.1}], NIntegrate[Sqrt[2x+3], {x, 0, 0.5}], > NIntegrate[Sqrt[2x+3], {x, 0, 0.9}]} > > when evaluating f[2,3,{0.1,0.5,0.9}] > > Thanks again. In[1]:= f = Block[{x, a = #1, b = #2}, (NIntegrate[Sqrt[a*x + b], {x, 0, #1}] & ) /@ #3] & ; f[2, 3, {0.1, 0.5, 0.9}] Out[2]= {0.17606053323094345, 0.9346158590977903, 1.7733735604641878} Regards, Jean-Marc > Bill Rowe escribió: >> On 6/2/07 at 4:17 AM, ruth.lazkoz at ehu.es (Ruth Lazkoz Saez) wrote: >> >> >>> I am trying to brush up a long code I have to make it more compliant >>> with the spirit of functional programming. I do not like to hear >>> that the kind of calculations I do should run faster in C, because I >>> suspect that if I managed to write good code in Mathematica it >>> should be as fast. So I have to go and improve my code chunk by >>> chunk. >>> >> >>> My first problem is that I want to generate a pure function say f, >>> which, so that f[2, {0.1, 0.5, 0.9}] gives me the same output as >>> >> >>> {NIntegrate[2x, {x, 0, 0.1}], NIntegrate[2x, {x, 0, 0.5}], >>> NIntegrate[2x, {x, 0, 0.9}]} >>> >> This will do the trick >> >> f = #1 Block[{x}, NIntegrate[x, {x, 0, #}] & /@ #2] &; >> >> Checking: >> >> In[14]:= f[2, {0.1, 0.5, 0.9}] >> >> Out[14]= {0.01,0.25,0.81} >> >> In[15]:= f[3, {0.1, 0.5, 0.9}] >> >> Out[15]= {0.015,0.375,1.215} >> -- >> To reply via email subtract one hundred and four >> >> >> > > > >