numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
compute_kernel_matrix.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/SparseCore>
27
31#include "num_collect/constants/zero.h" // IWYU pragma: keep
38
40
55template <concepts::distance_function DistanceFunction, concepts::rbf RBF,
56 concepts::length_parameter_calculator LengthParameterCalculator,
58 requires std::is_same_v<
59 typename LengthParameterCalculator::distance_function_type,
60 DistanceFunction> &&
61 std::is_same_v<typename DistanceFunction::value_type,
62 typename RBF::scalar_type> &&
63 std::is_same_v<typename DistanceFunction::value_type,
64 typename KernelMatrix::Scalar> &&
65 LengthParameterCalculator::uses_global_length_parameter
66inline void compute_kernel_matrix(const DistanceFunction& distance_function,
67 const RBF& rbf, LengthParameterCalculator& length_parameter_calculator,
68 const std::vector<typename DistanceFunction::variable_type>& variables,
69 KernelMatrix& kernel_matrix) {
70 using scalar_type = typename KernelMatrix::Scalar;
71
72 length_parameter_calculator.compute(variables, distance_function);
73
74 const scalar_type length_parameter =
75 length_parameter_calculator.length_parameter_at(
76 static_cast<index_type>(0));
77 NUM_COLLECT_ASSERT(length_parameter > static_cast<scalar_type>(0));
78
79 const scalar_type diagonal_coeff = rbf(constants::zero<scalar_type>);
80
81 const std::size_t num_variables = variables.size();
82 kernel_matrix.resize(static_cast<index_type>(num_variables),
83 static_cast<index_type>(num_variables));
84
85 for (std::size_t i = 0; i < num_variables; ++i) {
86 kernel_matrix(static_cast<index_type>(i), static_cast<index_type>(i)) =
87 diagonal_coeff;
88 for (std::size_t j = i + 1; j < num_variables; ++j) {
89 const scalar_type value =
90 rbf(distance_function(variables[i], variables[j]) /
91 length_parameter);
92 kernel_matrix(
93 static_cast<index_type>(i), static_cast<index_type>(j)) = value;
94 kernel_matrix(
95 static_cast<index_type>(j), static_cast<index_type>(i)) = value;
96 }
97 }
98}
99
114template <concepts::distance_function DistanceFunction, concepts::rbf RBF,
115 concepts::length_parameter_calculator LengthParameterCalculator,
116 base::concepts::dense_matrix KernelMatrix>
117 requires std::is_same_v<
118 typename LengthParameterCalculator::distance_function_type,
119 DistanceFunction> &&
120 std::is_same_v<typename DistanceFunction::value_type,
121 typename RBF::scalar_type> &&
122 std::is_same_v<typename DistanceFunction::value_type,
123 typename KernelMatrix::Scalar> &&
124 (!LengthParameterCalculator::uses_global_length_parameter)
125inline void compute_kernel_matrix(const DistanceFunction& distance_function,
126 const RBF& rbf, LengthParameterCalculator& length_parameter_calculator,
127 const std::vector<typename DistanceFunction::variable_type>& variables,
128 KernelMatrix& kernel_matrix) {
129 using scalar_type = typename KernelMatrix::Scalar;
130
131 length_parameter_calculator.compute(variables, distance_function);
132
133 const std::size_t num_variables = variables.size();
134 kernel_matrix.resize(static_cast<index_type>(num_variables),
135 static_cast<index_type>(num_variables));
136
137 for (std::size_t j = 0; j < num_variables; ++j) {
138 const scalar_type length_parameter =
139 length_parameter_calculator.length_parameter_at(
140 static_cast<index_type>(j));
141 NUM_COLLECT_ASSERT(length_parameter > static_cast<scalar_type>(0));
142
143 for (std::size_t i = 0; i < num_variables; ++i) {
144 const scalar_type value =
145 rbf(distance_function(variables[i], variables[j]) /
146 length_parameter);
147 kernel_matrix(
148 static_cast<index_type>(i), static_cast<index_type>(j)) = value;
149 }
150 }
151}
152
167template <concepts::distance_function DistanceFunction, concepts::csrbf RBF,
168 concepts::length_parameter_calculator LengthParameterCalculator,
170 KernelMatrix>
171 requires std::is_same_v<
172 typename LengthParameterCalculator::distance_function_type,
173 DistanceFunction> &&
174 std::is_same_v<typename DistanceFunction::value_type,
175 typename RBF::scalar_type> &&
176 std::is_same_v<typename DistanceFunction::value_type,
177 typename KernelMatrix::Scalar>
178inline void compute_kernel_matrix(const DistanceFunction& distance_function,
179 const RBF& rbf, LengthParameterCalculator& length_parameter_calculator,
180 const std::vector<typename DistanceFunction::variable_type>& variables,
181 KernelMatrix& kernel_matrix) {
182 using scalar_type = typename KernelMatrix::Scalar;
183
184 length_parameter_calculator.compute(variables, distance_function);
185
186 const auto num_variables = util::safe_cast<int>(variables.size());
187 kernel_matrix.resize(num_variables, num_variables);
188
189 const scalar_type support_boundary = RBF::support_boundary();
190
191 std::vector<
192 Eigen::Triplet<scalar_type, typename KernelMatrix::StorageIndex>>
193 triplets;
194 for (int j = 0; j < num_variables; ++j) {
195 const scalar_type length_parameter =
196 length_parameter_calculator.length_parameter_at(j);
197 NUM_COLLECT_ASSERT(length_parameter > static_cast<scalar_type>(0));
198
199 for (int i = 0; i < num_variables; ++i) {
200 const scalar_type distance_rate =
201 distance_function(variables[static_cast<std::size_t>(i)],
202 variables[static_cast<std::size_t>(j)]) /
203 length_parameter;
204 if (distance_rate >= support_boundary) {
205 continue;
206 }
207 const scalar_type value = rbf(distance_rate);
208 triplets.emplace_back(i, j, value);
209 }
210 }
211 kernel_matrix.setFromTriplets(triplets.begin(), triplets.end());
212}
213
214} // namespace num_collect::rbf
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
Concept of Eigen's dense matrices.
Concept of Eigen's sparse matrices with scalars of the given type.
Concept of RBFs.
Definition rbf.h:33
Definition of csrbf concept.
Definition of dense_matrix concept.
Definition of distance_function concept.
Definition of index_type type.
Definition of length_parameter_calculator concept.
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 RBF interpolation.
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.
auto safe_cast(const From &value) -> To
Cast safely.
Definition safe_cast.h:54
Definition of rbf concept.
Definition of safe_cast function.
Definition of sparse_matrix_of concept.
Definition of zero.