9 #ifndef EIGEN_AUTODIFF_CHAIN_HESSIAN_SPARSE_H_
10 #define EIGEN_AUTODIFF_CHAIN_HESSIAN_SPARSE_H_
18 template <
typename Functor>
25 #if EIGEN_HAS_VARIADIC_TEMPLATES
26 template <
typename... T>
31 template <
typename T0>
35 template <
typename T0,
typename T1>
39 template <
typename T0,
typename T1,
typename T2>
47 typedef typename ValueType::Scalar
Scalar;
61 typedef typename JacobianType::Index
Index;
71 typedef Matrix<OuterActiveScalar, InputsAtCompileTime, 1>
ActiveInput;
72 typedef Matrix<OuterActiveScalar, ValuesAtCompileTime, 1>
ActiveValue;
74 #if EIGEN_HAS_VARIADIC_TEMPLATES
83 template <
typename... ParamsType>
89 template <
typename... ParamsType>
92 AutoDiffChainJacobianSparse<Functor> autoj(*
static_cast<const Functor *
>(
this));
93 autoj(x, v, jac, Params...);
96 template <
typename... ParamsType>
98 const ParamsType &... Params)
const
100 AutoDiffChainJacobianSparse<Functor> autoj(*
static_cast<const Functor *
>(
this));
101 autoj(x, v, jac, ijac, Params...);
104 template <
typename... ParamsType>
107 this->
operator()(x, v, jac, hess,
nullptr,
nullptr, Params...);
110 template <
typename... ParamsType>
112 const ParamsType &... Params)
const
114 this->
operator()(x, v, jac, hess, &ijac, &ihess, Params...);
118 template <
typename... ParamsType>
120 const ParamsType &... Params)
const
137 autoj(
x, v, jac, ijac);
142 this->
operator()(x, v, jac, hess,
nullptr,
nullptr);
147 this->
operator()(x, v, jac, hess, &ijac, &ihess);
157 eigen_assert((_ijac && _ihess) || (!_ijac && !_ihess));
161 eigen_assert(
x.rows() == jac.cols());
163 for (
Index j = 0; j < jac.rows(); ++j)
165 av[j].derivatives().resize(
x.rows());
166 av[j].derivatives().reserve(
x.rows());
167 for (
Index k = 0; k <
x.rows(); ++k)
168 av[j].derivatives().insert(k).derivatives().resize(
x.rows());
171 for (
Index i = 0; i <
x.rows(); ++i)
173 ax[i].derivatives().resize(
x.rows());
174 ax[i].derivatives().insert(i) = 1.0;
175 ax[i].value().derivatives().resize(
x.rows());
176 ax[i].value().derivatives().insert(i) = 1.0;
177 ax[i].derivatives().coeffRef(i).derivatives().resize(
x.rows());
186 eigen_assert(
x.rows() == ihess.rows());
187 eigen_assert(ijac.cols() == ihess[0].rows() && ijac.cols() == ihess[0].cols());
189 for (
Index j = 0; j < jac.rows(); ++j)
191 av[j].derivatives().resize(ijac.cols());
192 av[j].derivatives().reserve(ijac.cols());
193 for (
Index k = 0; k < ijac.cols(); ++k)
194 av[j].derivatives().insert(k).derivatives().resize(ijac.cols());
197 for (
Index i = 0; i <
x.rows(); ++i)
199 ax[i].derivatives().resize(ijac.cols());
200 ax[i].derivatives() = ijac.row(i);
201 ax[i].value().derivatives().resize(ijac.cols());
202 ax[i].value().derivatives() = ijac.row(i);
203 for (
Index k = 0; k < ijac.cols(); ++k)
205 ax[i].derivatives().coeffRef(k).derivatives() = ihess[i].row(k);
210 #if EIGEN_HAS_VARIADIC_TEMPLATES
211 Functor::operator()(ax, av, Params...);
213 Functor::operator()(ax, av);
216 Index cols = _ijac ? _ijac->cols() :
x.rows();
218 hess.resize(jac.rows());
219 for (
Index i = 0; i < jac.rows(); ++i)
220 hess[i].resize(cols, cols);
223 for (
int i = 0; i < jac.rows(); ++i)
225 v[i] = av[i].value().value();
228 jac.insert(i, it.row()) = av[i].value().derivatives().coeffRef(it.row());
231 hess[i].insert(it.row(), ith.row()) = av[i].derivatives().coeffRef(it.row()).derivatives().coeffRef(ith.row());
240 #endif // EIGEN_AUTODIFF_CHAIN_HESSIAN_SPARSE_H_