For Education Purposes
I have a function which takes the derivative of a given input function.
//In analysis class
double analysis::differentiateBest(std::function<double(double)> fn, double x)
{
return derivative_1_Richardson_6O(fn, x);
}
The way I call this function is by using a lambda in place of the std::function
analysis al;
std::cout << al.differentiateBest([](double x) { return x*x*x*x*x*x; }, 2)
I want to extend the derivative to Higher Dimension. ie.Take a gardient.
// matrix<T> my class for holding matrices , implemented as 1D std::vector
matrix<double> analysis::gradient_2D(std::function<double(double, double)> fn,double x, double y)
{
matrix<double> R(2, 1);
//DOUBT
std::function<double(double xVar)> fnX = fn( xVar, y); //ERROR
// creates a new fn which holds y constant and allows x to vary
std::function<double(double yVar)> fnY = fn( x, yVar); //ERROR
// creates a new fn which holds x constant and allows y to vary
R(1, 1) = differentiateBest(fnX,x); // Takes derivative in X direction , holding y constant
R(1, 2) = differentiateBest(fnY,y); // Takes derivative in Y direction , holding x constant
return R;
}
So the way I will call gradient is
analysis al;
matrix<double> R = al.gradient_2D([](double x,double y) { return x*x*x*y + y; }, 2 ,3) ;
// Takes the gradient of lambda function at the point 2 , 3
This is what I want to do. But I get error in VS at this line
std::function<double(double xVar)> fnX = fn( xVar, y);
std::function<double(double yVar)> fnY = fn( x, yVar);
I want to make the 2D function into a 1D function , by fixing the x or y value. The x or y values are fixed at the point at which we want to take the gradient.
So my question is how can I convert a std::function taking 2 variables into a function taking a single variable .
PS- I know it will be easier to use a library , but I would like to learn to do this myself , hence educational at top . -Thank you
Aucun commentaire:
Enregistrer un commentaire