7#include <Eigen/SparseCore>
35 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
36 m_profiler.StartSetup();
38 for (
int row = 0; row < m_wrt.Rows(); ++row) {
39 m_wrt(row).expr->row = row;
42 for (Variable variable : m_variables) {
43 m_graphs.emplace_back(variable.expr);
47 m_cachedTriplets.reserve(m_variables.Rows() * m_wrt.Rows() * 0.01);
49 for (
int row = 0; row < m_variables.Rows(); ++row) {
50 if (m_variables(row).Type() == ExpressionType::kLinear) {
54 m_graphs[row].ComputeAdjoints([&](
int col,
double adjoint) {
55 m_cachedTriplets.emplace_back(row, col, adjoint);
57 }
else if (m_variables(row).Type() > ExpressionType::kLinear) {
60 m_nonlinearRows.emplace_back(row);
64 for (
int row = 0; row < m_wrt.Rows(); ++row) {
65 m_wrt(row).expr->row = -1;
68 if (m_nonlinearRows.empty()) {
69 m_J.setFromTriplets(m_cachedTriplets.begin(), m_cachedTriplets.end());
72 m_profiler.StopSetup();
86 for (
auto& elem : m_wrt) {
90 for (
int row = 0; row < m_variables.Rows(); ++row) {
91 auto grad = m_graphs[row].GenerateGradientTree(wrtVec);
92 for (
int col = 0; col < m_wrt.Rows(); ++col) {
93 result(row, col) =
Variable{std::move(grad[col])};
103 const Eigen::SparseMatrix<double>&
Value() {
104 if (m_nonlinearRows.empty()) {
108 m_profiler.StartSolve();
110 for (
auto& graph : m_graphs) {
116 auto triplets = m_cachedTriplets;
119 for (
int row : m_nonlinearRows) {
120 m_graphs[row].ComputeAdjoints([&](
int col,
double adjoint) {
121 triplets.emplace_back(row, col, adjoint);
125 m_J.setFromTriplets(triplets.begin(), triplets.end());
127 m_profiler.StopSolve();
143 Eigen::SparseMatrix<double> m_J{m_variables.
Rows(), m_wrt.
Rows()};
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition Jacobian.hpp:25
VariableMatrix Get() const
Returns the Jacobian as a VariableMatrix.
Definition Jacobian.hpp:81
Profiler & GetProfiler()
Returns the profiler.
Definition Jacobian.hpp:135
const Eigen::SparseMatrix< double > & Value()
Evaluates the Jacobian at wrt's value.
Definition Jacobian.hpp:103
Jacobian(const VariableMatrix &variables, const VariableMatrix &wrt) noexcept
Constructs a Jacobian object.
Definition Jacobian.hpp:34
Records the number of profiler measurements (start/stop pairs) and the average duration between each ...
Definition Profiler.hpp:15
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
A matrix of autodiff variables.
Definition VariableMatrix.hpp:28
int Rows() const
Returns number of rows in the matrix.
Definition VariableMatrix.hpp:753
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
void reserve(size_type N)
Definition SmallVector.h:679
Definition Hessian.hpp:18