numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
rbf_polynomial_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 <vector>
24
25#include <Eigen/Core>
26
50
51namespace num_collect::rbf {
52
55 logging::log_tag_view("num_collect::rbf::rbf_polynomial_interpolator");
56
69template <typename FunctionSignature,
70 concepts::rbf RBF =
72 index_type PolynomialDegree = 1,
74 concepts::distance_function DistanceFunction =
77 concepts::length_parameter_calculator LengthParameterCalculator =
79 DistanceFunction>>
81
94template <typename Variable, typename FunctionValue, concepts::rbf RBF,
95 index_type PolynomialDegree, kernel_matrix_type KernelMatrixType,
96 concepts::distance_function DistanceFunction,
97 concepts::length_parameter_calculator LengthParameterCalculator>
98class rbf_polynomial_interpolator<FunctionValue(Variable), RBF,
99 PolynomialDegree, KernelMatrixType, DistanceFunction,
100 LengthParameterCalculator> : public logging::logging_mixin {
101public:
103 using variable_type = Variable;
104
106 using function_value_type = FunctionValue;
107
109 using rbf_type = RBF;
110
112 using distance_function_type = DistanceFunction;
113
115 using length_parameter_calculator_type = LengthParameterCalculator;
116
118 static constexpr bool uses_global_length_parameter =
119 length_parameter_calculator_type::uses_global_length_parameter;
120
122 using kernel_value_type = typename DistanceFunction::value_type;
123
127 function_value_type, KernelMatrixType,
128 uses_global_length_parameter>;
129
132 typename equation_solver_type::kernel_matrix_type;
133
136 typename equation_solver_type::additional_matrix_type;
137
139 using function_value_vector_type = Eigen::VectorX<function_value_type>;
140
142 static constexpr index_type default_max_mle_evaluations = 20;
143
152 rbf_type rbf = rbf_type())
153 : logging::logging_mixin(rbf_polynomial_interpolator_tag),
154 distance_function_(std::move(distance_function)),
155 rbf_(std::move(rbf)) {}
156
166 void compute(const std::vector<variable_type>& variables,
167 const function_value_vector_type& function_values) {
168 const auto num_variables = static_cast<index_type>(variables.size());
170 num_variables > 0, this->logger(), "Variables must be given.");
171 const index_type num_dimensions = base::get_size(variables.front());
172 polynomial_calculator_.prepare(num_dimensions);
173
174 compute_kernel_matrix(distance_function_, rbf_,
175 length_parameter_calculator_, variables, kernel_matrix_);
176 polynomial_calculator_.compute_polynomial_term_matrix(
177 variables, polynomial_matrix_);
178 equation_solver_.compute(
179 kernel_matrix_, polynomial_matrix_, function_values);
180 equation_solver_.solve(kernel_coeffs_, polynomial_coeffs_, reg_param);
181 variables_ = &variables;
182 }
183
190 [[nodiscard]] auto interpolate(const variable_type& variable) const
192 auto value = static_cast<function_value_type>(0);
193
194 for (std::size_t i = 0; i < variables_->size(); ++i) {
195 const kernel_value_type distance_rate =
196 distance_function_(variable, (*variables_)[i]) /
197 length_parameter_calculator_.length_parameter_at(
198 static_cast<index_type>(i));
199 if constexpr (concepts::csrbf<rbf_type>) {
200 if (distance_rate < rbf_type::support_boundary()) {
201 value += kernel_coeffs_(static_cast<index_type>(i)) *
202 rbf_(distance_rate);
203 }
204 } else {
205 value += kernel_coeffs_(static_cast<index_type>(i)) *
206 rbf_(distance_rate);
207 }
208 }
209
210 value += polynomial_calculator_.evaluate_polynomial_for_variable(
211 variable, polynomial_coeffs_);
212
213 return value;
214 }
215
222 length_parameter_calculator_.scale(value);
223 }
224
238 const std::vector<variable_type>& variables,
239 const function_value_vector_type& function_values,
240 index_type max_mle_evaluations = default_max_mle_evaluations)
241 requires uses_global_length_parameter
242 {
243 const auto num_variables = static_cast<index_type>(variables.size());
245 num_variables > 0, this->logger(), "Variables must be given.");
246 const index_type num_dimensions = base::get_size(variables.front());
247 polynomial_calculator_.prepare(num_dimensions);
248
249 static constexpr auto base = static_cast<kernel_value_type>(10);
250 auto objective_function =
251 [this, &variables, &function_values](
253 const kernel_value_type scale = std::pow(base, log_scale);
254 length_parameter_calculator_.scale(scale);
255 compute_kernel_matrix(distance_function_, rbf_,
256 length_parameter_calculator_, variables, kernel_matrix_);
257 polynomial_calculator_.compute_polynomial_term_matrix(
258 variables, polynomial_matrix_);
259 equation_solver_.compute(
260 kernel_matrix_, polynomial_matrix_, function_values);
261 return std::log10(equation_solver_.calc_mle_objective(reg_param));
262 };
263
264 using objective_function_object_type = decltype(objective_function);
265 using objective_function_wrapper_type =
267 objective_function_object_type>;
268 using optimizer_type =
270
271 optimizer_type optimizer{
272 objective_function_wrapper_type{objective_function}};
273 configure_child_algorithm_logger_if_exists(optimizer);
274 constexpr auto lower_boundary = static_cast<kernel_value_type>(-1);
275 constexpr auto upper_boundary = static_cast<kernel_value_type>(2);
276 optimizer.max_evaluations(max_mle_evaluations);
277 optimizer.init(lower_boundary, upper_boundary);
278 optimizer.solve();
279
280 const kernel_value_type log_scale = optimizer.opt_variable();
281 const kernel_value_type scale = std::pow(base, log_scale);
282 NUM_COLLECT_LOG_DEBUG(this->logger(),
283 "Selected an optimized scale of length parameters: {}", scale);
284 this->length_parameter_calculator_.scale(scale);
285 }
286
292 [[nodiscard]] auto distance_function() const noexcept
293 -> const distance_function_type& {
294 return distance_function_;
295 }
296
302 [[nodiscard]] auto rbf() const noexcept -> const rbf_type& { return rbf_; }
303
309 [[nodiscard]] auto length_parameter_calculator() const noexcept
311 return length_parameter_calculator_;
312 }
313
319 [[nodiscard]] auto kernel_coeffs() const noexcept
321 return kernel_coeffs_;
322 }
323
329 [[nodiscard]] auto polynomial_coeffs() const noexcept
331 return polynomial_coeffs_;
332 }
333
334private:
336 static constexpr auto reg_param = static_cast<kernel_value_type>(0);
337
338 // TODO Implementation of regularization.
339
342
345
347 length_parameter_calculator_type length_parameter_calculator_{};
348
351 polynomial_calculator_{};
352
354 kernel_matrix_type kernel_matrix_{};
355
357 polynomial_matrix_type polynomial_matrix_{};
358
360 const std::vector<variable_type>* variables_{nullptr};
361
363 equation_solver_type equation_solver_{};
364
367
369 function_value_vector_type polynomial_coeffs_{};
370};
371
383template <typename FunctionSignature,
384 concepts::rbf RBF =
385 rbfs::gaussian_rbf<impl::get_default_scalar_type<FunctionSignature>>,
386 index_type PolynomialDegree = 1,
388 concepts::distance_function DistanceFunction =
389 distance_functions::euclidean_distance_function<
392 rbf_polynomial_interpolator<FunctionSignature, RBF, PolynomialDegree,
393 KernelMatrixType, DistanceFunction,
395 DistanceFunction>>;
396
397} // 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.
Class to calculate polynomials used with RBF interpolation.
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.
void compute(const std::vector< variable_type > &variables, const function_value_vector_type &function_values)
Compute parameters for interpolation.
Class to interpolate using RBF and polynomials.
Class of Gaussian RBF fornberg2015.
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 exceptions.
Definition of function_object_wrapper class.
Definition of gaussian_rbf class.
Definition of general_spline_equation_solver class.
Definition of get_default_scalar_type type.
Definition of get_size class.
Definition of get_variable_type class.
Definition of global_length_parameter_calculator class.
Definition of index_type type.
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
auto get_size(const Matrix &matrix) -> index_type
Get the size.
Definition get_size.h:39
typename get_variable_type< FunctionSignature >::type get_variable_type_t
Get the type of variables from function signature.
Namespace of RBF interpolation.
constexpr auto rbf_polynomial_interpolator_tag
Tag of rbf_polynomial_interpolator.
kernel_matrix_type
Enumeration of types of kernel matrices.
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 polynomial_calculator class.
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 rbf concept.