7#include <Eigen/SparseCore>
29 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
49 : m_variables{
detail::AdjointExpressionGraph{variable}
50 .generate_gradient_tree(wrt)},
55 for (
size_t col = 0; col < m_wrt.size(); ++col) {
56 m_wrt[col].expr->col = col;
59 for (
auto& variable : m_variables) {
60 m_graphs.emplace_back(variable);
64 for (
auto& node : m_wrt) {
68 for (
int row = 0; row < m_variables.rows(); ++row) {
69 if (m_variables[row].expr ==
nullptr) {
77 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
81 m_nonlinear_rows.emplace_back(row);
85 if (m_nonlinear_rows.empty()) {
86 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
87 if constexpr (UpLo == Eigen::Lower) {
88 m_H = m_H.triangularView<Eigen::Lower>();
105 for (
int row = 0; row < m_variables.rows(); ++row) {
106 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
107 for (
int col = 0; col < m_wrt.rows(); ++col) {
108 if (grad[col].expr !=
nullptr) {
109 result(row, col) = std::move(grad[col]);
124 const Eigen::SparseMatrix<double>&
value() {
125 if (m_nonlinear_rows.empty()) {
129 for (
auto& graph : m_graphs) {
130 graph.update_values();
135 auto triplets = m_cached_triplets;
138 for (
int row : m_nonlinear_rows) {
139 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
142 m_H.setFromTriplets(triplets.begin(), triplets.end());
143 if constexpr (UpLo == Eigen::Lower) {
144 m_H = m_H.triangularView<Eigen::Lower>();
156 Eigen::SparseMatrix<double> m_H{m_variables.
rows(), m_wrt.
rows()};
#define slp_assert(condition)
Abort in C++.
Definition assert.hpp:27
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition hessian.hpp:30
Hessian(Variable variable, SleipnirMatrixLike auto wrt)
Constructs a Hessian object.
Definition hessian.hpp:48
VariableMatrix get() const
Returns the Hessian as a VariableMatrix.
Definition hessian.hpp:101
Hessian(Variable variable, Variable wrt)
Constructs a Hessian object.
Definition hessian.hpp:38
const Eigen::SparseMatrix< double > & value()
Evaluates the Hessian at wrt's value.
Definition hessian.hpp:124
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
Definition concepts.hpp:30
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
wpi::SmallVector< T > small_vector
Definition small_vector.hpp:10
Definition expression_graph.hpp:11
@ LINEAR
The expression is composed of linear and lower-order operators.
Definition PointerIntPair.h:280
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34