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/gradient_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/empty.hpp"
16#include "sleipnir/util/symbol_exports.hpp"
17
18namespace slp {
19
27template <typename Scalar>
28class Jacobian {
29 public:
37
45
53 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
54 slp_assert(m_variables.cols() == 1);
55 slp_assert(m_wrt.cols() == 1);
56
57 // Initialize column each expression's adjoint occupies in the Jacobian
58 for (size_t col = 0; col < m_wrt.size(); ++col) {
59 m_wrt[col].expr->col = col;
60 }
61
62 for (auto& variable : m_variables) {
63 m_graphs.emplace_back(variable);
64 }
65
66 // Reset col to -1
67 for (auto& node : m_wrt) {
68 node.expr->col = -1;
69 }
70
71 for (int row = 0; row < m_variables.rows(); ++row) {
72 if (m_variables[row].expr == nullptr) {
73 continue;
74 }
75
76 if (m_variables[row].type() == ExpressionType::LINEAR) {
77 // If the row is linear, compute its gradient once here and cache its
78 // triplets. Constant rows are ignored because their gradients have no
79 // nonzero triplets.
80 m_graphs[row].append_triplets(m_cached_triplets, row, m_wrt);
81 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
82 // If the row is quadratic or nonlinear, add it to the list of nonlinear
83 // rows to be recomputed in Value().
84 m_nonlinear_rows.emplace_back(row);
85 }
86 }
87
88 if (m_nonlinear_rows.empty()) {
89 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
90 }
91 }
92
100 VariableMatrix<Scalar> result{detail::empty, m_variables.rows(),
101 m_wrt.rows()};
102
103 for (int row = 0; row < m_variables.rows(); ++row) {
104 auto grad = m_graphs[row].generate_tree(m_wrt);
105 for (int col = 0; col < m_wrt.rows(); ++col) {
106 if (grad[col].expr != nullptr) {
107 result[row, col] = std::move(grad[col]);
108 } else {
109 result[row, col] = Variable{Scalar(0)};
110 }
111 }
112 }
113
114 return result;
115 }
116
120 const Eigen::SparseMatrix<Scalar>& value() {
121 if (m_nonlinear_rows.empty()) {
122 return m_J;
123 }
124
125 for (auto& graph : m_graphs) {
126 graph.update_values();
127 }
128
129 // Copy the cached triplets so triplets added for the nonlinear rows are
130 // thrown away at the end of the function
131 auto triplets = m_cached_triplets;
132
133 // Compute each nonlinear row of the Jacobian
134 for (int row : m_nonlinear_rows) {
135 m_graphs[row].append_triplets(triplets, row, m_wrt);
136 }
137
138 m_J.setFromTriplets(triplets.begin(), triplets.end());
139
140 return m_J;
141 }
142
143 private:
144 VariableMatrix<Scalar> m_variables;
146
147 gch::small_vector<detail::GradientExpressionGraph<Scalar>> m_graphs;
148
149 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
150
152 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
153
156 gch::small_vector<int> m_nonlinear_rows;
157};
158
159extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
160Jacobian<double>;
161
162} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition jacobian.hpp:28
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition jacobian.hpp:34
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:43
const Eigen::SparseMatrix< Scalar > & value()
Definition jacobian.hpp:120
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:99
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:51
Definition variable_matrix.hpp:33
Definition variable.hpp:47
Definition concepts.hpp:33