7#include <Eigen/SparseCore>
8#include <gch/small_vector.hpp>
10#include "sleipnir/autodiff/adjoint_expression_graph.hpp"
11#include "sleipnir/autodiff/variable.hpp"
12#include "sleipnir/autodiff/variable_matrix.hpp"
13#include "sleipnir/util/assert.hpp"
14#include "sleipnir/util/concepts.hpp"
15#include "sleipnir/util/empty.hpp"
16#include "sleipnir/util/symbol_exports.hpp"
29template <
typename Scalar>
62 slp_assert(m_variables.cols() == 1);
63 slp_assert(m_wrt.cols() == 1);
66 for (
size_t col = 0; col < m_wrt.size(); ++col) {
67 m_wrt[col].expr->col = col;
75 for (
auto&
node : m_wrt) {
79 for (
int row = 0; row < m_variables.rows(); ++row) {
80 if (m_variables[row].expr ==
nullptr) {
84 if (m_variables[row].type() == ExpressionType::LINEAR) {
88 m_graphs[row].append_gradient_triplets(m_cached_triplets, row, m_wrt);
89 }
else if (m_variables[row].type() > ExpressionType::LINEAR) {
92 m_nonlinear_rows.emplace_back(row);
96 if (m_nonlinear_rows.empty()) {
97 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
113 for (
int row = 0; row < m_variables.rows(); ++row) {
114 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
115 for (
int col = 0; col < m_wrt.rows(); ++col) {
116 if (
grad[col].expr !=
nullptr) {
132 const Eigen::SparseMatrix<Scalar>&
value() {
133 if (m_nonlinear_rows.empty()) {
137 for (
auto&
graph : m_graphs) {
138 graph.update_values();
146 for (
int row : m_nonlinear_rows) {
147 m_graphs[row].append_gradient_triplets(
triplets, row, m_wrt);
159 gch::small_vector<detail::AdjointExpressionGraph<Scalar>> m_graphs;
161 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
164 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
168 gch::small_vector<int> m_nonlinear_rows;
171extern template class EXPORT_TEMPLATE_DECLARE(
172 SLEIPNIR_DLLEXPORT) Jacobian<double>;
Definition intrusive_shared_ptr.hpp:29
Definition jacobian.hpp:30
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition jacobian.hpp:38
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:49
const Eigen::SparseMatrix< Scalar > & value()
Definition jacobian.hpp:132
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:109
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:59
Definition variable_matrix.hpp:35
Definition variable.hpp:49
Definition concepts.hpp:33