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#include <gch/small_vector.hpp>
9
10#include "sleipnir/autodiff/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#include "sleipnir/util/symbol_exports.hpp"
16
17namespace slp {
18
26template <typename Scalar>
27class Jacobian {
28 public:
36
44
52 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
53 slp_assert(m_variables.cols() == 1);
54 slp_assert(m_wrt.cols() == 1);
55
56 for (auto& variable : m_variables) {
57 m_top_lists.emplace_back(detail::topological_sort(variable.expr));
58 }
59
60 // Initialize column each expression's adjoint occupies in the Jacobian
61 for (size_t col = 0; col < m_wrt.size(); ++col) {
62 m_wrt[col].expr->scratch = col;
63 }
64
65 // Make list of output rows as column-node pairs
66 for (auto& top_list : m_top_lists) {
67 m_output_lists.emplace_back();
68 for (const auto& node : top_list) {
69 if (node->scratch != -1) {
70 m_output_lists.back().emplace_back(node->scratch, node);
71 }
72 }
73 }
74
75 // Reset col to -1
76 for (auto& node : m_wrt) {
77 node.expr->scratch = -1;
78 }
79
80 for (int row = 0; row < m_variables.rows(); ++row) {
81 if (m_variables[row].expr == nullptr) {
82 continue;
83 }
84
85 if (m_variables[row].type() == ExpressionType::LINEAR) {
86 // If the row is linear, compute its gradient once here and cache its
87 // triplets. Constant rows are ignored because their gradients have no
88 // nonzero triplets.
89 detail::append_triplets(m_top_lists[row], m_output_lists[row],
90 m_cached_triplets, row);
91 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
92 // If the row is quadratic or nonlinear, add it to the list of nonlinear
93 // rows to be recomputed in value().
94 m_nonlinear_rows.emplace_back(row);
95 }
96 }
97
98 if (m_nonlinear_rows.empty()) {
99 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
100 }
101 }
102
110 VariableMatrix<Scalar> result{detail::empty, m_variables.rows(),
111 m_wrt.rows()};
112
113 for (int row = 0; row < m_variables.rows(); ++row) {
114 auto grad = detail::gradient_tree(m_top_lists[row], m_wrt);
115 for (int col = 0; col < m_wrt.rows(); ++col) {
116 if (grad[col].expr != nullptr) {
117 result[row, col] = std::move(grad[col]);
118 } else {
119 result[row, col] = Variable{Scalar(0)};
120 }
121 }
122 }
123
124 return result;
125 }
126
130 const Eigen::SparseMatrix<Scalar>& value() {
131 if (m_nonlinear_rows.empty()) {
132 return m_J;
133 }
134
135 for (auto& top_list : m_top_lists) {
136 detail::update_values(top_list);
137 }
138
139 // Copy the cached triplets so triplets added for the nonlinear rows are
140 // thrown away at the end of the function
141 auto triplets = m_cached_triplets;
142
143 // Compute each nonlinear row of the Jacobian
144 for (int row : m_nonlinear_rows) {
145 detail::append_triplets(m_top_lists[row], m_output_lists[row], triplets,
146 row);
147 }
148
149 m_J.setFromTriplets(triplets.begin(), triplets.end());
150
151 return m_J;
152 }
153
154 private:
155 VariableMatrix<Scalar> m_variables;
157
159 gch::small_vector<detail::ExpressionGraph<Scalar>> m_top_lists;
160
162 gch::small_vector<
163 gch::small_vector<std::pair<int, detail::Expression<Scalar>*>>>
164 m_output_lists;
165
166 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
167
169 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
170
173 gch::small_vector<int> m_nonlinear_rows;
174};
175
176extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
177Jacobian<double>;
178
179} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition jacobian.hpp:27
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition jacobian.hpp:33
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:42
const Eigen::SparseMatrix< Scalar > & value()
Definition jacobian.hpp:130
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:109
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:50
Definition variable_matrix.hpp:33
Definition variable.hpp:52
Definition concepts.hpp:33