WPILibC++ 2027.0.0-alpha-4
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
17
18namespace slp {
19
20/// This class calculates the Jacobian of a vector of variables with respect to
21/// a vector of variables.
22///
23/// The Jacobian is only recomputed if the variable expression is quadratic or
24/// higher order.
25///
26/// @tparam Scalar Scalar type.
27template <typename Scalar>
28class Jacobian {
29 public:
30 /// Constructs a Jacobian object.
31 ///
32 /// @param variable Variable of which to compute the Jacobian.
33 /// @param wrt Variable with respect to which to compute the Jacobian.
35 : Jacobian{VariableMatrix<Scalar>{std::move(variable)},
36 VariableMatrix<Scalar>{std::move(wrt)}} {}
37
38 /// Constructs a Jacobian object.
39 ///
40 /// @param variable Variable of which to compute the Jacobian.
41 /// @param wrt Vector of variables with respect to which to compute the
42 /// Jacobian.
44 : Jacobian{VariableMatrix<Scalar>{std::move(variable)}, std::move(wrt)} {}
45
46 /// Constructs a Jacobian object.
47 ///
48 /// @param variables Vector of variables of which to compute the Jacobian.
49 /// @param wrt Vector of variables with respect to which to compute the
50 /// Jacobian.
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
93 /// Returns the Jacobian as a VariableMatrix.
94 ///
95 /// This is useful when constructing optimization problems with derivatives in
96 /// them.
97 ///
98 /// @return The Jacobian as a VariableMatrix.
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
117 /// Evaluates the Jacobian at wrt's value.
118 ///
119 /// @return The Jacobian at wrt's value.
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
148
149 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
150
151 // Cached triplets for gradients of linear rows
153
154 // List of row indices for nonlinear rows whose graients will be computed in
155 // Value()
156 gch::small_vector<int> m_nonlinear_rows;
157};
158
159extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
160Jacobian<double>;
161
162} // namespace slp
#define EXPORT_TEMPLATE_DECLARE(export)
Definition SymbolExports.hpp:94
#define slp_assert(condition)
Abort in C++.
Definition assert.hpp:25
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Constructs a Jacobian object.
Definition jacobian.hpp:34
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Constructs a Jacobian object.
Definition jacobian.hpp:43
const Eigen::SparseMatrix< Scalar > & value()
Evaluates the Jacobian at wrt's value.
Definition jacobian.hpp:120
VariableMatrix< Scalar > get() const
Returns the Jacobian as a VariableMatrix.
Definition jacobian.hpp:99
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Constructs a Jacobian object.
Definition jacobian.hpp:51
An autodiff variable pointing to an expression node.
Definition variable.hpp:47
A matrix of autodiff variables.
Definition variable_matrix.hpp:33
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:972
Definition concepts.hpp:33
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition empty.hpp:11
Definition expression_graph.hpp:11
@ LINEAR
The expression is composed of linear and lower-order operators.
Definition expression_type.hpp:22
Definition StringMap.hpp:773
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34