Boost logo

Boost Users :

From: John Maddock (jz.maddock_at_[hidden])
Date: 2020-08-16 11:50:49


On 14/08/2020 22:56, Anirban Pal via Boost-users wrote:
> Hello,
>
> I'm trying to integrate functions with BOOST quadrature routines. So
> far they have been extremely impressive accuracy-wise, particularly
> with multiprecision features.
>
> I wish to integrate a function and pass some arguments to it. These
> arguments can be scalars, matrices, structs or objects. I was
> wondering if there is an example that explores this. Would I need to
> use a boost.function? Or would a function pointer work?

To create a reusable single-valued functor that integrates from [a, x]
for fixed a, then I think some nested lambda expressions would work:

 Â Â  auto integral = [](double x)
 Â Â  {  // Integrates from 0 to x:
 Â Â Â Â Â  static tanh_sinh<double> integrator;
 Â Â Â Â Â  static auto f = [](double x) { return 5 * x + 7; };
 Â Â Â Â Â  return integrator.integrate(f, 0.0, x);
 Â Â  };
 Â Â  std::cout << integral(2) << std::endl;
 Â Â  std::cout << integral(4.5) << std::endl;

I'm not sure what you had in mind with non-scalar arguments, but if you
need to evaluate the above at multiple x values [x_1, x_2.... x_n] then
conceivably you could integrate from

[a, x_0]

[x_1, x_2]

....

[x_n-1, x_n]

and then sum to get the integrals

[a, x_0]

[a, x_1]

....

[a, x_n]

Which may or may not be more efficient.

HTH, John.

>
> I currently have an implementation using GSL_functions which
> explicitly allow the function and parameters to be passed as pointers.
> I am curious if I can do something similar with boost.
>
> Thank you.
>
>
> --
> Anirban Pal
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users

-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net