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

MathGroup Archive 2007

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

Search the Archive

Re: pure function to generate a list of integrals

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77169] Re: [mg77062] pure function to generate a list of integrals
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Tue, 5 Jun 2007 06:39:27 -0400 (EDT)
  • References: <26394746.1180984140440.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

Clear[f, g]
Block[{k, x, z},
  g[k_] = Function[{z}, Evaluate@Integrate[k x, {x, 0, z}]];
  f = Function[{k, z}, g[k] /@ z];
  ]
f[3, {0.1, 0.5, 0.9}]

{0.015, 0.375, 1.215}

(That version Integrates in advance and may be faster than others.)

or

f = Function[{k, upper}, NIntegrate[k x, {x, 0, #}] & /@ upper];
f[3, {0.1, 0.5, 0.9}]

{0.015, 0.375, 1.215}

or

Clear[f]
f[k_, limits_List] :=
  Block[{x}, NIntegrate[k x, {x, 0, #}] & /@ limits]
f[3, {0.1, 0.5, 0.9}]

{0.015, 0.375, 1.215}

or

Clear[f]
f[k_, limits_List] := NIntegrate[k x, {x, 0, #}] & /@ limits
f[3, {0.1, 0.5, 0.9}]

{0.015, 0.375, 1.215}

Bobby

On Sat, 02 Jun 2007 03:17:59 -0500, <""Ruth Lazkoz  
Saez"<ruth.lazkoz"@ehu.es>> wrote:

> Hi everyone,
>
> 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}]}
>
> That is, I want to generate a list of numerical integrals of the same
> function but making one of the integration limits change by taking
> values from a list.
>
> I also want my function to admit two arguments (a number and a list)
> because I want to be able to use the same definition to generate the
> same output as for instance
>
>
> {NIntegrate[3x, {x, 0, 0.1}], NIntegrate[3x, {x, 0, 0.5}],
>    NIntegrate[3x, {x, 0, 0.9}]}
>
> by evaluating f[3, {0.1, 0.5, 0.9}] this time.
>
> I tried for quite a while, but I failed. I suspect one of the problems
> is NIntegrate is not listable. I could make some progress with  Map but
> I only what halfway and on top I was not satisfied with the syntax I
> would have to use.
>
> Thanks in advance,
>
> Ruth Lazkoz
>
>
>
>



-- 

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Latex and \sum
  • Next by Date: Re: RE: simple question
  • Previous by thread: Re: pure function to generate a list of integrals
  • Next by thread: Re: pure function to generate a list of integrals