numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
local_length_parameter_calculator.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 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 <limits>
24#include <vector>
25
26#include <Eigen/Core>
27
31#include "num_collect/constants/zero.h" // IWYU pragma: keep
35
37
44template <concepts::distance_function DistanceFunction>
46public:
48 using distance_function_type = DistanceFunction;
49
51 using variable_type = typename DistanceFunction::variable_type;
52
54 using scalar_type = typename DistanceFunction::value_type;
55
57 static constexpr bool uses_global_length_parameter = false;
58
63
70 void compute(const std::vector<variable_type>& variables,
71 const distance_function_type& distance_function) {
72 const std::size_t num_samples = variables.size();
74 num_samples > 0, "Sample points must be given.");
75
76 length_parameters_.resize(static_cast<index_type>(num_samples));
77 for (std::size_t i = 0; i < num_samples; ++i) {
78 auto min_distance = std::numeric_limits<scalar_type>::max();
79 for (std::size_t j = 0; j < num_samples; ++j) {
80 if (i != j) {
81 const auto distance =
82 distance_function(variables[i], variables[j]);
83 if (distance < min_distance) {
84 min_distance = distance;
85 }
86 }
87 }
88 length_parameters_(static_cast<index_type>(i)) =
89 scale_ * min_distance;
90 }
91 }
92
99 [[nodiscard]] auto length_parameter_at(index_type i) const -> scalar_type {
102 return length_parameters_(i);
103 }
104
110 [[nodiscard]] auto scale() const noexcept -> scalar_type { return scale_; }
111
117 void scale(scalar_type value) {
119 "Scale of length parameters must be a positive number.");
120 scale_ = value;
121 }
122
123private:
125 static constexpr auto default_scale = static_cast<scalar_type>(10);
126
129
131 Eigen::VectorX<scalar_type> length_parameters_{};
132};
133
134} // namespace num_collect::rbf::length_parameter_calculators
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 to calculate length parameters for RBF using length parameters localized for each sample point.
auto length_parameter_at(index_type i) const -> scalar_type
Get the length parameter at a point.
auto scale() const noexcept -> scalar_type
Get the current scale of length parameters.
void compute(const std::vector< variable_type > &variables, const distance_function_type &distance_function)
Compute the length parameters.
static constexpr auto default_scale
Default value of the scalar of length parameters.
static constexpr bool uses_global_length_parameter
Whether this calculator uses the globally fixed length parameters.
Definition of distance_function concept.
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr T zero
Value 0.
Definition zero.h:30
Namespace of calculators of length parameters.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of zero.