Sleipnir C++ API
Loading...
Searching...
No Matches
inertia.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <format>
6#include <limits>
7
8#include <Eigen/Core>
9
10namespace slp {
11
14class Inertia {
15 public:
17 int positive = 0;
19 int negative = 0;
21 int zero = 0;
22
23 constexpr Inertia() = default;
24
31 constexpr Inertia(int positive, int negative, int zero)
33
39 template <typename Scalar>
40 explicit Inertia(const Eigen::Vector<Scalar, Eigen::Dynamic>& D) {
41 for (const auto& elem : D) {
42 if (elem > std::numeric_limits<Scalar>::epsilon()) {
43 ++positive;
44 } else if (elem < -std::numeric_limits<Scalar>::epsilon()) {
45 ++negative;
46 } else {
47 ++zero;
48 }
49 }
50 }
51
57 template <typename Scalar>
58 explicit Inertia(
59 const Eigen::Diagonal<
60 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>>& D) {
61 for (const auto& elem : D) {
62 if (elem > std::numeric_limits<Scalar>::epsilon()) {
63 ++positive;
64 } else if (elem < -std::numeric_limits<Scalar>::epsilon()) {
65 ++negative;
66 } else {
67 ++zero;
68 }
69 }
70 }
71
75 bool operator==(const Inertia&) const = default;
76};
77
78} // namespace slp
79
81template <>
82struct std::formatter<slp::Inertia> {
87 constexpr auto parse(std::format_parse_context& ctx) {
88 return m_underlying.parse(ctx);
89 }
90
97 template <typename FmtContext>
98 auto format(const slp::Inertia& inertia, FmtContext& ctx) const {
99 auto out = ctx.out();
100
101 out = std::format_to(out, "(");
102 out = m_underlying.format(inertia.positive, ctx);
103 out = std::format_to(out, ", ");
104 out = m_underlying.format(inertia.negative, ctx);
105 out = std::format_to(out, ", ");
106 out = m_underlying.format(inertia.zero, ctx);
107 return std::format_to(out, ")");
108 }
109
110 private:
111 std::formatter<int> m_underlying;
112};
Definition inertia.hpp:14
int zero
The number of zero eigenvalues.
Definition inertia.hpp:21
Inertia(const Eigen::Diagonal< const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > > &D)
Definition inertia.hpp:58
constexpr Inertia(int positive, int negative, int zero)
Definition inertia.hpp:31
bool operator==(const Inertia &) const =default
int positive
The number of positive eigenvalues.
Definition inertia.hpp:17
Inertia(const Eigen::Vector< Scalar, Eigen::Dynamic > &D)
Definition inertia.hpp:40
int negative
The number of negative eigenvalues.
Definition inertia.hpp:19
Definition intrusive_shared_ptr.hpp:27
constexpr auto parse(std::format_parse_context &ctx)
Definition inertia.hpp:87
auto format(const slp::Inertia &inertia, FmtContext &ctx) const
Definition inertia.hpp:98