numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
rbf_interpolator.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 <type_traits> // IWYU pragma: keep
24#include <vector>
25
26#include <Eigen/Core>
27
48
49namespace num_collect::rbf {
50
52constexpr auto rbf_interpolator_tag =
53 logging::log_tag_view("num_collect::rbf::rbf_interpolator");
54
66template <typename FunctionSignature,
67 concepts::rbf RBF =
70 concepts::distance_function DistanceFunction =
73 concepts::length_parameter_calculator LengthParameterCalculator =
75 DistanceFunction>>
77
89template <typename Variable, typename FunctionValue, concepts::rbf RBF,
90 kernel_matrix_type KernelMatrixType,
91 concepts::distance_function DistanceFunction,
92 concepts::length_parameter_calculator LengthParameterCalculator>
93 requires std::is_same_v<Variable,
94 typename DistanceFunction::variable_type> &&
95 std::is_same_v<typename DistanceFunction::value_type,
96 typename RBF::scalar_type> &&
97 std::is_same_v<DistanceFunction,
98 typename LengthParameterCalculator::distance_function_type>
99class rbf_interpolator<FunctionValue(Variable), RBF, KernelMatrixType,
100 DistanceFunction, LengthParameterCalculator>
101 : public logging::logging_mixin {
102public:
104 using variable_type = Variable;
105
107 using function_value_type = FunctionValue;
108
110 using rbf_type = RBF;
111
113 using distance_function_type = DistanceFunction;
114
116 using length_parameter_calculator_type = LengthParameterCalculator;
117
119 static constexpr bool uses_global_length_parameter =
120 length_parameter_calculator_type::uses_global_length_parameter;
121
123 using kernel_value_type = typename DistanceFunction::value_type;
124
128 KernelMatrixType, uses_global_length_parameter>;
129
132 typename kernel_matrix_solver_type::kernel_matrix_type;
133
135 using function_value_vector_type = Eigen::VectorX<function_value_type>;
136
138 static constexpr index_type default_max_mle_evaluations = 20;
139
148 rbf_type rbf = rbf_type())
149 : logging::logging_mixin(rbf_interpolator_tag),
150 distance_function_(std::move(distance_function)),
151 rbf_(std::move(rbf)) {}
152
162 void compute(const std::vector<variable_type>& variables,
163 const function_value_vector_type& function_values) {
164 compute_kernel_matrix(distance_function_, rbf_,
165 length_parameter_calculator_, variables, kernel_matrix_);
166 kernel_matrix_solver_.compute(kernel_matrix_, function_values);
167 kernel_matrix_solver_.solve(coeffs_, reg_param, function_values);
168 variables_ = &variables;
169 }
170
177 [[nodiscard]] auto interpolate(const variable_type& variable) const
179 auto value = static_cast<function_value_type>(0);
180 for (std::size_t i = 0; i < variables_->size(); ++i) {
181 const kernel_value_type distance_rate =
182 distance_function_(variable, (*variables_)[i]) /
183 length_parameter_calculator_.length_parameter_at(
184 static_cast<index_type>(i));
185 if constexpr (concepts::csrbf<rbf_type>) {
186 if (distance_rate < rbf_type::support_boundary()) {
187 value += coeffs_(static_cast<index_type>(i)) *
188 rbf_(distance_rate);
189 }
190 } else {
191 value +=
192 coeffs_(static_cast<index_type>(i)) * rbf_(distance_rate);
193 }
194 }
195 return value;
196 }
197
204 length_parameter_calculator_.scale(value);
205 }
206
220 const std::vector<variable_type>& variables,
221 const function_value_vector_type& function_values,
222 index_type max_mle_evaluations = default_max_mle_evaluations)
223 requires uses_global_length_parameter
224 {
225 static constexpr auto base = static_cast<kernel_value_type>(10);
226
227 auto objective_function =
228 [this, &variables, &function_values](
230 const kernel_value_type scale = std::pow(base, log_scale);
231 this->length_parameter_calculator_.scale(scale);
232 compute_kernel_matrix(this->distance_function_, this->rbf_,
233 this->length_parameter_calculator_, variables,
234 this->kernel_matrix_);
235 this->kernel_matrix_solver_.compute(
236 this->kernel_matrix_, function_values);
237 return std::log10(
238 this->kernel_matrix_solver_.calc_mle_objective(reg_param));
239 };
240
241 using objective_function_object_type = decltype(objective_function);
242 using objective_function_wrapper_type =
244 objective_function_object_type>;
245 using optimizer_type =
247
248 optimizer_type optimizer{
249 objective_function_wrapper_type{objective_function}};
250 configure_child_algorithm_logger_if_exists(optimizer);
251 constexpr auto lower_boundary = static_cast<kernel_value_type>(-1);
252 constexpr auto upper_boundary = static_cast<kernel_value_type>(2);
253 optimizer.max_evaluations(max_mle_evaluations);
254 optimizer.init(lower_boundary, upper_boundary);
255 optimizer.solve();
256
257 const kernel_value_type log_scale = optimizer.opt_variable();
258 const kernel_value_type scale = std::pow(base, log_scale);
259 NUM_COLLECT_LOG_DEBUG(this->logger(),
260 "Selected an optimized scale of length parameters: {}", scale);
261 this->length_parameter_calculator_.scale(scale);
262 }
263
269 [[nodiscard]] auto distance_function() const noexcept
270 -> const distance_function_type& {
271 return distance_function_;
272 }
273
279 [[nodiscard]] auto rbf() const noexcept -> const rbf_type& { return rbf_; }
280
286 [[nodiscard]] auto length_parameter_calculator() const noexcept
288 return length_parameter_calculator_;
289 }
290
296 [[nodiscard]] auto coeffs() const noexcept
298 return coeffs_;
299 }
300
301protected:
307 [[nodiscard]] auto variables() const noexcept
308 -> const std::vector<variable_type>& {
309 return *variables_;
310 }
311
317 [[nodiscard]] auto kernel_matrix_solver() const noexcept
318 -> const kernel_matrix_solver_type& {
319 return kernel_matrix_solver_;
320 }
321
322private:
324 static constexpr auto reg_param = static_cast<kernel_value_type>(0);
325
326 // TODO Implementation of regularization.
327
330
333
335 length_parameter_calculator_type length_parameter_calculator_{};
336
338 kernel_matrix_type kernel_matrix_{};
339
341 const std::vector<variable_type>* variables_{nullptr};
342
344 kernel_matrix_solver_type kernel_matrix_solver_{};
345
348};
349
360template <typename FunctionSignature,
361 concepts::rbf RBF =
362 rbfs::gaussian_rbf<impl::get_default_scalar_type<FunctionSignature>>,
364 concepts::distance_function DistanceFunction =
365 distance_functions::euclidean_distance_function<
368 rbf_interpolator<FunctionSignature, RBF, KernelMatrixType, DistanceFunction,
370 DistanceFunction>>;
371
381template <typename FunctionSignature,
382 concepts::rbf RBF =
385 concepts::distance_function DistanceFunction =
389 rbf_interpolator<FunctionSignature, RBF, KernelMatrixType, DistanceFunction,
391 DistanceFunction>>;
392
402template <typename FunctionSignature,
405 concepts::distance_function DistanceFunction =
408using local_csrbf_interpolator = rbf_interpolator<FunctionSignature, RBF,
409 kernel_matrix_type::sparse, DistanceFunction,
411 DistanceFunction>>;
412
413} // namespace num_collect::rbf
Class of tags of logs without memory management.
Class to incorporate logging in algorithms.
Class of dividing rectangles (DIRECT) method jones1993 for optimization.
Wrapper class of a function object to use as an objective function.
Class to calculate length parameters for RBF using global fixed length parameter.
Class to calculate length parameters for RBF using length parameters localized for each sample point.
auto coeffs() const noexcept -> const function_value_vector_type &
Get the coefficients for samples points.
rbf_interpolator(distance_function_type distance_function=distance_function_type(), rbf_type rbf=rbf_type())
Constructor.
void compute(const std::vector< variable_type > &variables, const function_value_vector_type &function_values)
Compute parameters for interpolation.
auto interpolate(const variable_type &variable) const -> function_value_type
Interpolate for a variable.
auto length_parameter_calculator() const noexcept -> const length_parameter_calculator_type &
Get the calculator of length parameters.
void optimize_length_parameter_scale(const std::vector< variable_type > &variables, const function_value_vector_type &function_values, index_type max_mle_evaluations=default_max_mle_evaluations)
Set the scale of length parameters with optimization using MLE scheuerer2011.
auto kernel_matrix_solver() const noexcept -> const kernel_matrix_solver_type &
Get the solver of the linear equation of kernel matrix.
Class to interpolate using RBF.
Class of Gaussian RBF fornberg2015.
Class of Wendland's Compactly Supported RBF wendland1995.
Definition of compute_kernel_matrix function.
Concept of CSRBFs (compactly supported RBFs).
Definition csrbf.h:33
Concept of RBFs.
Definition rbf.h:33
Definition of csrbf concept.
Definition of distance_function concept.
Definition of dividing_rectangles class.
Definition of euclidean_distance_function class.
Definition of function_object_wrapper class.
Definition of gaussian_rbf class.
Definition of get_default_scalar_type type.
Definition of get_variable_type class.
Definition of global_length_parameter_calculator class.
Definition of index_type type.
Definition of kernel_matrix_solver class.
Definition of kernel_matrix_type enumeration.
Definition of length_parameter_calculator concept.
Definition of local_length_parameter_calculator class.
Definition of log_tag_view class.
Definition of macros for logging.
#define NUM_COLLECT_LOG_DEBUG(LOGGER,...)
Write a debug log.
Definition of logging_mixin class.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
typename get_variable_type< FunctionSignature >::type get_variable_type_t
Get the type of variables from function signature.
typename distance_functions::euclidean_distance_function< impl::get_variable_type_t< FunctionSignature > >::value_type get_default_scalar_type
Get the default type of scalars from function signature.
Namespace of RBF interpolation.
kernel_matrix_type
Enumeration of types of kernel matrices.
constexpr auto rbf_interpolator_tag
Tag of rbf_interpolator.
void compute_kernel_matrix(const DistanceFunction &distance_function, const RBF &rbf, LengthParameterCalculator &length_parameter_calculator, const std::vector< typename DistanceFunction::variable_type > &variables, KernelMatrix &kernel_matrix)
Compute a kernel matrix.
STL namespace.
Definition of rbf concept.
Definition of wendland_csrbf class.