7#include <Eigen/SparseCore>
8#include <gch/small_vector.hpp>
10#include "sleipnir/autodiff/gradient_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"
27template <
typename Scalar>
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);
58 for (
size_t col = 0; col < m_wrt.size(); ++col) {
59 m_wrt[col].expr->col = col;
67 for (
auto&
node : m_wrt) {
71 for (
int row = 0; row < m_variables.rows(); ++row) {
72 if (m_variables[row].expr ==
nullptr) {
76 if (m_variables[row].type() == ExpressionType::LINEAR) {
80 m_graphs[row].append_triplets(m_cached_triplets, row, m_wrt);
81 }
else if (m_variables[row].type() > ExpressionType::LINEAR) {
84 m_nonlinear_rows.emplace_back(row);
88 if (m_nonlinear_rows.empty()) {
89 m_J.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
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) {
120 const Eigen::SparseMatrix<Scalar>&
value() {
121 if (m_nonlinear_rows.empty()) {
125 for (
auto&
graph : m_graphs) {
126 graph.update_values();
134 for (
int row : m_nonlinear_rows) {
135 m_graphs[row].append_triplets(
triplets, row, m_wrt);
147 gch::small_vector<detail::GradientExpressionGraph<Scalar>> m_graphs;
149 Eigen::SparseMatrix<Scalar> m_J{m_variables.rows(), m_wrt.rows()};
152 gch::small_vector<Eigen::Triplet<Scalar>> m_cached_triplets;
156 gch::small_vector<int> m_nonlinear_rows;
159extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
Definition intrusive_shared_ptr.hpp:27
Definition jacobian.hpp:28
Jacobian(Variable< Scalar > variable, Variable< Scalar > wrt)
Definition jacobian.hpp:34
Jacobian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:43
const Eigen::SparseMatrix< Scalar > & value()
Definition jacobian.hpp:120
VariableMatrix< Scalar > get() const
Definition jacobian.hpp:99
Jacobian(VariableMatrix< Scalar > variables, SleipnirMatrixLike< Scalar > auto wrt)
Definition jacobian.hpp:51
Definition variable_matrix.hpp:33
Definition variable.hpp:47
Definition concepts.hpp:33