vendredi 5 janvier 2018

C++ composite function call and evaluation

I am trying to call function from another function. I am creating 2 vectors in main() abd passing them to fun2(). fun2 calls fun1 with parameters as (4.0 -2.0*p, 5.0+3.0*p). fun1 should return 5*(4.0 -2.0*p)^2 + 4*(5.0+3.0*p)^2 + 14. This will result in function with p as parameter which will be evaluated by line //Question : value 3 should be put there. But the syntax or method is not quite right. How can I evaluate composite functions like this?

#include<iostream>
#include<vector>

double fun1(double x1, double x2)
{
    double res1 = std::pow(x1,2.0)*5.0 + std::pow(x2,2)*4.0 + 14.0; 
    return res1;
}

double fun2(const std::vector<double> *v1, const std::vector<double> *v2, double(*f1)(double, double))
{
    double p, res2;
    res2 = fun1(((*v1)[0] + p*(*v2)[0]), ((*v1)[1] + p*(*v2)[1]) );
    return res2;
}

int main()
{
    double res3;
    std::vector<double> v1 = { 4.0,5.0 };
    std::vector<double> v2 = { -2.0,3.0 };
    res3 = fun2(&v1, &v2, &fun1)(3); //Question

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire