29 template <
int IN_DIM,
int OUT_DIM>
30 class DerivativesCppad :
public Derivatives<IN_DIM, OUT_DIM, double>
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35 typedef ADScalar AD_SCALAR;
37 typedef Eigen::Matrix<AD_SCALAR, IN_DIM, 1> IN_TYPE_AD;
38 typedef Eigen::Matrix<AD_SCALAR, OUT_DIM, 1> OUT_TYPE_AD;
40 typedef Eigen::Matrix<double, IN_DIM, 1> IN_TYPE_D;
41 typedef Eigen::Matrix<double, OUT_DIM, 1> OUT_TYPE_D;
42 typedef Eigen::Matrix<double, OUT_DIM, IN_DIM> JAC_TYPE_D;
43 typedef Eigen::Matrix<double, OUT_DIM, IN_DIM, Eigen::RowMajor>
45 typedef Eigen::Matrix<double, IN_DIM, IN_DIM> HES_TYPE_D;
46 typedef Eigen::Matrix<double, IN_DIM, IN_DIM, Eigen::RowMajor> HES_TYPE_ROW_MAJOR;
48 typedef std::function<OUT_TYPE_AD(const IN_TYPE_AD&)> FUN_TYPE_AD;
50 typedef Derivatives<IN_DIM, OUT_DIM> DerivativesBase;
66 DerivativesCppad(FUN_TYPE_AD& f,
int inputDim = IN_DIM,
int outputDim = OUT_DIM)
67 : DerivativesBase(), adStdFun_(f), inputDim_(inputDim), outputDim_(outputDim)
69 update(f, inputDim, outputDim);
73 DerivativesCppad(
const DerivativesCppad& arg)
74 : DerivativesBase(arg), adStdFun_(arg.adStdFun_), inputDim_(arg.inputDim_), outputDim_(arg.outputDim_)
76 adCppadFun_ = arg.adCppadFun_;
89 void update(FUN_TYPE_AD& f,
const size_t inputDim = IN_DIM,
const size_t outputDim = OUT_DIM)
92 outputDim_ = outputDim;
94 if (outputDim_ > 0 && inputDim_ > 0)
99 virtual ~DerivativesCppad() {}
101 DerivativesCppad* clone()
const {
return new DerivativesCppad<IN_DIM, OUT_DIM>(*this); }
102 virtual OUT_TYPE_D forwardZero(
const Eigen::VectorXd& x) {
return adCppadFun_.Forward(0, x); }
103 virtual JAC_TYPE_D jacobian(
const Eigen::VectorXd& x)
106 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
109 Eigen::VectorXd jac = adCppadFun_.Jacobian(x);
111 JAC_TYPE_D out(outputDim_, x.rows());
112 out = JAC_TYPE_ROW_MAJOR::Map(jac.data(), outputDim_, x.rows());
116 virtual void sparseJacobian(
const Eigen::VectorXd& x,
117 Eigen::VectorXd& jac,
118 Eigen::VectorXi& iRow,
119 Eigen::VectorXi& jCol)
122 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
124 jac = adCppadFun_.SparseJacobian(x);
127 virtual Eigen::VectorXd sparseJacobianValues(
const Eigen::VectorXd& x)
130 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
132 return adCppadFun_.SparseJacobian(x);
136 virtual HES_TYPE_D hessian(
const Eigen::VectorXd& x,
const Eigen::VectorXd& lambda)
139 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
141 Eigen::VectorXd hessian = adCppadFun_.Hessian(x, lambda);
142 HES_TYPE_D out(x.rows(), x.rows());
143 out = HES_TYPE_ROW_MAJOR::Map(hessian.data(), x.rows(), x.rows());
147 virtual void sparseHessian(
const Eigen::VectorXd& x,
148 const Eigen::VectorXd& lambda,
149 Eigen::VectorXd& hes,
150 Eigen::VectorXi& iRow,
151 Eigen::VectorXi& jCol)
154 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
156 hes = adCppadFun_.SparseHessian(x, lambda);
160 virtual Eigen::VectorXd sparseHessianValues(
const Eigen::VectorXd& x,
const Eigen::VectorXd& lambda)
163 throw std::runtime_error(
"Outdim dim smaller 0; Define output dim in DerivativesCppad constructor");
165 return adCppadFun_.SparseHessian(x, lambda);
175 Eigen::Matrix<AD_SCALAR, Eigen::Dynamic, 1> x(inputDim_);
178 CppAD::Independent(x);
181 Eigen::Matrix<AD_SCALAR, Eigen::Dynamic, 1> y(outputDim_);
186 CppAD::ADFun<double> fAd(x, y);
190 std::cout <<
"AD FUn recorded" << std::endl;
195 std::function<OUT_TYPE_AD(const IN_TYPE_AD&)> adStdFun_;
200 CppAD::ADFun<double> adCppadFun_;