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/expression_type.hpp"
14#include "sleipnir/autodiff/variable.hpp"
15#include "sleipnir/autodiff/variable_matrix.hpp"
16#include "sleipnir/util/assert.hpp"
17#include "sleipnir/util/concepts.hpp"
18#include "sleipnir/util/empty.hpp"
19#include "sleipnir/util/symbol_exports.hpp"
20
21namespace slp {
22
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 (m_variables[row].type() == ExpressionType::LINEAR) {
90 // If the row is linear, compute its gradient once here and cache its
91 // triplets. Constant rows are ignored because their gradients have no
92 // nonzero triplets.
93 detail::append_triplets(m_top_lists[row], m_output_lists[row],
94 m_cached_triplets, row);
95 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
96 // If the row is quadratic or nonlinear, add it to the list of nonlinear
97 // rows to be recomputed in value().
98 m_nonlinear_rows.emplace_back(row);
99 }
100 }
101
102 if (m_nonlinear_rows.empty()) {
103 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
104 }
105 }
106
114 VariableMatrix<Scalar> result{detail::empty, m_variables.rows(),
115 m_wrt.rows()};
116
117 for (int row = 0; row < m_variables.rows(); ++row) {
118 auto grad = detail::gradient_tree(m_top_lists[row], m_wrt);
119 for (int col = 0; col < m_wrt.rows(); ++col) {
120 if (grad[col].expr != nullptr) {
121 result[row, col] = std::move(grad[col]);
122 } else {
123 result[row, col] = Variable{Scalar(0)};
124 }
125 }
126 }
127
128 return result;
129 }
130
134 const Eigen::SparseMatrix<Scalar>& value() {
135 if (m_nonlinear_rows.empty()) {
136 return m_J;
137 }
138
139 for (auto& top_list : m_top_lists) {
140 detail::update_values(top_list);
141 }
142
143 // Copy the cached triplets so triplets added for the nonlinear rows are
144 // thrown away at the end of the function
145 auto triplets = m_cached_triplets;
146
147 // Compute each nonlinear row of the Jacobian
148 for (int row : m_nonlinear_rows) {
149 detail::append_triplets(m_top_lists[row], m_output_lists[row], triplets,
150 row);
151 }
152
153 m_J.setFromTriplets(triplets.begin(), triplets.end());
154
155 return m_J;
156 }
157
158 private:
159 VariableMatrix<Scalar> m_variables;
161
163 gch::small_vector<detail::ExpressionGraph<Scalar>> m_top_lists;
164
166 gch::small_vector<
167 gch::small_vector<std::pair<int, detail::Expression<Scalar>*>>>
168 m_output_lists;
169
170 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
171
173 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
174
177 gch::small_vector<int> m_nonlinear_rows;
178};
179
180extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
181Jacobian<double>;
182
183} // 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:134
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:113
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:54
Definition variable_matrix.hpp:35
Definition variable.hpp:55
Definition concepts.hpp:33