Sleipnir C++ API
Loading...
Searching...
No Matches
jacobian.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <cstddef>
6#include <utility>
7
8#include <Eigen/SparseCore>
9#include <gch/small_vector.hpp>
10
11#include "sleipnir/autodiff/expression.hpp"
12#include "sleipnir/autodiff/expression_graph.hpp"
13#include "sleipnir/autodiff/variable.hpp"
14#include "sleipnir/autodiff/variable_matrix.hpp"
15#include "sleipnir/util/assert.hpp"
16#include "sleipnir/util/concepts.hpp"
17#include "sleipnir/util/empty.hpp"
18#include "sleipnir/util/symbol_exports.hpp"
19
20namespace slp {
21
30template <typename Scalar>
31class Jacobian {
32 public:
40
48
56 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
57 slp_assert(m_variables.cols() == 1);
58 slp_assert(m_wrt.cols() == 1);
59
60 for (auto& variable : m_variables) {
61 m_top_lists.emplace_back(detail::topological_sort(variable.expr));
62 }
63
64 // Initialize column each expression's adjoint occupies in the Jacobian
65 for (size_t col = 0; col < m_wrt.size(); ++col) {
66 m_wrt[col].expr->scratch = col;
67 }
68
69 // Make list of output rows as column-node pairs
70 for (auto& top_list : m_top_lists) {
71 m_output_lists.emplace_back();
72 for (const auto& node : top_list) {
73 if (node->scratch != -1) {
74 m_output_lists.back().emplace_back(node->scratch, node);
75 }
76 }
77 }
78
79 // Reset col to -1
80 for (auto& node : m_wrt) {
81 node.expr->scratch = -1;
82 }
83
84 for (int row = 0; row < m_variables.rows(); ++row) {
85 if (m_variables[row].expr == nullptr) {
86 continue;
87 }
88
89 if (detail::has_constant_gradient(m_top_lists[row])) {
90 // If the row's gradient is constant, compute it once here and cache
91 // its triplets
92 detail::append_triplets(m_top_lists[row], m_output_lists[row],
93 m_cached_triplets, row);
94 } else {
95 // Otherwise, add it to the list of rows to be recomputed in value()
96 m_nonlinear_rows.emplace_back(row);
97 }
98 }
99
100 if (m_nonlinear_rows.empty()) {
101 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
102 }
103 }
104
112 VariableMatrix<Scalar> result{detail::empty, m_variables.rows(),
113 m_wrt.rows()};
114
115 for (int row = 0; row < m_variables.rows(); ++row) {
116 auto grad = detail::gradient_tree(m_top_lists[row], m_wrt);
117 for (int col = 0; col < m_wrt.rows(); ++col) {
118 if (grad[col].expr != nullptr) {
119 result[row, col] = std::move(grad[col]);
120 } else {
121 result[row, col] = Variable{Scalar(0)};
122 }
123 }
124 }
125
126 return result;
127 }
128
132 const Eigen::SparseMatrix<Scalar>& value() {
133 if (m_nonlinear_rows.empty()) {
134 return m_J;
135 }
136
137 for (auto& top_list : m_top_lists) {
138 detail::update_values(top_list);
139 }
140
141 // Copy the cached triplets so triplets added for the nonlinear rows are
142 // thrown away at the end of the function
143 auto triplets = m_cached_triplets;
144
145 // Compute each nonlinear row of the Jacobian
146 for (int row : m_nonlinear_rows) {
147 detail::append_triplets(m_top_lists[row], m_output_lists[row], triplets,
148 row);
149 }
150
151 m_J.setFromTriplets(triplets.begin(), triplets.end());
152
153 return m_J;
154 }
155
156 private:
157 VariableMatrix<Scalar> m_variables;
159
161 gch::small_vector<detail::ExpressionGraph<Scalar>> m_top_lists;
162
164 gch::small_vector<
165 gch::small_vector<std::pair<int, detail::Expression<Scalar>*>>>
166 m_output_lists;
167
168 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
169
171 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
172
175 gch::small_vector<int> m_nonlinear_rows;
176};
177
178extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
179Jacobian<double>;
180
181} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition jacobian.hpp:31
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition jacobian.hpp:37
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:46
const Eigen::SparseMatrix< Scalar > & value()
Definition jacobian.hpp:132
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:111
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:54
Definition variable_matrix.hpp:38
Definition variable.hpp:63
Definition concepts.hpp:33