       Re: pure function to generate a list of integrals

• To: mathgroup at smc.vnet.net
• Subject: [mg77089] Re: pure function to generate a list of integrals
• From: dimitris <dimmechan at yahoo.com>
• Date: Sun, 3 Jun 2007 06:10:06 -0400 (EDT)
• References: <f3r9er\$19e\$1@smc.vnet.net>

```Hi.

\$VersionNumber
5.2

You could try

In:=
f[g_, {x_, a_, b_}, opts___(*add options of NIntegrate ] := NInt[g,
{x, a, b}, opts] (*the function you are interested in*)

In:=
(f[2*x, {x, 0, #1}] & ) /@ Range[0.1, 0.9, 0.4] (*an application*)
Out=
{NInt[2*x, {x, 0, 0.1}], NInt[2*x, {x, 0, 0.5}], NInt[2*x, {x, 0,
0.9}]}

In:=
% /. NInt -> NIntegrate (*change to NIntegrate in order to get
numerical results*)
Out=
{0.01,0.25,0.81}

In:=
(f[3*x, {x, 0, #1}] & ) /@ Range[0.1, 0.9, 0.4]
Out=
{NInt[3 x,{x,0,0.1}],NInt[3 x,{x,0,0.5}],NInt[3 x,{x,0,0.9}]}

In:=
% /. NInt -> NIntegrate
Out=
{0.015,0.375,1.215}

As another more advanced example consider

In:=
(f[Sin[x], {x, 0, #1}, WorkingPrecision -> 30, MaxRecursion -> 12,
PrecisionGoal -> 20] & ) /@ {10, 100, 1000}
% /. NInt -> NIntegrate

Out=
{NInt[Sin[x], {x, 0, 10}, WorkingPrecision -> 30, MaxRecursion -> 12,
PrecisionGoal -> 20],
NInt[Sin[x], {x, 0, 100}, WorkingPrecision -> 30, MaxRecursion ->
12, PrecisionGoal -> 20],
NInt[Sin[x], {x, 0, 1000}, WorkingPrecision -> 30, MaxRecursion ->
12, PrecisionGoal -> 20]}
Out=
{1.839071529076452452259,0.137681127712316065898,0.43762092370929700892}

I hope you will find useful this workaround.
Others may give you better solutions.

But an important fact must be pointed out. "The problem is that
NIntegrate is not
listable..." you mention and at first glance you are right.

In:=
Attributes[NIntegrate]
Out=
{HoldAll, Protected}

However NIntegrate behaves like "listable".

In:=
g = HoldForm[NIntegrate[{x, 2*x, 3*x}, {x, 1, 3}]]
Out=
NIntegrate[{x,2 x,3 x},{x,1,3}]

In:=
ReleaseHold[g]
Out=
{4.,8.,12.}

It is the same situation with Plot for example

In:=
Attributes[Plot]
Out=
{HoldAll, Protected}

But

In:=
ReleaseHold[g /. NIntegrate -> Plot]
(*plot to be displayed*)

or Integrate

In:=
Attributes[Integrate]
Out=

but

In:=
ReleaseHold[g /. NIntegrate -> Integrate]
Out=
{4, 8, 12}

There are now two attitudes you could face above situation for
NIntegrate,Plot,
Integrate. The first is the quick one: Ignore any incosistency and be
happy
about the fact these commands behave like these.
The second is (again be happy(!) and) try to figure out what is going
on.
At first glance someone would say that the fact that Plot and
NIntegrate behave like
"listable" is astonising. However the situation is not like this.
Functions that are Listable are those that have a single argument. But
actually the functions can have more than one argument, and in this
case equal length lists must be given for every argument. An example
of this is:

In:=
Times[{2,3,4},{x,o,e}]
Out=
{2 x,3 o,4 e}

Commands like NIntegrate and Plot DO not allow you to have a list for
the
second argument, the iterator. So the commands are NOT in fact
Listable.

Functions like Integrate and Plot are overloaded commands in the
sense
that they have one definition for the case of a non-List expression in
the
first slot and a different definition when the first slot contains a
list. Indeed

In:=
Information["Plot", LongForm -> False]

"Plot[f, {x, xmin, xmax}] generates a plot of f as a function of x
from xmin to xmax. Plot[{f1, f2, ... }, {x, xmin, xmax}] plots several
functions fi."*Button[More..., ButtonData :> "Plot", Active -> True,

The two different definitions are apparent. I think something similar
should have been
added to the documentation of Integrate and NIntegrate (at least for
version 5.2 this is not
the case).

Adopted this explanation one will continue his experiments with
Mathematica
being happy (always!) that he has understood something that look like
an
astonising incosistency.

Since you are working with NIntegrate there are possibilities to deal
with Principal Values Integrals
that cannot be done analytically. In this case you should load the
proper package

In:=
Needs["NumericalMath`CauchyPrincipalValue`"]

Say now that you have the list of the following functions with poles
at the origin

In:=
o = {1/x, 1/Sin[x], Tan[x]};

In view of previous explanation and considering that based on Help
Brower the command CauchyPrincipalValue uses NIntegrate you could
try

In:=
CauchyPrincipalValue[o, {x, -1, {0}, 2}, AccuracyGoal -> 10]

Surprisingly it doesn't work! Incosistency? Maybe yes, may be no! The
truth is that this function has one definition; just for one argument
in the first slot!

In:=
CauchyPrincipalValue[Evaluate@o, {x, -1, {0}, 2}, AccuracyGoal -> 10]

Not working either.

In this case there is indeed no kind of "listability". Try instead

In:=
(CauchyPrincipalValue[#1, {x, -1, {0}, 1/2}, AccuracyGoal -> 10] & ) /
@ o
Out=
{-0.6931471805602387, -0.7605693185090221, -0.4850422299423481}

Best Regards
Dimitris

/  Ruth Lazkoz Saez       :
> 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.
>