numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
adc_rectangle.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 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 <cmath>
23#include <cstdint>
24#include <utility>
25
29
30namespace num_collect::opt::impl {
31
39template <base::concepts::real_scalar Value, typename TernaryVector>
41public:
43 using value_type = Value;
44
46 using ternary_vector_type = TernaryVector;
47
57
63 [[nodiscard]] auto vertex() const -> const ternary_vector_type& {
64 return vertex_;
65 }
66
72 [[nodiscard]] auto ave_value() const -> const value_type& {
73 return ave_value_;
74 }
75
81 [[nodiscard]] auto sample_points() const
84 }
85
91 [[nodiscard]] auto dist() const -> value_type {
92 auto squared_sum = static_cast<value_type>(0);
93 for (index_type i = 0; i < vertex_.dim(); ++i) {
94 using std::pow;
95 squared_sum +=
96 pow(static_cast<value_type>(3), -2 * (vertex_.digits(i) - 1));
97 }
98 using std::sqrt;
99 const auto half = static_cast<value_type>(0.5);
100 return half * sqrt(squared_sum);
101 }
102
109 [[nodiscard]] static auto determine_sample_points(
110 const ternary_vector_type& lowest_vertex)
111 -> std::pair<ternary_vector_type, ternary_vector_type> {
112 auto res = std::make_pair(lowest_vertex, lowest_vertex);
113 const auto dim = lowest_vertex.dim();
114 for (index_type i = 0; i < dim; ++i) {
115 const auto digits = lowest_vertex.digits(i);
116 NUM_COLLECT_DEBUG_ASSERT(digits > 0);
117 std::uint_fast32_t one_count = 0;
118 for (index_type j = 0; j < digits; ++j) {
119 if (lowest_vertex(i, j) ==
120 typename ternary_vector_type::digit_type{1}) {
121 ++one_count;
122 }
123 }
124
125 auto last_digit = // NOLINTNEXTLINE: false positive
126 static_cast<std::int_fast32_t>(lowest_vertex(i, digits - 1));
127 ++last_digit;
128 constexpr std::uint_fast32_t odd_mask = 1;
129 if ((one_count & odd_mask) == odd_mask) {
130 res.first(i, digits - 1) =
131 static_cast<typename ternary_vector_type::digit_type>(
132 last_digit);
133 } else {
134 res.second(i, digits - 1) =
135 static_cast<typename ternary_vector_type::digit_type>(
136 last_digit);
137 }
138 }
139 normalize_point(res.first);
140 normalize_point(res.second);
141 return res;
142 }
143
144private:
151 for (index_type i = 0; i < point.dim(); ++i) {
152 for (index_type j = point.digits(i) - 1; j > 0; --j) {
153 if (point(i, j) ==
154 typename ternary_vector_type::digit_type{3}) {
155 point(i, j) = 0;
156 std::int_fast32_t temp =
157 point(i, j - 1); // NOLINT: false positive
158 ++temp;
159 point(i, j - 1) =
160 static_cast<typename ternary_vector_type::digit_type>(
161 temp);
162 }
163 }
164 }
165 }
166
169
172};
173
174} // namespace num_collect::opt::impl
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
static void normalize_point(ternary_vector_type &point)
Normalize point.
static auto determine_sample_points(const ternary_vector_type &lowest_vertex) -> std::pair< ternary_vector_type, ternary_vector_type >
Determine sampling points.
auto dist() const -> value_type
Get the distance between center point and vertex.
adc_rectangle(const ternary_vector_type &vertex, const value_type &ave_value)
Constructor.
auto sample_points() const -> std::pair< ternary_vector_type, ternary_vector_type >
Determine sampling points.
Definition of index_type type.
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
STL namespace.
Definition of real_scalar concept.