WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
inertia.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <Eigen/Core>
6
7namespace slp {
8
9/// Represents the inertia of a matrix (the number of positive, negative, and
10/// zero eigenvalues).
11class Inertia {
12 public:
13 /// The number of positive eigenvalues.
14 int positive = 0;
15 /// The number of negative eigenvalues.
16 int negative = 0;
17 /// The number of zero eigenvalues.
18 int zero = 0;
19
20 constexpr Inertia() = default;
21
22 /// Constructs Inertia with the given number of positive, negative, and zero
23 /// eigenvalues.
24 ///
25 /// @param positive The number of positive eigenvalues.
26 /// @param negative The number of negative eigenvalues.
27 /// @param zero The number of zero eigenvalues.
28 constexpr Inertia(int positive, int negative, int zero)
30
31 /// Constructs Inertia from the D matrix of an LDLT decomposition
32 /// (see https://en.wikipedia.org/wiki/Sylvester's_law_of_inertia).
33 ///
34 /// @tparam Scalar Scalar type.
35 /// @param D The D matrix of an LDLT decomposition in vector form.
36 template <typename Scalar>
37 explicit Inertia(const Eigen::Vector<Scalar, Eigen::Dynamic>& D) {
38 for (const auto& elem : D) {
39 if (elem > Scalar(0)) {
40 ++positive;
41 } else if (elem < Scalar(0)) {
42 ++negative;
43 } else {
44 ++zero;
45 }
46 }
47 }
48
49 /// Constructs Inertia from the D matrix of an LDLT decomposition
50 /// (see https://en.wikipedia.org/wiki/Sylvester's_law_of_inertia).
51 ///
52 /// @tparam Scalar Scalar type.
53 /// @param D The D matrix of an LDLT decomposition in vector form.
54 template <typename Scalar>
55 explicit Inertia(
56 const Eigen::Diagonal<
57 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>>& D) {
58 for (const auto& elem : D) {
59 if (elem > Scalar(0)) {
60 ++positive;
61 } else if (elem < Scalar(0)) {
62 ++negative;
63 } else {
64 ++zero;
65 }
66 }
67 }
68
69 /// Inertia equality operator.
70 ///
71 /// @return True if Inertia is equal.
72 bool operator==(const Inertia&) const = default;
73};
74
75} // namespace slp
constexpr Inertia()=default
int zero
The number of zero eigenvalues.
Definition inertia.hpp:18
Inertia(const Eigen::Diagonal< const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > > &D)
Constructs Inertia from the D matrix of an LDLT decomposition (see https://en.wikipedia....
Definition inertia.hpp:55
constexpr Inertia(int positive, int negative, int zero)
Constructs Inertia with the given number of positive, negative, and zero eigenvalues.
Definition inertia.hpp:28
bool operator==(const Inertia &) const =default
Inertia equality operator.
int positive
The number of positive eigenvalues.
Definition inertia.hpp:14
Inertia(const Eigen::Vector< Scalar, Eigen::Dynamic > &D)
Constructs Inertia from the D matrix of an LDLT decomposition (see https://en.wikipedia....
Definition inertia.hpp:37
int negative
The number of negative eigenvalues.
Definition inertia.hpp:16
Definition expression_graph.hpp:11