WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
NumericalJacobian.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include "frc/EigenCore.h"
8
9namespace frc {
10
11/**
12 * Returns numerical Jacobian with respect to x for f(x).
13 *
14 * @tparam Rows Number of rows in result of f(x).
15 * @tparam Cols Number of columns in result of f(x).
16 * @param f Vector-valued function from which to compute Jacobian.
17 * @param x Vector argument.
18 */
19template <int Rows, int Cols, typename F>
20auto NumericalJacobian(F&& f, const Vectord<Cols>& x) {
21 constexpr double kEpsilon = 1e-5;
23 result.setZero();
24
25 // It's more expensive, but +- epsilon will be more accurate
26 for (int i = 0; i < Cols; ++i) {
27 Vectord<Cols> dX_plus = x;
28 dX_plus(i) += kEpsilon;
29 Vectord<Cols> dX_minus = x;
30 dX_minus(i) -= kEpsilon;
31 result.col(i) = (f(dX_plus) - f(dX_minus)) / (kEpsilon * 2.0);
32 }
33
34 return result;
35}
36
37/**
38 * Returns numerical Jacobian with respect to x for f(x).
39 *
40 * @param f Vector-valued function from which to compute Jacobian.
41 * @param x Vector argument.
42 */
43
44template <typename F>
45Eigen::MatrixXd NumericalJacobian(F&& f, const Eigen::VectorXd& x) {
46 constexpr double kEpsilon = 1e-5;
47 Eigen::MatrixXd result;
48
49 // It's more expensive, but +- epsilon will be more accurate
50 for (int i = 0; i < x.rows(); ++i) {
51 Eigen::VectorXd dX_plus = x;
52 dX_plus(i) += kEpsilon;
53 Eigen::VectorXd dX_minus = x;
54 dX_minus(i) -= kEpsilon;
55 Eigen::VectorXd partialDerivative =
56 (f(dX_plus) - f(dX_minus)) / (kEpsilon * 2.0);
57 if (i == 0) {
58 result.resize(partialDerivative.rows(), x.rows());
59 result.setZero();
60 }
61 result.col(i) = partialDerivative;
62 }
63
64 return result;
65}
66
67/**
68 * Returns numerical Jacobian with respect to x for f(x, u, ...).
69 *
70 * @tparam Rows Number of rows in result of f(x, u, ...).
71 * @tparam States Number of rows in x.
72 * @tparam Inputs Number of rows in u.
73 * @tparam F Function object type.
74 * @tparam Args... Types of remaining arguments to f(x, u, ...).
75 * @param f Vector-valued function from which to compute Jacobian.
76 * @param x State vector.
77 * @param u Input vector.
78 * @param args Remaining arguments to f(x, u, ...).
79 */
80template <int Rows, int States, int Inputs, typename F, typename... Args>
82 const Vectord<Inputs>& u, Args&&... args) {
84 [&](const Vectord<States>& x) { return f(x, u, args...); }, x);
85}
86
87/**
88 * Returns numerical Jacobian with respect to x for f(x, u, ...).
89 *
90 * @tparam F Function object type.
91 * @tparam Args... Types of remaining arguments to f(x, u, ...).
92 * @param f Vector-valued function from which to compute Jacobian.
93 * @param x State vector.
94 * @param u Input vector.
95 * @param args Remaining arguments to f(x, u, ...).
96 */
97template <typename F, typename... Args>
98auto NumericalJacobianX(F&& f, const Eigen::VectorXd& x,
99 const Eigen::VectorXd& u, Args&&... args) {
100 return NumericalJacobian(
101 [&](const Eigen::VectorXd& x) { return f(x, u, args...); }, x);
102}
103
104/**
105 * Returns numerical Jacobian with respect to u for f(x, u, ...).
106 *
107 * @tparam Rows Number of rows in result of f(x, u, ...).
108 * @tparam States Number of rows in x.
109 * @tparam Inputs Number of rows in u.
110 * @tparam F Function object type.
111 * @tparam Args... Types of remaining arguments to f(x, u, ...).
112 * @param f Vector-valued function from which to compute Jacobian.
113 * @param x State vector.
114 * @param u Input vector.
115 * @param args Remaining arguments to f(x, u, ...).
116 */
117template <int Rows, int States, int Inputs, typename F, typename... Args>
119 const Vectord<Inputs>& u, Args&&... args) {
121 [&](const Vectord<Inputs>& u) { return f(x, u, args...); }, u);
122}
123
124/**
125 * Returns numerical Jacobian with respect to u for f(x, u, ...).
126 *
127 * @tparam F Function object type.
128 * @tparam Args... Types of remaining arguments to f(x, u, ...).
129 * @param f Vector-valued function from which to compute Jacobian.
130 * @param x State vector.
131 * @param u Input vector.
132 * @param args Remaining arguments to f(x, u, ...).
133 */
134template <typename F, typename... Args>
135auto NumericalJacobianU(F&& f, const Eigen::VectorXd& x,
136 const Eigen::VectorXd& u, Args&&... args) {
137 return NumericalJacobian(
138 [&](const Eigen::VectorXd& u) { return f(x, u, args...); }, u);
139}
140
141} // namespace frc
Definition SystemServer.h:9
auto NumericalJacobian(F &&f, const Vectord< Cols > &x)
Returns numerical Jacobian with respect to x for f(x).
Definition NumericalJacobian.h:20
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition EigenCore.h:12
auto NumericalJacobianU(F &&f, const Vectord< States > &x, const Vectord< Inputs > &u, Args &&... args)
Returns numerical Jacobian with respect to u for f(x, u, ...).
Definition NumericalJacobian.h:118
auto NumericalJacobianX(F &&f, const Vectord< States > &x, const Vectord< Inputs > &u, Args &&... args)
Returns numerical Jacobian with respect to x for f(x, u, ...).
Definition NumericalJacobian.h:81