numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
adc_ternary_vector.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <cstddef>
23#include <cstdint>
24#include <functional>
25#include <utility>
26
27#include <Eigen/Core>
28
34
35namespace num_collect::opt::impl {
36
44template <base::concepts::real_scalar_dense_vector VariableType,
45 index_type MaxDigits>
47public:
48 static_assert(MaxDigits > 0, "MaxDigits must be a positive integer.");
49
51 using digit_type = std::int8_t;
52
55 VariableType::RowsAtCompileTime;
56
59 VariableType::MaxRowsAtCompileTime;
60
62 static constexpr index_type max_digits_at_compile_time = MaxDigits;
63
67 adc_ternary_vector() = default;
68
75 : data_(data_type::Zero(dim, MaxDigits)) {}
76
82 [[nodiscard]] auto dim() const -> index_type { return data_.rows(); }
83
89 [[nodiscard]] auto current_max_digits() const -> index_type {
91 }
92
98 [[nodiscard]] auto next_divided_dimension_index() const -> index_type {
100 }
101
108 [[nodiscard]] auto is_full() const -> bool {
111 }
112
119 [[nodiscard]] auto digits(index_type dim) const -> index_type {
122 return current_max_digits_;
123 }
124 return current_max_digits_ - 1;
125 }
126
134 [[nodiscard]] auto operator()(index_type dim, index_type digit) const
135 -> digit_type {
138 return data_.coeff(dim, digit);
139 }
140
148 [[nodiscard]] auto operator()(index_type dim, index_type digit)
149 -> digit_type& {
152 return data_.coeffRef(dim, digit);
153 }
154
163 auto push_back(digit_type digit) -> std::pair<index_type, index_type> {
167 "Tried to add a digit to a full adc_ternary_vector.");
168 }
170 }
175 digit;
176 const index_type divided_dimension_index =
180 return {divided_dimension_index, current_max_digits_ - 1};
181 }
182
189 [[nodiscard]] auto operator==(const adc_ternary_vector& right) const
190 -> bool {
191 return data_ == right.data_;
192 }
193
200 [[nodiscard]] auto operator!=(const adc_ternary_vector& right) const
201 -> bool {
202 return !operator==(right);
203 }
204
212 template <typename Scalar>
213 [[nodiscard]] auto elem_as(index_type dim) const -> Scalar {
215 auto num = static_cast<Scalar>(0);
216 auto coeff = static_cast<Scalar>(1);
217 static const Scalar inv_base =
218 static_cast<Scalar>(1) / static_cast<Scalar>(3);
219 for (index_type i = 0; i < current_max_digits_; ++i) {
220 num += coeff * static_cast<Scalar>(data_.coeff(dim, i));
221 coeff *= inv_base;
222 }
223 return num;
224 }
225
233 [[nodiscard]] auto as_variable(const VariableType& lower_bound,
234 const VariableType& width) const -> VariableType {
235 VariableType res(dim());
236 for (index_type i = 0; i < dim(); ++i) {
237 res(i) = lower_bound(i) +
239 }
240 return res;
241 }
242
248 [[nodiscard]] auto hash() const -> std::size_t {
249 std::size_t res = 0;
250 for (index_type i = 0; i < data_.rows(); ++i) {
251 for (index_type j = 0; j < data_.cols(); ++j) {
252 res += static_cast<std::size_t>(data_.coeff(i, j)) +
253 (res << 1U) + (res >> 2U);
254 }
255 }
256 return res;
257 }
258
259private:
262 MaxDigits, Eigen::RowMajor, max_dimensions_at_compile_time, MaxDigits>;
263
266
269
272};
273
274} // namespace num_collect::opt::impl
275
276namespace std {
277
286 num_collect::index_type MaxDigits>
287class hash<
288 num_collect::opt::impl::adc_ternary_vector<VariableType, MaxDigits>> {
289public:
293
295 using result_type = std::size_t;
296
303 [[nodiscard]] auto operator()(const argument_type& vec) const
304 -> result_type {
305 return vec.hash();
306 }
307};
308
309} // namespace std
Definition of assertion macros.
#define NUM_COLLECT_DEBUG_ASSERT(CONDITION)
Macro to check whether a condition is satisfied in debug build only.
Definition assert.h:75
Class of exception on not satisfying a precondition.
Definition exception.h:77
Class of vectors of ternary floating-point numbers in num_collect::opt::adaptive_diagonal_curves.
auto is_full() const -> bool
Check whether this vector is full.
auto elem_as(index_type dim) const -> Scalar
Get the element of this vector as a floating-point number.
auto operator()(index_type dim, index_type digit) const -> digit_type
Access a digit.
Eigen::Matrix< digit_type, dimensions_at_compile_time, MaxDigits, Eigen::RowMajor, max_dimensions_at_compile_time, MaxDigits > data_type
auto digits(index_type dim) const -> index_type
Get the number of digits in a dimension.
adc_ternary_vector(index_type dim)
Constructor.
auto next_divided_dimension_index() const -> index_type
Get the next dimension index to add a digit.
auto as_variable(const VariableType &lower_bound, const VariableType &width) const -> VariableType
Convert this vector to a variable.
auto operator!=(const adc_ternary_vector &right) const -> bool
Compare with another object.
auto operator==(const adc_ternary_vector &right) const -> bool
Compare with another object.
auto hash() const -> std::size_t
Calculate the hash of this vector.
auto current_max_digits() const -> index_type
Get the current maximum number of digits.
auto push_back(digit_type digit) -> std::pair< index_type, index_type >
Add a digit to a dimension specified by next_divided_dimension_index().
auto operator()(index_type dim, index_type digit) -> digit_type &
Access a digit.
num_collect::opt::impl::adc_ternary_vector< VariableType, MaxDigits > argument_type
Type of argument.
auto operator()(const argument_type &vec) const -> result_type
Calculate hash.
Concept of Eigen's dense vectors with real scalars.
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
#define NUM_COLLECT_LOG_AND_THROW(EXCEPTION_TYPE,...)
Write an error log and throw an exception for an error.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of internal implementations.
Definition adc_group.h:30
Namespace of num_collect source codes.
STL namespace.
Definition of real_scalar_dense_vector concept.