0

I would like to implement a numerical integral whose integrand is evaluated at quadrature points. Therefore something like: integral(domain, f), where domain is indeed the domain where I want to integrate and f is the function to integrate. f is only a function of the Point p (quadrature points) inside the domain and can have vector values (scalar is a particular case).

Since the function f can be, in general, a combination of different functions, I wonder how to overload arithmetic operators for functions.

I already found this Implementing multiplication operator for mathematical functions C++ but it does not cover my question, because the Function returns only x, while In my case I would like to have different Functions which can return a more complex function of x.

So, let f_1,...f_N be different functions which have the same return type, for example a std::array<double,M> with given length M, and which receive the same input Point p, i.e for I=1,...,N:

std::array<double,M> f_i(Point p)
      { std::array<double,M> x; 
        \\ compute x somehow depending on i
        return x;}

Then I would like to create f as a combination of the previous f_1,...f_N, e.g. f=f_1 *f_2+(f_3*f_4)*f_5... (here the operations are meant to be component wise).

In this way I could evaluate f(p) inside integral(domain, f), obtaining for each quadrature point exactly:

f_1(p) *f_2(p)+(f_3(p)*f_4(p))*f_5(p)...

Edit: I know I have to use functors and not simple functions (which I used just to state the problem), but I am not able to figure out how for this purpose. Any hint?

Thank you

Garo
  • 111
  • 7
  • You cannot overload operators without a user defined data type being involved. `f_1 + f_2` for example can't be simple `std::array (*)(Point)`s. But you can make a `template struct Math_function;` wrapper holding a callable which can then be overloaded and you can get `Math_function{f_1} + f_2;` to compile. – nwp May 07 '19 at 14:11
  • Yes, I used the name function, but I should have use functor maybe. – Garo May 07 '19 at 14:26
  • I'm a bit at a loss what you are asking. "Any hint?" isn't a real question and you seem to already know how to do it. – nwp May 07 '19 at 14:28
  • I know I have to use functors, but I am not able to figure out how for this purpose... – Garo May 07 '19 at 14:33
  • Make a wrapper for the functors and overload operators for the wrapper. – nwp May 07 '19 at 14:35

0 Answers0