Sleipnir C++ API
Loading...
Searching...
No Matches
jacobian.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7#include <Eigen/SparseCore>
8
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"
17
18namespace slp {
19
27class SLEIPNIR_DLLEXPORT Jacobian {
28 public:
35 Jacobian(Variable variable, Variable wrt) noexcept
36 : Jacobian{VariableMatrix{std::move(variable)},
37 VariableMatrix{std::move(wrt)}} {}
38
46 Jacobian(VariableMatrix variables, SleipnirMatrixLike auto wrt) noexcept
47 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
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_adjoint_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_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
81 }
82
83 m_profilers.emplace_back("");
84 m_profilers.emplace_back(" ↳ graph update");
85 m_profilers.emplace_back(" ↳ adjoints");
86 m_profilers.emplace_back(" ↳ matrix build");
87 }
88
98 VariableMatrix result{VariableMatrix::empty, m_variables.rows(),
99 m_wrt.rows()};
100
101 for (int row = 0; row < m_variables.rows(); ++row) {
102 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
103 for (int col = 0; col < m_wrt.rows(); ++col) {
104 if (grad[col].expr != nullptr) {
105 result(row, col) = std::move(grad[col]);
106 } else {
107 result(row, col) = Variable{0.0};
108 }
109 }
110 }
111
112 return result;
113 }
114
120 const Eigen::SparseMatrix<double>& value() {
121 ScopedProfiler value_profiler{m_profilers[0]};
122
123 if (m_nonlinear_rows.empty()) {
124 return m_J;
125 }
126
127 ScopedProfiler graph_update_profiler{m_profilers[1]};
128
129 for (auto& graph : m_graphs) {
130 graph.update_values();
131 }
132
133 graph_update_profiler.stop();
134 ScopedProfiler adjoints_profiler{m_profilers[2]};
135
136 // Copy the cached triplets so triplets added for the nonlinear rows are
137 // thrown away at the end of the function
138 auto triplets = m_cached_triplets;
139
140 // Compute each nonlinear row of the Jacobian
141 for (int row : m_nonlinear_rows) {
142 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
143 }
144
145 adjoints_profiler.stop();
146 ScopedProfiler matrix_build_profiler{m_profilers[3]};
147
148 if (!triplets.empty()) {
149 m_J.setFromTriplets(triplets.begin(), triplets.end());
150 } else {
151 // setFromTriplets() is a no-op on empty triplets, so explicitly zero out
152 // the storage
153 m_J.setZero();
154 }
155
156 return m_J;
157 }
158
164 const small_vector<SolveProfiler>& get_profilers() const {
165 return m_profilers;
166 }
167
168 private:
169 VariableMatrix m_variables;
170 VariableMatrix m_wrt;
171
172 small_vector<detail::AdjointExpressionGraph> m_graphs;
173
174 Eigen::SparseMatrix<double> m_J{m_variables.rows(), m_wrt.rows()};
175
176 // Cached triplets for gradients of linear rows
177 small_vector<Eigen::Triplet<double>> m_cached_triplets;
178
179 // List of row indices for nonlinear rows whose graients will be computed in
180 // Value()
181 small_vector<int> m_nonlinear_rows;
182
183 small_vector<SolveProfiler> m_profilers;
184};
185
186} // namespace slp
Definition jacobian.hpp:27
Jacobian(Variable variable, Variable wrt) noexcept
Definition jacobian.hpp:35
const small_vector< SolveProfiler > & get_profilers() const
Definition jacobian.hpp:164
VariableMatrix get() const
Definition jacobian.hpp:97
const Eigen::SparseMatrix< double > & value()
Definition jacobian.hpp:120
Jacobian(VariableMatrix variables, SleipnirMatrixLike auto wrt) noexcept
Definition jacobian.hpp:46
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
Definition variable.hpp:41
Definition concepts.hpp:23