Sleipnir C++ API
Loading...
Searching...
No Matches
hessian.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7#include <Eigen/SparseCore>
8#include <gch/small_vector.hpp>
9
10#include "sleipnir/autodiff/gradient_expression_graph.hpp"
11#include "sleipnir/autodiff/variable.hpp"
12#include "sleipnir/autodiff/variable_matrix.hpp"
13#include "sleipnir/util/assert.hpp"
14#include "sleipnir/util/concepts.hpp"
15
16namespace slp {
17
26template <typename Scalar, int UpLo>
27 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
28class Hessian {
29 public:
36
43 : m_variables{detail::GradientExpressionGraph<Scalar>{variable}
44 .generate_tree(wrt)},
45 m_wrt{wrt} {
46 slp_assert(m_wrt.cols() == 1);
47
48 // Initialize column each expression's adjoint occupies in the Jacobian
49 for (size_t col = 0; col < m_wrt.size(); ++col) {
50 m_wrt[col].expr->col = col;
51 }
52
53 for (auto& variable : m_variables) {
54 m_graphs.emplace_back(variable);
55 }
56
57 // Reset col to -1
58 for (auto& node : m_wrt) {
59 node.expr->col = -1;
60 }
61
62 for (int row = 0; row < m_variables.rows(); ++row) {
63 if (m_variables[row].expr == nullptr) {
64 continue;
65 }
66
67 if (m_variables[row].type() == ExpressionType::LINEAR) {
68 // If the row is linear, compute its gradient once here and cache its
69 // triplets. Constant rows are ignored because their gradients have no
70 // nonzero triplets.
71 m_graphs[row].append_triplets(m_cached_triplets, row, m_wrt);
72 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
73 // If the row is quadratic or nonlinear, add it to the list of nonlinear
74 // rows to be recomputed in Value().
75 m_nonlinear_rows.emplace_back(row);
76 }
77 }
78
79 if (m_nonlinear_rows.empty()) {
80 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
81 if constexpr (UpLo == Eigen::Lower) {
82 m_H = m_H.template triangularView<Eigen::Lower>();
83 }
84 }
85 }
86
94 VariableMatrix<Scalar> result{detail::empty, m_variables.rows(),
95 m_wrt.rows()};
96
97 for (int row = 0; row < m_variables.rows(); ++row) {
98 auto grad = m_graphs[row].generate_tree(m_wrt);
99 for (int col = 0; col < m_wrt.rows(); ++col) {
100 if (grad[col].expr != nullptr) {
101 result[row, col] = std::move(grad[col]);
102 } else {
103 result[row, col] = Variable{Scalar(0)};
104 }
105 }
106 }
107
108 return result;
109 }
110
114 const Eigen::SparseMatrix<Scalar>& value() {
115 if (m_nonlinear_rows.empty()) {
116 return m_H;
117 }
118
119 for (auto& graph : m_graphs) {
120 graph.update_values();
121 }
122
123 // Copy the cached triplets so triplets added for the nonlinear rows are
124 // thrown away at the end of the function
125 auto triplets = m_cached_triplets;
126
127 // Compute each nonlinear row of the Hessian
128 for (int row : m_nonlinear_rows) {
129 m_graphs[row].append_triplets(triplets, row, m_wrt);
130 }
131
132 m_H.setFromTriplets(triplets.begin(), triplets.end());
133 if constexpr (UpLo == Eigen::Lower) {
134 m_H = m_H.template triangularView<Eigen::Lower>();
135 }
136
137 return m_H;
138 }
139
140 private:
141 VariableMatrix<Scalar> m_variables;
143
144 gch::small_vector<detail::GradientExpressionGraph<Scalar>> m_graphs;
145
146 Eigen::SparseMatrix<Scalar> m_H{m_variables.rows(), m_wrt.rows()};
147
148 // Cached triplets for gradients of linear rows
149 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
150
151 // List of row indices for nonlinear rows whose graients will be computed in
152 // Value()
153 gch::small_vector<int> m_nonlinear_rows;
154};
155
156// @cond Suppress Doxygen
157extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
158Hessian<double, Eigen::Lower | Eigen::Upper>;
159// @endcond
160
161} // namespace slp
Definition hessian.hpp:28
VariableMatrix< Scalar > get() const
Definition hessian.hpp:93
const Eigen::SparseMatrix< Scalar > & value()
Definition hessian.hpp:114
Hessian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition hessian.hpp:42
Hessian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition hessian.hpp:34
Definition intrusive_shared_ptr.hpp:27
Definition variable_matrix.hpp:33
Definition variable.hpp:47
Definition concepts.hpp:33