WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Hessian.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7#include <Eigen/Core>
8#include <Eigen/SparseCore>
9#include <wpi/SmallVector.h>
10
17
18namespace sleipnir {
19
20/**
21 * This class calculates the Hessian of a variable with respect to a vector of
22 * variables.
23 *
24 * The gradient tree is cached so subsequent Hessian calculations are faster,
25 * and the Hessian is only recomputed if the variable expression is nonlinear.
26 */
28 public:
29 /**
30 * Constructs a Hessian object.
31 *
32 * @param variable Variable of which to compute the Hessian.
33 * @param wrt Vector of variables with respect to which to compute the
34 * Hessian.
35 */
36 Hessian(Variable variable, const VariableMatrix& wrt) noexcept
37 : m_jacobian{
38 [&] {
40 wrtVec.reserve(wrt.size());
41 for (auto& elem : wrt) {
42 wrtVec.emplace_back(elem.expr);
43 }
44
45 auto grad =
47 wrtVec);
48
49 VariableMatrix ret{wrt.Rows()};
50 for (int row = 0; row < ret.Rows(); ++row) {
51 ret(row) = Variable{std::move(grad[row])};
52 }
53 return ret;
54 }(),
55 wrt} {}
56
57 /**
58 * Returns the Hessian as a VariableMatrix.
59 *
60 * This is useful when constructing optimization problems with derivatives in
61 * them.
62 */
63 VariableMatrix Get() const { return m_jacobian.Get(); }
64
65 /**
66 * Evaluates the Hessian at wrt's value.
67 */
68 const Eigen::SparseMatrix<double>& Value() { return m_jacobian.Value(); }
69
70 /**
71 * Returns the profiler.
72 */
73 Profiler& GetProfiler() { return m_jacobian.GetProfiler(); }
74
75 private:
76 Jacobian m_jacobian;
77};
78
79} // namespace sleipnir
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition Hessian.hpp:27
Profiler & GetProfiler()
Returns the profiler.
Definition Hessian.hpp:73
Hessian(Variable variable, const VariableMatrix &wrt) noexcept
Constructs a Hessian object.
Definition Hessian.hpp:36
VariableMatrix Get() const
Returns the Hessian as a VariableMatrix.
Definition Hessian.hpp:63
const Eigen::SparseMatrix< double > & Value()
Evaluates the Hessian at wrt's value.
Definition Hessian.hpp:68
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition Jacobian.hpp:25
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
This class is an adaptor type that performs value updates of an expression's computational graph in a...
Definition ExpressionGraph.hpp:19
wpi::SmallVector< ExpressionPtr > GenerateGradientTree(std::span< const ExpressionPtr > wrt) const
Returns the variable's gradient tree.
Definition ExpressionGraph.hpp:123
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