WPILibC++ 2023.4.3
Eigen.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 <fmt/format.h>
8
9#include "Eigen/Core"
10#include "Eigen/SparseCore"
11
12/**
13 * Formatter for Eigen::Matrix<double, Rows, Cols>.
14 *
15 * @tparam Rows Number of rows.
16 * @tparam Cols Number of columns.
17 * @tparam Args Defaulted template arguments to Eigen::Matrix<>.
18 */
19template <int Rows, int Cols, int... Args>
20struct fmt::formatter<Eigen::Matrix<double, Rows, Cols, Args...>> {
21 /**
22 * Storage for format specifier.
23 */
24 char presentation = 'f';
25
26 /**
27 * Format string parser.
28 *
29 * @param ctx Format string context.
30 */
31 constexpr auto parse(fmt::format_parse_context& ctx) {
32 auto it = ctx.begin(), end = ctx.end();
33 if (it != end && (*it == 'f' || *it == 'e')) {
34 presentation = *it++;
35 }
36
37 if (it != end && *it != '}') {
38 throw fmt::format_error("invalid format");
39 }
40
41 return it;
42 }
43
44 /**
45 * Writes out a formatted matrix.
46 *
47 * @tparam FormatContext Format string context type.
48 * @param mat Matrix to format.
49 * @param ctx Format string context.
50 */
51 template <typename FormatContext>
53 FormatContext& ctx) {
54 auto out = ctx.out();
55 for (int i = 0; i < mat.rows(); ++i) {
56 for (int j = 0; j < mat.cols(); ++j) {
57 out = fmt::format_to(out, " {:f}", mat(i, j));
58 }
59
60 if (i < mat.rows() - 1) {
61 out = fmt::format_to(out, "\n");
62 }
63 }
64
65 return out;
66 }
67};
68
69/**
70 * Formatter for Eigen::SparseMatrix<double>.
71 *
72 * @tparam Options Union of bit flags controlling the storage scheme.
73 * @tparam StorageIndex The type of the indices.
74 */
75template <int Options, typename StorageIndex>
76struct fmt::formatter<Eigen::SparseMatrix<double, Options, StorageIndex>> {
77 /**
78 * Storage for format specifier.
79 */
80 char presentation = 'f';
81
82 /**
83 * Format string parser.
84 *
85 * @param ctx Format string context.
86 */
87 constexpr auto parse(fmt::format_parse_context& ctx) {
88 auto it = ctx.begin(), end = ctx.end();
89 if (it != end && (*it == 'f' || *it == 'e')) {
90 presentation = *it++;
91 }
92
93 if (it != end && *it != '}') {
94 throw fmt::format_error("invalid format");
95 }
96
97 return it;
98 }
99
100 /**
101 * Writes out a formatted matrix.
102 *
103 * @tparam FormatContext Format string context type.
104 * @param mat Matrix to format.
105 * @param ctx Format string context.
106 */
107 template <typename FormatContext>
109 FormatContext& ctx) {
110 auto out = ctx.out();
111 for (int i = 0; i < mat.rows(); ++i) {
112 for (int j = 0; j < mat.cols(); ++j) {
113 out = fmt::format_to(out, " {:f}", mat.coeff(i, j));
114 }
115
116 if (i < mat.rows() - 1) {
117 out = fmt::format_to(out, "\n");
118 }
119 }
120
121 return out;
122 }
123};
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:145
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:143
A versatible sparse matrix representation.
Definition: SparseMatrix.h:98
Scalar coeff(Index row, Index col) const
Definition: SparseMatrix.h:190
Index rows() const
Definition: SparseMatrix.h:138
Index cols() const
Definition: SparseMatrix.h:140
basic_format_parse_context< char > format_parse_context
Definition: core.h:724
static EIGEN_DEPRECATED const end_t end
Definition: IndexedViewHelper.h:181
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
auto format(const Eigen::Matrix< double, Rows, Cols, Args... > &mat, FormatContext &ctx)
Writes out a formatted matrix.
Definition: Eigen.h:52
constexpr auto parse(fmt::format_parse_context &ctx)
Format string parser.
Definition: Eigen.h:31
auto format(const Eigen::SparseMatrix< double, Options, StorageIndex > &mat, FormatContext &ctx)
Writes out a formatted matrix.
Definition: Eigen.h:108
constexpr auto parse(fmt::format_parse_context &ctx)
Format string parser.
Definition: Eigen.h:87
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
Definition: xchar.h:136