3 ms·
Using dual numbers and modern C++ you can write a library that can do this: auto lambda = [](auto x, auto y) { // The Rosenbroc
by petters 10y ago
Using dual numbers and modern C++ you can write a library that can do this:
auto lambda =
[](auto x, auto y)
{
// The Rosenbrock function.
auto d0 = y[0] - x[0]*x[0];
auto d1 = 1 - x[0];
return 100 * d0*d0 + d1*d1;
};
// The lambda function will be copied and
// automatically differentiated. The derivatives
// are computed using templates, not numerically.
//
// No need to derive or compute derivatives
// manually!
auto func = make_differentiable<1, 1>(lambda);
"func" now has code for first-order, second-order derivatives all generated and heavily optimized at compile-time.
This is one reason C++ is so good for (mathematical) optimization.