7#include <Eigen/SparseCore>
46 : m_variables{
std::move(variables)}, m_wrt{
std::move(wrt)} {
51 for (
size_t col = 0; col < m_wrt.size(); ++col) {
52 m_wrt[col].expr->col = col;
55 for (
auto& variable : m_variables) {
56 m_graphs.emplace_back(variable);
60 for (
auto& node : m_wrt) {
64 for (
int row = 0; row < m_variables.rows(); ++row) {
65 if (m_variables[row].expr ==
nullptr) {
69 if (m_variables[row].type() == ExpressionType::LINEAR) {
73 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
74 }
else if (m_variables[row].type() > ExpressionType::LINEAR) {
77 m_nonlinear_rows.emplace_back(row);
81 if (m_nonlinear_rows.empty()) {
82 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
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]);
117 const Eigen::SparseMatrix<double>&
value() {
118 if (m_nonlinear_rows.empty()) {
122 for (
auto& graph : m_graphs) {
123 graph.update_values();
128 auto triplets = m_cached_triplets;
131 for (
int row : m_nonlinear_rows) {
132 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
135 m_J.setFromTriplets(triplets.begin(), triplets.end());
146 Eigen::SparseMatrix<double> m_J{m_variables.
rows(), m_wrt.
rows()};
#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