lundi 22 janvier 2018

nested functional approaches to cpp

I am trying to come up with a way to build something that looks like this: $\sum_{i,k,l} F(F(F(g(x)),i),k),l)$ where F is the function below:

    double F(const std::function<double(std::vector<double>,std::vector<double>)> f, int order, std::vector<double> vars, std::vector<double> pars, const int par_0_index) {
      switch (order) {
        case 0: return f_zero(f, vars, pars, par_0_index);-
                break;
        case 1: return f_lin(f, vars, pars, par_0_index);-
                break;
        case 2: return f_quad(f, vars, pars, par_0_index);-
                 break;
        default:   break;
      }
    return nan("");
    }

this results is 27 terms and I don't wont to write them all out... If I now loop :

    for (int i =0; i<2 ;i++){
      sum += Derivative::F(f, i, vars, pars, 3);
    }
    return sum;

this fails with:

test_taylor(17451,0x7fffb3509340) malloc: *** error for object 0x7fdda7e2d548: incorrect checksum for freed object - object was probably modified after being freed.

But this works:

return Derivative::F(f, 0, vars, pars, 3) + Derivative::F(f, 1, vars, pars, 3);

So my first question would be why does this fail when I loop over it and not when I just add two terms? And can this conceptually be done better? Thank you.

Aucun commentaire:

Enregistrer un commentaire