WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
jacobian.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7#include <Eigen/SparseCore>
9
16
17namespace slp {
18
19/**
20 * This class calculates the Jacobian of a vector of variables with respect to a
21 * vector of variables.
22 *
23 * The Jacobian is only recomputed if the variable expression is quadratic or
24 * higher order.
25 */
27 public:
28 /**
29 * Constructs a Jacobian object.
30 *
31 * @param variable Variable of which to compute the Jacobian.
32 * @param wrt Variable with respect to which to compute the Jacobian.
33 */
34 Jacobian(Variable variable, Variable wrt)
35 : Jacobian{VariableMatrix{std::move(variable)},
36 VariableMatrix{std::move(wrt)}} {}
37
38 /**
39 * Constructs a Jacobian object.
40 *
41 * @param variables Vector of variables of which to compute the Jacobian.
42 * @param wrt Vector of variables with respect to which to compute the
43 * Jacobian.
44 */
46 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
47 slp_assert(m_variables.cols() == 1);
48 slp_assert(m_wrt.cols() == 1);
49
50 // Initialize column each expression's adjoint occupies in the Jacobian
51 for (size_t col = 0; col < m_wrt.size(); ++col) {
52 m_wrt[col].expr->col = col;
53 }
54
55 for (auto& variable : m_variables) {
56 m_graphs.emplace_back(variable);
57 }
58
59 // Reset col to -1
60 for (auto& node : m_wrt) {
61 node.expr->col = -1;
62 }
63
64 for (int row = 0; row < m_variables.rows(); ++row) {
65 if (m_variables[row].expr == nullptr) {
66 continue;
67 }
68
69 if (m_variables[row].type() == ExpressionType::LINEAR) {
70 // If the row is linear, compute its gradient once here and cache its
71 // triplets. Constant rows are ignored because their gradients have no
72 // nonzero triplets.
73 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
74 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
75 // If the row is quadratic or nonlinear, add it to the list of nonlinear
76 // rows to be recomputed in Value().
77 m_nonlinear_rows.emplace_back(row);
78 }
79 }
80
81 if (m_nonlinear_rows.empty()) {
82 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
83 }
84 }
85
86 /**
87 * Returns the Jacobian as a VariableMatrix.
88 *
89 * This is useful when constructing optimization problems with derivatives in
90 * them.
91 *
92 * @return The Jacobian as a VariableMatrix.
93 */
95 VariableMatrix result{VariableMatrix::empty, m_variables.rows(),
96 m_wrt.rows()};
97
98 for (int row = 0; row < m_variables.rows(); ++row) {
99 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
100 for (int col = 0; col < m_wrt.rows(); ++col) {
101 if (grad[col].expr != nullptr) {
102 result(row, col) = std::move(grad[col]);
103 } else {
104 result(row, col) = Variable{0.0};
105 }
106 }
107 }
108
109 return result;
110 }
111
112 /**
113 * Evaluates the Jacobian at wrt's value.
114 *
115 * @return The Jacobian at wrt's value.
116 */
117 const Eigen::SparseMatrix<double>& value() {
118 if (m_nonlinear_rows.empty()) {
119 return m_J;
120 }
121
122 for (auto& graph : m_graphs) {
123 graph.update_values();
124 }
125
126 // Copy the cached triplets so triplets added for the nonlinear rows are
127 // thrown away at the end of the function
128 auto triplets = m_cached_triplets;
129
130 // Compute each nonlinear row of the Jacobian
131 for (int row : m_nonlinear_rows) {
132 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
133 }
134
135 m_J.setFromTriplets(triplets.begin(), triplets.end());
136
137 return m_J;
138 }
139
140 private:
141 VariableMatrix m_variables;
142 VariableMatrix m_wrt;
143
145
146 Eigen::SparseMatrix<double> m_J{m_variables.rows(), m_wrt.rows()};
147
148 // Cached triplets for gradients of linear rows
150
151 // List of row indices for nonlinear rows whose graients will be computed in
152 // Value()
153 gch::small_vector<int> m_nonlinear_rows;
154};
155
156} // namespace slp
#define slp_assert(condition)
Abort in C++.
Definition assert.hpp:27
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition jacobian.hpp:26
VariableMatrix get() const
Returns the Jacobian as a VariableMatrix.
Definition jacobian.hpp:94
Jacobian(VariableMatrix variables, SleipnirMatrixLike auto wrt)
Constructs a Jacobian object.
Definition jacobian.hpp:45
Jacobian(Variable variable, Variable wrt)
Constructs a Jacobian object.
Definition jacobian.hpp:34
const Eigen::SparseMatrix< double > & value()
Evaluates the Jacobian at wrt's value.
Definition jacobian.hpp:117
An autodiff variable pointing to an expression node.
Definition variable.hpp:40
A matrix of autodiff variables.
Definition variable_matrix.hpp:29
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:914
Definition concepts.hpp:30
wpi::SmallVector< T > small_vector
Definition small_vector.hpp:10
Definition expression_graph.hpp:11
Definition PointerIntPair.h:280
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34