Re: Re: Re: pure function to generate a list of
- To: mathgroup at smc.vnet.net
- Subject: [mg77293] Re: [mg77192] Re: [mg77090] Re: pure function to generate a list of
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Wed, 6 Jun 2007 07:18:34 -0400 (EDT)
- Reply-to: hanlonr at cox.net
Clear[f] f[a_, b_, upperLimits_List] := NIntegrate[Sqrt[a*x + b], {x, 0, #}] & /@ upperLimits; f[2, 3, {0.1, 0.5, 0.9}] == {NIntegrate[Sqrt[2 x + 3], {x, 0, 0.1}], NIntegrate[Sqrt[2 x + 3], {x, 0, 0.5}], NIntegrate[Sqrt[2 x + 3], {x, 0, 0.9}]} True Bob Hanlon ---- Ruth <ruth.lazkoz at ehu.es> 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. > > > Bill Rowe escribi=C3=B3: > > 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 > > > > > > > > > >