Sleipnir C++ API
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>
8
15
16namespace sleipnir {
17
26 public:
34 Jacobian(const VariableMatrix& variables, const VariableMatrix& wrt) noexcept
35 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
36 m_profiler.StartSetup();
37
38 for (int row = 0; row < m_wrt.Rows(); ++row) {
39 m_wrt(row).expr->row = row;
40 }
41
42 for (auto& variable : m_variables) {
43 m_graphs.emplace_back(variable);
44 }
45
46 for (int row = 0; row < m_variables.Rows(); ++row) {
47 if (m_variables(row).Type() == ExpressionType::kLinear) {
48 // If the row is linear, compute its gradient once here and cache its
49 // triplets. Constant rows are ignored because their gradients have no
50 // nonzero triplets.
51 m_graphs[row].ComputeAdjoints([&](int col, double adjoint) {
52 m_cachedTriplets.emplace_back(row, col, adjoint);
53 });
54 } else if (m_variables(row).Type() > ExpressionType::kLinear) {
55 // If the row is quadratic or nonlinear, add it to the list of nonlinear
56 // rows to be recomputed in Value().
57 m_nonlinearRows.emplace_back(row);
58 }
59 }
60
61 for (int row = 0; row < m_wrt.Rows(); ++row) {
62 m_wrt(row).expr->row = -1;
63 }
64
65 if (m_nonlinearRows.empty()) {
66 m_J.setFromTriplets(m_cachedTriplets.begin(), m_cachedTriplets.end());
67 }
68
69 m_profiler.StopSetup();
70 }
71
79 VariableMatrix result{m_variables.Rows(), m_wrt.Rows()};
80
81 for (int row = 0; row < m_variables.Rows(); ++row) {
82 for (auto& node : m_wrt) {
83 node.expr->adjointExpr = nullptr;
84 }
85
86 auto grad = m_graphs[row].GenerateGradientTree(m_wrt);
87 for (int col = 0; col < m_wrt.Rows(); ++col) {
88 if (grad(col).expr != nullptr) {
89 result(row, col) = std::move(grad(col));
90 }
91 }
92 }
93
94 return result;
95 }
96
100 const Eigen::SparseMatrix<double>& Value() {
101 m_profiler.StartSolve();
102
103 if (m_nonlinearRows.empty()) {
104 m_profiler.StopSolve();
105 return m_J;
106 }
107
108 for (auto& graph : m_graphs) {
109 graph.Update();
110 }
111
112 // Copy the cached triplets so triplets added for the nonlinear rows are
113 // thrown away at the end of the function
114 auto triplets = m_cachedTriplets;
115
116 // Compute each nonlinear row of the Jacobian
117 for (int row : m_nonlinearRows) {
118 m_graphs[row].ComputeAdjoints([&](int col, double adjoint) {
119 triplets.emplace_back(row, col, adjoint);
120 });
121 }
122
123 m_J.setFromTriplets(triplets.begin(), triplets.end());
124
125 m_profiler.StopSolve();
126
127 return m_J;
128 }
129
133 Profiler& GetProfiler() { return m_profiler; }
134
135 private:
136 VariableMatrix m_variables;
137 VariableMatrix m_wrt;
138
140
141 Eigen::SparseMatrix<double> m_J{m_variables.Rows(), m_wrt.Rows()};
142
143 // Cached triplets for gradients of linear rows
144 small_vector<Eigen::Triplet<double>> m_cachedTriplets;
145
146 // List of row indices for nonlinear rows whose graients will be computed in
147 // Value()
148 small_vector<int> m_nonlinearRows;
149
150 Profiler m_profiler;
151};
152
153} // namespace sleipnir
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
Definition Jacobian.hpp:25
VariableMatrix Get() const
Definition Jacobian.hpp:78
Profiler & GetProfiler()
Definition Jacobian.hpp:133
const Eigen::SparseMatrix< double > & Value()
Definition Jacobian.hpp:100
Jacobian(const VariableMatrix &variables, const VariableMatrix &wrt) noexcept
Definition Jacobian.hpp:34
Definition Profiler.hpp:13
Definition VariableMatrix.hpp:28
int Rows() const
Definition VariableMatrix.hpp:773
Definition small_vector.hpp:3616
Definition Expression.hpp:18