WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Gradient.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/SparseCore>
8
14
15namespace sleipnir {
16
17/**
18 * This class calculates the gradient of a a variable with respect to a vector
19 * of variables.
20 *
21 * The gradient is only recomputed if the variable expression is quadratic or
22 * higher order.
23 */
25 public:
26 /**
27 * Constructs a Gradient object.
28 *
29 * @param variable Variable of which to compute the gradient.
30 * @param wrt Variable with respect to which to compute the gradient.
31 */
32 Gradient(Variable variable, Variable wrt) noexcept
33 : Gradient{std::move(variable), VariableMatrix{wrt}} {}
34
35 /**
36 * Constructs a Gradient object.
37 *
38 * @param variable Variable of which to compute the gradient.
39 * @param wrt Vector of variables with respect to which to compute the
40 * gradient.
41 */
42 Gradient(Variable variable, const VariableMatrix& wrt) noexcept
43 : m_jacobian{variable, wrt} {}
44
45 /**
46 * Returns the gradient as a VariableMatrix.
47 *
48 * This is useful when constructing optimization problems with derivatives in
49 * them.
50 */
51 VariableMatrix Get() const { return m_jacobian.Get().T(); }
52
53 /**
54 * Evaluates the gradient at wrt's value.
55 */
56 const Eigen::SparseVector<double>& Value() {
57 m_g = m_jacobian.Value();
58
59 return m_g;
60 }
61
62 /**
63 * Returns the profiler.
64 */
65 Profiler& GetProfiler() { return m_jacobian.GetProfiler(); }
66
67 private:
68 Eigen::SparseVector<double> m_g;
69
70 Jacobian m_jacobian;
71};
72
73} // namespace sleipnir
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class calculates the gradient of a a variable with respect to a vector of variables.
Definition Gradient.hpp:24
const Eigen::SparseVector< double > & Value()
Evaluates the gradient at wrt's value.
Definition Gradient.hpp:56
Gradient(Variable variable, const VariableMatrix &wrt) noexcept
Constructs a Gradient object.
Definition Gradient.hpp:42
Gradient(Variable variable, Variable wrt) noexcept
Constructs a Gradient object.
Definition Gradient.hpp:32
Profiler & GetProfiler()
Returns the profiler.
Definition Gradient.hpp:65
VariableMatrix Get() const
Returns the gradient as a VariableMatrix.
Definition Gradient.hpp:51
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
VariableMatrix T() const
Returns the transpose of the variable matrix.
Definition VariableMatrix.hpp:738
Definition Hessian.hpp:18