In some application I need to have access to the elements of some polynomial expansion (for example x^i for i = 0,1,...). My idea was to create a "function factory" like
#include <functional>
#include <iostream>
std::function<double(double)> Taylor(unsigned int i)
{
return [i](double y) {return std::pow(y, i);};
}
int main()
{
unsigned int n = 3; // the wanted degree in your polynomial basis
auto f = Taylor(n);
double a = f(3);
std::cout << "a = " << a << std::endl;
return 0;
}
In the example above, we would get the output a = 27.
This would be useful in the context of function estimation (i.e., roughly, find the coefficients of some polynomial expansion given data).
Is there some better way to do this? Also, if the polynomial basis is not just x^i but something more complicated, using a lambda function could be inappropriate...