numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
format_dense_vector.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 <Eigen/Core>
23#include <fmt/base.h>
24
28
29namespace num_collect::util {
30
31namespace impl {
32
38template <typename Vector>
40public:
46 explicit dense_vector_format_view(const Vector& vec) : vec_(&vec) {}
47
53 [[nodiscard]] auto vec() const noexcept -> const Vector& { return *vec_; }
54
55private:
57 const Vector* vec_;
58};
59
60} // namespace impl
61
71template <typename Vector>
72[[nodiscard]] inline auto format_dense_vector(
73 const Eigen::DenseBase<Vector>& vec) {
75 vec.cols() == 1, "format_dense_vector requires a vector.");
76 return impl::dense_vector_format_view<Vector>(vec.derived());
77}
78
79} // namespace num_collect::util
80
81namespace fmt {
82
88template <typename Vector>
89struct formatter<num_collect::util::impl::dense_vector_format_view<Vector>>
90 : public formatter<typename Vector::Scalar> {
91public:
99 auto format(
101 format_context& context) const -> decltype(context.out()) {
102 const auto& vec = val.vec();
103 return format_impl(vec, context);
104 }
105
106private:
114 auto format_impl(const Vector& vec, format_context& context) const
115 -> decltype(context.out()) {
116 const Eigen::Index size = vec.size();
117 auto out = context.out();
118 *out = '[';
119 ++out;
120 for (Eigen::Index i = 0; i < size; ++i) {
121 if (i != 0) {
122 out = write_comma(out);
123 }
124 context.advance_to(out);
125 out = formatter<typename Vector::Scalar>::format(vec(i), context);
126 }
127 *out = ']';
128 ++out;
129 return out;
130 }
131
138 static auto write_comma(format_context::iterator out)
139 -> format_context::iterator {
140 *out = ',';
141 ++out;
142 *out = ' ';
143 ++out;
144 return out;
145 }
146};
147
148} // namespace fmt
Class of expressions to format dense vectors.
dense_vector_format_view(const Vector &vec)
Constructor.
auto vec() const noexcept -> const Vector &
Get the vector.
Definition of exceptions.
Definition of macros for logging.
Namespace of fmt library.
Definition log_level.h:114
Namespace of utilities.
Definition assert.h:30
auto format_dense_vector(const Eigen::DenseBase< Vector > &vec)
Format a dense vector.
Namespace of num_collect source codes.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
auto format(const num_collect::util::impl::dense_vector_format_view< Vector > &val, format_context &context) const -> decltype(context.out())
Format a value.
auto format_impl(const Vector &vec, format_context &context) const -> decltype(context.out())
Format a vector.
static auto write_comma(format_context::iterator out) -> format_context::iterator
Write a comma.