numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
format_dense_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstdint>
23
24#include <Eigen/Core>
25#include <fmt/base.h>
26
29
30namespace num_collect::util {
31
35enum class dense_matrix_format_type : std::uint8_t {
38
41};
42
43namespace impl {
44
50template <typename Matrix>
52public:
61
67 [[nodiscard]] auto mat() const noexcept -> const Matrix& { return *mat_; }
68
74 [[nodiscard]] auto type() const noexcept -> dense_matrix_format_type {
75 return type_;
76 }
77
78private:
80 const Matrix* mat_;
81
84};
85
86} // namespace impl
87
98template <typename Matrix>
99[[nodiscard]] inline auto format_dense_matrix(
100 const Eigen::DenseBase<Matrix>& mat,
102 return impl::dense_matrix_format_view<Matrix>(mat.derived(), type);
103}
104
105} // namespace num_collect::util
106
107namespace fmt {
108
114template <typename Matrix>
115struct formatter<num_collect::util::impl::dense_matrix_format_view<Matrix>>
116 : public formatter<typename Matrix::Scalar> {
117public:
125 auto format(
127 format_context& context) const -> decltype(context.out()) {
128 const auto& mat = val.mat();
129 switch (val.type()) {
131 return format_one_line(mat, context);
133 return format_multi_line(mat, context);
134 }
136 num_collect::invalid_argument, "Invalid format type.");
137 }
138
139private:
147 auto format_one_line(const Matrix& mat, format_context& context) const
148 -> decltype(context.out()) {
149 const Eigen::Index rows = mat.rows();
150 const Eigen::Index cols = mat.cols();
151 auto out = context.out();
152 *out = '[';
153 ++out;
154 for (Eigen::Index row = 0; row < rows; ++row) {
155 if (row != 0) {
156 out = write_comma(out);
157 }
158 *out = '[';
159 ++out;
160 for (Eigen::Index col = 0; col < cols; ++col) {
161 if (col != 0) {
162 out = write_comma(out);
163 }
164 context.advance_to(out);
165 out = formatter<typename Matrix::Scalar>::format(
166 mat(row, col), context);
167 }
168 *out = ']';
169 ++out;
170 }
171 *out = ']';
172 ++out;
173 return out;
174 }
175
183 auto format_multi_line(const Matrix& mat, format_context& context) const
184 -> decltype(context.out()) {
185 const Eigen::Index rows = mat.rows();
186 const Eigen::Index cols = mat.cols();
187 auto out = context.out();
188 *out = '[';
189 ++out;
190 *out = '\n';
191 ++out;
192 for (Eigen::Index row = 0; row < rows; ++row) {
193 if (row != 0) {
194 *out = ',';
195 ++out;
196 *out = '\n';
197 ++out;
198 }
199 *out = ' ';
200 ++out;
201 *out = ' ';
202 ++out;
203 *out = '[';
204 ++out;
205 for (Eigen::Index col = 0; col < cols; ++col) {
206 if (col != 0) {
207 out = write_comma(out);
208 }
209 context.advance_to(out);
210 out = formatter<typename Matrix::Scalar>::format(
211 mat(row, col), context);
212 }
213 *out = ']';
214 ++out;
215 }
216 *out = '\n';
217 ++out;
218 *out = ']';
219 ++out;
220 return out;
221 }
222
229 static auto write_comma(format_context::iterator out)
230 -> format_context::iterator {
231 *out = ',';
232 ++out;
233 *out = ' ';
234 ++out;
235 return out;
236 }
237};
238
239} // namespace fmt
Class of exception on invalid arguments.
Definition exception.h:85
Class of expressions to format dense matrices.
dense_matrix_format_view(const Matrix &mat, dense_matrix_format_type type)
Constructor.
auto mat() const noexcept -> const Matrix &
Get the matrix.
auto type() const noexcept -> dense_matrix_format_type
Get the type.
Definition of exceptions.
Definition of macros for logging.
#define NUM_COLLECT_LOG_AND_THROW(EXCEPTION_TYPE,...)
Write an error log and throw an exception for an error.
Namespace of fmt library.
Definition log_level.h:114
Namespace of utilities.
Definition assert.h:30
auto format_dense_matrix(const Eigen::DenseBase< Matrix > &mat, dense_matrix_format_type type=dense_matrix_format_type::one_line)
Format a dense matrix.
dense_matrix_format_type
Enumeration of matrix format types.
Namespace of num_collect source codes.
auto format_multi_line(const Matrix &mat, format_context &context) const -> decltype(context.out())
Format a matrix in multiple lines.
auto format(const num_collect::util::impl::dense_matrix_format_view< Matrix > &val, format_context &context) const -> decltype(context.out())
Format a value.
auto format_one_line(const Matrix &mat, format_context &context) const -> decltype(context.out())
Format a matrix in one line.
static auto write_comma(format_context::iterator out) -> format_context::iterator
Write a comma.