Loading...
Searching...
No Matches
StationaryChartKernel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
13
14#ifndef GPR_STATIONARYCHARTKERNEL_H_
15#define GPR_STATIONARYCHARTKERNEL_H_
16
17#include <Eigen/Dense>
18#include <cmath>
19#include <concepts>
20#include <cstddef>
21#include <memory>
22#include <stdexcept>
23#include <utility>
24#include <vector>
25
26#include "KernelOperator.h"
27
28namespace gpr {
29
31template <typename Radial>
32concept DifferentiableRadial = requires(const Radial& radial, double squared) {
33 {
34 radial(squared)
35 } -> std::convertible_to<double>;
36 {
37 radial.derivative(squared)
38 } -> std::convertible_to<double>;
39 {
40 radial.secondDerivative(squared)
41 } -> std::convertible_to<double>;
42};
43
48template <typename Radial>
51public:
53 std::shared_ptr<const geometry::CoordinateChart> chart,
54 double length_scale, Radial radial)
55 : chart_(std::move(chart)),
56 length_scale_(length_scale),
57 radial_(std::move(radial))
58 {
59 if (!chart_ || !chart_->valid()) {
60 throw std::invalid_argument(
61 "chart kernel requires a valid coordinate chart");
62 }
63 if (!(length_scale_ > 0.0) || !std::isfinite(length_scale_)) {
64 throw std::invalid_argument(
65 "chart kernel length scale must be positive");
66 }
67 }
68
69 [[nodiscard]] Eigen::MatrixXd covarianceBlock(
70 const Eigen::MatrixXd& left_positions,
71 const Eigen::MatrixXd& right_positions, Eigen::Index left_channel,
72 Eigen::Index right_channel) const override
73 {
74 const Eigen::Index dimensions = chart_->anchor().size();
75 requireChannel(left_channel, dimensions);
76 requireChannel(right_channel, dimensions);
77 const std::vector<ChartPoint> left = evaluate(left_positions);
78 const std::vector<ChartPoint> right = evaluate(right_positions);
79 Eigen::MatrixXd block(left_positions.rows(), right_positions.rows());
80 for (Eigen::Index row = 0; row < block.rows(); ++row) {
81 for (Eigen::Index col = 0; col < block.cols(); ++col) {
82 block(row, col) = entry(left[static_cast<std::size_t>(row)],
83 right[static_cast<std::size_t>(col)],
84 left_channel, right_channel);
85 }
86 }
87 return block;
88 }
89
90private:
91 struct ChartPoint {
92 Eigen::VectorXd coordinates;
93 Eigen::MatrixXd jacobian;
94 };
95
96 void requireChannel(Eigen::Index channel, Eigen::Index dimensions) const
97 {
98 if (channel < -1 || channel >= dimensions) {
99 throw std::invalid_argument(
100 "chart kernel channel is outside the geometry dimension");
101 }
102 }
103
104 [[nodiscard]] std::vector<ChartPoint> evaluate(
105 const Eigen::MatrixXd& positions) const
106 {
107 const Eigen::Index dimensions = chart_->anchor().size();
108 if (positions.cols() != dimensions || !positions.allFinite()) {
109 throw std::invalid_argument(
110 "chart kernel positions have invalid shape or values");
111 }
112 std::vector<ChartPoint> points;
113 points.reserve(static_cast<std::size_t>(positions.rows()));
114 for (Eigen::Index row = 0; row < positions.rows(); ++row) {
115 const Eigen::VectorXd cartesian = positions.row(row).transpose();
116 ChartPoint point{chart_->coordinates(cartesian),
117 chart_->jacobian(cartesian)};
118 if (point.coordinates.size() != chart_->rank() ||
119 point.jacobian.rows() != chart_->rank() ||
120 point.jacobian.cols() != dimensions ||
121 !point.coordinates.allFinite() || !point.jacobian.allFinite()) {
122 throw std::runtime_error(
123 "chart kernel coordinate evaluation is invalid");
124 }
125 points.push_back(std::move(point));
126 }
127 return points;
128 }
129
134 [[nodiscard]] double entry(const ChartPoint& left, const ChartPoint& right,
135 Eigen::Index left_channel,
136 Eigen::Index right_channel) const
137 {
138 const Eigen::VectorXd delta = left.coordinates - right.coordinates;
139 const double inverse_length_squared =
140 1.0 / (length_scale_ * length_scale_);
141 const double squared_distance =
142 inverse_length_squared * delta.squaredNorm();
143 const double value = radial_(squared_distance);
144 const double slope = radial_.derivative(squared_distance);
145 const double curvature = radial_.secondDerivative(squared_distance);
146 const double left_delta =
147 left_channel < 0 ? 0.0 : left.jacobian.col(left_channel).dot(delta);
148 const double right_delta =
149 right_channel < 0 ? 0.0
150 : right.jacobian.col(right_channel).dot(delta);
151 const double tangent =
152 left_channel < 0 || right_channel < 0
153 ? 0.0
154 : left.jacobian.col(left_channel)
155 .dot(right.jacobian.col(right_channel));
156 if (left_channel < 0 && right_channel < 0) {
157 return value;
158 }
159 if (left_channel < 0) {
160 return -2.0 * inverse_length_squared * right_delta * slope;
161 }
162 if (right_channel < 0) {
163 return 2.0 * inverse_length_squared * left_delta * slope;
164 }
165 const double scale = inverse_length_squared;
166 return -4.0 * scale * scale * left_delta * right_delta * curvature -
167 2.0 * scale * tangent * slope;
168 }
169
170 std::shared_ptr<const geometry::CoordinateChart> chart_;
171 double length_scale_;
172 Radial radial_;
173};
174
175} // namespace gpr
176
177#endif // GPR_STATIONARYCHARTKERNEL_H_
StationaryChartKernel(std::shared_ptr< const geometry::CoordinateChart > chart, double length_scale, Radial radial)
Eigen::MatrixXd covarianceBlock(const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions, Eigen::Index left_channel, Eigen::Index right_channel) const override
Covariance between one channel at each set of geometry rows.
Value and both derivatives with respect to one squared distance.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
STL namespace.