7#include <Eigen/SparseCore>
9#include "sleipnir/autodiff/adjoint_expression_graph.hpp"
10#include "sleipnir/autodiff/variable.hpp"
11#include "sleipnir/autodiff/variable_matrix.hpp"
12#include "sleipnir/util/concepts.hpp"
13#include "sleipnir/util/scoped_profiler.hpp"
14#include "sleipnir/util/small_vector.hpp"
15#include "sleipnir/util/solve_profiler.hpp"
16#include "sleipnir/util/symbol_exports.hpp"
30 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
54 for (
size_t col = 0; col < m_wrt.size(); ++col) {
55 m_wrt[col].expr->
col = col;
58 for (
auto& variable : m_variables) {
59 m_graphs.emplace_back(variable);
63 for (
auto& node : m_wrt) {
67 for (
int row = 0; row < m_variables.rows(); ++row) {
68 if (m_variables[row].expr ==
nullptr) {
72 if (m_variables[row].type() == ExpressionType::LINEAR) {
76 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
77 }
else if (m_variables[row].type() > ExpressionType::LINEAR) {
80 m_nonlinear_rows.emplace_back(row);
84 if (m_nonlinear_rows.empty()) {
85 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
86 if constexpr (UpLo == Eigen::Lower) {
87 m_H = m_H.triangularView<Eigen::Lower>();
91 m_profilers.emplace_back(
"");
92 m_profilers.emplace_back(
" ↳ graph update");
93 m_profilers.emplace_back(
" ↳ adjoints");
94 m_profilers.emplace_back(
" ↳ matrix build");
109 for (
int row = 0; row < m_variables.rows(); ++row) {
110 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
111 for (
int col = 0; col < m_wrt.rows(); ++col) {
112 if (grad[col].expr !=
nullptr) {
113 result(row, col) = std::move(grad[col]);
128 const Eigen::SparseMatrix<double>&
value() {
131 if (m_nonlinear_rows.empty()) {
137 for (
auto& graph : m_graphs) {
138 graph.update_values();
141 graph_update_profiler.
stop();
146 auto triplets = m_cached_triplets;
149 for (
int row : m_nonlinear_rows) {
150 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
153 adjoints_profiler.
stop();
156 if (!triplets.empty()) {
157 m_H.setFromTriplets(triplets.begin(), triplets.end());
158 if constexpr (UpLo == Eigen::Lower) {
159 m_H = m_H.triangularView<Eigen::Lower>();
183 small_vector<detail::AdjointExpressionGraph> m_graphs;
185 Eigen::SparseMatrix<double> m_H{m_variables.
rows(), m_wrt.
rows()};
188 small_vector<Eigen::Triplet<double>> m_cached_triplets;
192 small_vector<int> m_nonlinear_rows;
194 small_vector<SolveProfiler> m_profilers;
Definition hessian.hpp:31
const small_vector< SolveProfiler > & get_profilers() const
Definition hessian.hpp:175
Hessian(Variable variable, SleipnirMatrixLike auto wrt) noexcept
Definition hessian.hpp:49
Hessian(Variable variable, Variable wrt) noexcept
Definition hessian.hpp:39
VariableMatrix get() const
Definition hessian.hpp:105
const Eigen::SparseMatrix< double > & value()
Definition hessian.hpp:128
Definition scoped_profiler.hpp:19
void stop()
Definition scoped_profiler.hpp:57
Definition variable_matrix.hpp:29
int rows() const
Definition variable_matrix.hpp:907
VariableBlock< VariableMatrix > col(int col)
Definition variable_matrix.hpp:546
static constexpr empty_t empty
Definition variable_matrix.hpp:39
Definition variable.hpp:41
Definition adjoint_expression_graph.hpp:21
VariableMatrix generate_gradient_tree(const VariableMatrix &wrt) const
Definition adjoint_expression_graph.hpp:52
Definition concepts.hpp:23