7#include <Eigen/SparseCore>
28 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
52 for (
size_t col = 0; col < m_wrt.size(); ++col) {
53 m_wrt[col].expr->col = col;
56 for (
auto& variable : m_variables) {
57 m_graphs.emplace_back(variable);
61 for (
auto& node : m_wrt) {
65 for (
int row = 0; row < m_variables.rows(); ++row) {
66 if (m_variables[row].expr ==
nullptr) {
74 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
78 m_nonlinear_rows.emplace_back(row);
82 if (m_nonlinear_rows.empty()) {
83 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
84 if constexpr (UpLo == Eigen::Lower) {
85 m_H = m_H.triangularView<Eigen::Lower>();
102 for (
int row = 0; row < m_variables.rows(); ++row) {
103 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
104 for (
int col = 0; col < m_wrt.rows(); ++col) {
105 if (grad[col].expr !=
nullptr) {
106 result(row, col) = std::move(grad[col]);
121 const Eigen::SparseMatrix<double>&
value() {
122 if (m_nonlinear_rows.empty()) {
126 for (
auto& graph : m_graphs) {
127 graph.update_values();
132 auto triplets = m_cached_triplets;
135 for (
int row : m_nonlinear_rows) {
136 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
139 if (!triplets.empty()) {
140 m_H.setFromTriplets(triplets.begin(), triplets.end());
141 if constexpr (UpLo == Eigen::Lower) {
142 m_H = m_H.triangularView<Eigen::Lower>();
159 Eigen::SparseMatrix<double> m_H{m_variables.
rows(), m_wrt.
rows()};
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition hessian.hpp:29
Hessian(Variable variable, SleipnirMatrixLike auto wrt) noexcept
Constructs a Hessian object.
Definition hessian.hpp:47
Hessian(Variable variable, Variable wrt) noexcept
Constructs a Hessian object.
Definition hessian.hpp:37
VariableMatrix get() const
Returns the Hessian as a VariableMatrix.
Definition hessian.hpp:98
const Eigen::SparseMatrix< double > & value()
Evaluates the Hessian at wrt's value.
Definition hessian.hpp:121
An autodiff variable pointing to an expression node.
Definition variable.hpp:40
A matrix of autodiff variables.
Definition variable_matrix.hpp:29
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:914
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition variable_matrix.hpp:39
This class is an adaptor type that performs value updates of an expression's adjoint graph.
Definition adjoint_expression_graph.hpp:21
VariableMatrix generate_gradient_tree(const VariableMatrix &wrt) const
Returns the variable's gradient tree.
Definition adjoint_expression_graph.hpp:52
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
Definition concepts.hpp:30
Definition expression_graph.hpp:11
@ LINEAR
The expression is composed of linear and lower-order operators.
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34