Skip to content

Instantly share code, notes, and snippets.

@fangzhou-xie
Created September 22, 2021 17:28
Show Gist options
  • Save fangzhou-xie/8a8a6754a34b19851fb59f7cbd3bc9aa to your computer and use it in GitHub Desktop.
Save fangzhou-xie/8a8a6754a34b19851fb59f7cbd3bc9aa to your computer and use it in GitHub Desktop.
gradient from Eigen
// test the partial derivative in one go
#include "adcpp.h"
#include <iostream>
using namespace adcpp;
static bwd::Double mysq(const bwd::Double &x, const bwd::Double &y) {
return bwd::pow(x, 2) + bwd::pow(y, 3);
}
int main(const int argc, const char **argv) {
// bwd::Double x = bwd::Double(3.0);
// bwd::Double y = bwd::Double(2.0);
// bwd::Double f = mysq(x, y);
// bwd::Double::DerivativeMap derivative;
// f.derivative(derivative);
// std::cout << "result: " << f << std::endl
// << "x = " << x << std::endl
// << "y = " << y << std::endl
// << "fx = " << derivative(x) << std::endl
// << "fy = " << derivative(y) << std::endl;
bwd::Matrix2d x;
x << bwd::Double(1.0), bwd::Double(1.9), bwd::Double(1.5), bwd::Double(3.0);
bwd::Vector2d c;
c << bwd::Double(2.9), bwd::Double(4.8);
bwd::Double y = (x * c).array().sum();
Eigen::Vector2d fc;
bwd::gradient(c, y, fc);
std::cout << fc << std::endl;
Eigen::Matrix2d fx;
bwd::gradient(x, y, fx);
std::cout << fx << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment