numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
format_sparse_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 <Eigen/SparseCore> // IWYU pragma: keep
26#include <fmt/base.h>
27
31
32namespace num_collect::util {
33
37enum class sparse_matrix_format_type : std::uint8_t {
40
43};
44
45namespace impl {
46
52template <base::concepts::sparse_matrix Matrix>
54public:
63
69 [[nodiscard]] auto mat() const noexcept -> const Matrix& { return *mat_; }
70
76 [[nodiscard]] auto type() const noexcept -> sparse_matrix_format_type {
77 return type_;
78 }
79
80private:
82 const Matrix* mat_;
83
86};
87
88} // namespace impl
89
100template <base::concepts::sparse_matrix Matrix>
101[[nodiscard]] inline auto format_sparse_matrix(const Matrix& mat,
104}
105
106} // namespace num_collect::util
107
108namespace fmt {
109
115template <num_collect::base::concepts::sparse_matrix Matrix>
116struct formatter<num_collect::util::impl::sparse_matrix_format_view<Matrix>>
117 : public formatter<typename Matrix::Scalar> {
118public:
126 auto format(
128 format_context& context) const -> decltype(context.out()) {
129 const auto& mat = val.mat();
130 switch (val.type()) {
132 return format_one_line(mat, context);
134 return format_multi_line(mat, context);
135 }
137 num_collect::invalid_argument, "Invalid format type.");
138 }
139
140private:
148 auto format_one_line(const Matrix& mat, format_context& context) const
149 -> decltype(context.out()) {
150 const Eigen::Index rows = mat.rows();
151 const Eigen::Index cols = mat.cols();
152 auto out = context.out();
153 *out = '[';
154 ++out;
155 for (Eigen::Index row = 0; row < rows; ++row) {
156 if (row != 0) {
157 out = write_comma(out);
158 }
159 *out = '[';
160 ++out;
161 for (Eigen::Index col = 0; col < cols; ++col) {
162 if (col != 0) {
163 out = write_comma(out);
164 }
165 context.advance_to(out);
166 out = formatter<typename Matrix::Scalar>::format(
167 mat.coeff(row, col), context);
168 }
169 *out = ']';
170 ++out;
171 }
172 *out = ']';
173 ++out;
174 return out;
175 }
176
184 auto format_multi_line(const Matrix& mat, format_context& context) const
185 -> decltype(context.out()) {
186 const Eigen::Index rows = mat.rows();
187 const Eigen::Index cols = mat.cols();
188 auto out = context.out();
189 *out = '[';
190 ++out;
191 *out = '\n';
192 ++out;
193 for (Eigen::Index row = 0; row < rows; ++row) {
194 if (row != 0) {
195 *out = ',';
196 ++out;
197 *out = '\n';
198 ++out;
199 }
200 *out = ' ';
201 ++out;
202 *out = ' ';
203 ++out;
204 *out = '[';
205 ++out;
206 for (Eigen::Index col = 0; col < cols; ++col) {
207 if (col != 0) {
208 out = write_comma(out);
209 }
210 context.advance_to(out);
211 out = formatter<typename Matrix::Scalar>::format(
212 mat.coeff(row, col), context);
213 }
214 *out = ']';
215 ++out;
216 }
217 *out = '\n';
218 ++out;
219 *out = ']';
220 ++out;
221 return out;
222 }
223
230 static auto write_comma(format_context::iterator out)
231 -> format_context::iterator {
232 *out = ',';
233 ++out;
234 *out = ' ';
235 ++out;
236 return out;
237 }
238};
239
240} // namespace fmt
Class of exception on invalid arguments.
Definition exception.h:85
Class of expressions to format sparse matrices.
auto mat() const noexcept -> const Matrix &
Get the matrix.
auto type() const noexcept -> sparse_matrix_format_type
Get the type.
sparse_matrix_format_view(const Matrix &mat, sparse_matrix_format_type type)
Constructor.
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
sparse_matrix_format_type
Enumeration of matrix format types.
auto format_sparse_matrix(const Matrix &mat, sparse_matrix_format_type type=sparse_matrix_format_type::one_line)
Format a sparse matrix.
Namespace of num_collect source codes.
Definition of sparse_matrix concept.
auto format(const num_collect::util::impl::sparse_matrix_format_view< Matrix > &val, format_context &context) const -> decltype(context.out())
Format a value.
auto format_multi_line(const Matrix &mat, format_context &context) const -> decltype(context.out())
Format a matrix in multiple lines.
static auto write_comma(format_context::iterator out) -> format_context::iterator
Write a comma.
auto format_one_line(const Matrix &mat, format_context &context) const -> decltype(context.out())
Format a matrix in one line.