mardi 7 mai 2019

How to overload arithmetic operators (+, *, -, /) for functions in c++?

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)...

Any hint?

Thank you

Aucun commentaire:

Enregistrer un commentaire