#include #include #include #include template T fun_sin(Eigen::Matrix const &x) { T y; y = sin(x(0) + x(1)); //y = x(0)*x(0)*x(1)*x(1); return y; } int main() { std::cout << "test_fun_sin_ADDS" << std::endl; typedef Eigen::Matrix inner_derivative_t; typedef Eigen::AutoDiffScalar inner_scalar_t; typedef Eigen::Matrix derivative_t; typedef Eigen::AutoDiffScalar scalar_t; typedef Eigen::Matrix input_t; input_t x(2); x(0).value()=1; x(1).value()=1; //set unit vectors for the derivative directions (partial derivatives of the input vector) x(0).derivatives().resize(2); x(0).derivatives().setZero(); x(0).derivatives()(0)= 1; x(1).derivatives().resize(2); x(1).derivatives().setZero(); x(1).derivatives()(1)=1; //repeat partial derivatives for the inner AutoDiffScalar x(0).value().derivatives() = inner_derivative_t::Unit(2,0); x(1).value().derivatives() = inner_derivative_t::Unit(2,1); //set the hessian matrix to zero for(int idx=0; idx<2; idx++) { x(0).derivatives()(idx).derivatives() = inner_derivative_t::Zero(2); x(1).derivatives()(idx).derivatives() = inner_derivative_t::Zero(2); } scalar_t y = fun_sin(x); std::cout << std::endl; std::cout << "y.value().value() = " << y.value().value() << std::endl; std::cout << "gradient" << std::endl; std::cout << "y.value().derivatives() = " << y.value().derivatives().transpose() << std::endl; std::cout << "y.derivatives()(0).value() = " << y.derivatives()(0).value() << std::endl; std::cout << "y.derivatives()(1).value() = " << y.derivatives()(1).value() << std::endl; std::cout << "Hessian" << std::endl; std::cout << "y.derivatives()(0).derivatives() = " << y.derivatives()(0).derivatives().transpose() << std::endl; std::cout << "y.derivatives()(1).derivatives() = " << y.derivatives()(1).derivatives().transpose() << std::endl; return(0); }