WPILibC++ 2027.0.0-alpha-2
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 slp {
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 : m_jacobian{std::move(variable), std::move(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, SleipnirMatrixLike auto wrt) noexcept
43 : m_jacobian{VariableMatrix{std::move(variable)}, std::move(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 * @return The gradient as a VariableMatrix.
52 */
53 VariableMatrix get() const { return m_jacobian.get().T(); }
54
55 /**
56 * Evaluates the gradient at wrt's value.
57 *
58 * @return The gradient at wrt's value.
59 */
60 const Eigen::SparseVector<double>& value() {
61 m_g = m_jacobian.value();
62
63 return m_g;
64 }
65
66 private:
67 Eigen::SparseVector<double> m_g;
68
69 Jacobian m_jacobian;
70};
71
72} // namespace slp
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:60
VariableMatrix get() const
Returns the gradient as a VariableMatrix.
Definition gradient.hpp:53
Gradient(Variable variable, SleipnirMatrixLike auto wrt) noexcept
Constructs a Gradient object.
Definition gradient.hpp:42
Gradient(Variable variable, Variable wrt) noexcept
Constructs a Gradient object.
Definition gradient.hpp:32
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition jacobian.hpp:25
An autodiff variable pointing to an expression node.
Definition variable.hpp:40
A matrix of autodiff variables.
Definition variable_matrix.hpp:29
VariableMatrix T() const
Returns the transpose of the variable matrix.
Definition variable_matrix.hpp:897
Definition concepts.hpp:30
Definition expression_graph.hpp:11
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34