7#include <Eigen/SparseCore>
26template <
typename Scalar,
int UpLo>
27 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
43 : m_variables{
detail::GradientExpressionGraph<Scalar>{variable}
49 for (
size_t col = 0; col < m_wrt.size(); ++col) {
50 m_wrt[col].expr->col = col;
53 for (
auto& variable : m_variables) {
54 m_graphs.emplace_back(variable);
58 for (
auto& node : m_wrt) {
62 for (
int row = 0; row < m_variables.rows(); ++row) {
63 if (m_variables[row].expr ==
nullptr) {
67 if (m_variables[row].
type() == ExpressionType::LINEAR) {
71 m_graphs[row].append_triplets(m_cached_triplets, row, m_wrt);
72 }
else if (m_variables[row].
type() > ExpressionType::LINEAR) {
75 m_nonlinear_rows.emplace_back(row);
79 if (m_nonlinear_rows.empty()) {
80 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
81 if constexpr (UpLo == Eigen::Lower) {
82 m_H = m_H.template triangularView<Eigen::Lower>();
97 for (
int row = 0; row < m_variables.rows(); ++row) {
98 auto grad = m_graphs[row].generate_tree(m_wrt);
99 for (
int col = 0; col < m_wrt.rows(); ++col) {
100 if (grad[col].expr !=
nullptr) {
101 result(row, col) = std::move(grad[col]);
103 result(row, col) =
Variable{Scalar(0)};
114 const Eigen::SparseMatrix<Scalar>&
value() {
115 if (m_nonlinear_rows.empty()) {
119 for (
auto& graph : m_graphs) {
120 graph.update_values();
125 auto triplets = m_cached_triplets;
128 for (
int row : m_nonlinear_rows) {
129 m_graphs[row].append_triplets(triplets, row, m_wrt);
132 m_H.setFromTriplets(triplets.begin(), triplets.end());
133 if constexpr (UpLo == Eigen::Lower) {
134 m_H = m_H.template triangularView<Eigen::Lower>();
146 Eigen::SparseMatrix<Scalar> m_H{m_variables.
rows(), m_wrt.
rows()};
158Hessian<double, Eigen::Lower | Eigen::Upper>;
#define EXPORT_TEMPLATE_DECLARE(export)
Definition SymbolExports.hpp:94
#define slp_assert(condition)
Abort in C++.
Definition assert.hpp:25
VariableMatrix< Scalar > get() const
Returns the Hessian as a VariableMatrix.
Definition hessian.hpp:93
const Eigen::SparseMatrix< Scalar > & value()
Evaluates the Hessian at wrt's value.
Definition hessian.hpp:114
Hessian(Variable< Scalar > variable, SleipnirMatrixLike< Scalar > auto wrt)
Constructs a Hessian object.
Definition hessian.hpp:42
Hessian(Variable< Scalar > variable, Variable< Scalar > wrt)
Constructs a Hessian object.
Definition hessian.hpp:34
An autodiff variable pointing to an expression node.
Definition variable.hpp:47
A matrix of autodiff variables.
Definition variable_matrix.hpp:33
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:972
Definition concepts.hpp:33
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
type
Definition base.h:985
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition empty.hpp:11
Definition expression_graph.hpp:11
Definition StringMap.hpp:773
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34