Loading...
Searching...
No Matches
gpr::ski Namespace Reference

Classes

struct  InterpolationWeights
class  UniformGrid
class  DerivativeSKI
struct  DSkipReport

Functions

double cubicConvolutionWeight (double s) noexcept
double cubicConvolutionWeightDerivative (double s) noexcept
InterpolationWeights interpolate (const UniformGrid &grid, const Eigen::VectorXd &x)
Eigen::MatrixXd lanczosLowRank (const Eigen::MatrixXd &A, double tol, Eigen::Index max_rank)
DSkipReport dskipProduct (const Eigen::MatrixXd &x, const Eigen::VectorXd &lengthscales, double tol, Eigen::Index max_rank)

Function Documentation

◆ cubicConvolutionWeight()

double gpr::ski::cubicConvolutionWeight ( double s)
nodiscardnoexcept

Keys' cubic convolution weight and its derivative, the interpolation D-SKI differentiates. w has support abs(s) < 2 and reproduces cubics exactly.

Definition at line 27 of file DerivativeSKI.cpp.

28{
29 const double a = std::fabs(s);
30 if (a < 1.0) {
31 return ((kKeysA + 2.0) * a - (kKeysA + 3.0)) * a * a + 1.0;
32 }
33 if (a < 2.0) {
34 return ((kKeysA * a - 5.0 * kKeysA) * a + 8.0 * kKeysA) * a -
35 4.0 * kKeysA;
36 }
37 return 0.0;
38}

◆ cubicConvolutionWeightDerivative()

double gpr::ski::cubicConvolutionWeightDerivative ( double s)
nodiscardnoexcept

Definition at line 40 of file DerivativeSKI.cpp.

41{
42 const double a = std::fabs(s);
43 // d|s|/ds is the sign; at s == 0 the weight is smooth with zero slope.
44 const double sign = (s > 0.0) ? 1.0 : ((s < 0.0) ? -1.0 : 0.0);
45 if (a < 1.0) {
46 return sign * (3.0 * (kKeysA + 2.0) * a * a - 2.0 * (kKeysA + 3.0) * a);
47 }
48 if (a < 2.0) {
49 return sign * (3.0 * kKeysA * a * a - 10.0 * kKeysA * a + 8.0 * kKeysA);
50 }
51 return 0.0;
52}

◆ dskipProduct()

DSkipReport gpr::ski::dskipProduct ( const Eigen::MatrixXd & x,
const Eigen::VectorXd & lengthscales,
double tol,
Eigen::Index max_rank )
nodiscard

Run the D-SKIP product over per-dimension SE factors and report whether the rank stayed bounded. x is n x d; one lengthscale per column.

Definition at line 230 of file DerivativeSKI.cpp.

233{
234 DSkipReport report;
235 const Eigen::Index n = x.rows();
236 const Eigen::Index d = x.cols();
237 if (n == 0 || d == 0 || lengthscales.size() != d) return report;
238
239 // Start from the first dimension's factor, then fold in one dimension at a
240 // time. The ARD product is elementwise (Hadamard) in the kernel matrices.
241 Eigen::MatrixXd running = Eigen::MatrixXd::Ones(n, n);
242 Eigen::MatrixXd exact = Eigen::MatrixXd::Ones(n, n);
243 for (Eigen::Index dim = 0; dim < d; ++dim) {
244 Eigen::MatrixXd factor(n, n);
245 for (Eigen::Index i = 0; i < n; ++i) {
246 for (Eigen::Index j = 0; j < n; ++j) {
247 factor(i, j) =
248 seKernel(x(i, dim) - x(j, dim), lengthscales(dim));
249 }
250 }
251 exact = exact.cwiseProduct(factor);
252 running = running.cwiseProduct(factor);
253 // Compress the running product, which is what D-SKIP does after each
254 // multiply to stop the rank compounding.
255 const Eigen::MatrixXd F = lanczosLowRank(running, tol, max_rank);
256 report.rank_after_product.push_back(F.cols());
257 running = F * F.transpose();
258 }
259
260 const double denom = exact.norm();
261 report.relative_error =
262 (denom > 0.0) ? (running - exact).norm() / denom : 0.0;
263 const Eigen::MatrixXd Fe = lanczosLowRank(exact, tol, 0);
264 report.exact_rank = Fe.cols();
265 report.rank_bounded = !report.rank_after_product.empty() &&
266 report.rank_after_product.back() < n;
267 return report;
268}
@ x
Definition Enums.h:28
Eigen::MatrixXd lanczosLowRank(const Eigen::MatrixXd &A, double tol, Eigen::Index max_rank)
double relative_error
against the exact product
Eigen::Index exact_rank
numerical rank of the exact matrix
bool rank_bounded
did the rank stay below the matrix size
std::vector< Eigen::Index > rank_after_product
one entry per dimension

◆ interpolate()

InterpolationWeights gpr::ski::interpolate ( const UniformGrid & grid,
const Eigen::VectorXd & x )
nodiscard

Definition at line 73 of file DerivativeSKI.cpp.

75{
77 const Eigen::Index n = x.size();
78 const Eigen::Index m = grid.size();
79 out.W = Eigen::MatrixXd::Zero(n, m);
80 out.dW = Eigen::MatrixXd::Zero(n, m);
81 out.out_of_grid = 0;
82 const double inv_h = 1.0 / grid.spacing();
83
84 for (Eigen::Index i = 0; i < n; ++i) {
85 const double t = grid.indexOf(x(i));
86 const Eigen::Index base = static_cast<Eigen::Index>(std::floor(t));
87 bool supported = false;
88 for (Eigen::Index k = -1; k <= 2; ++k) {
89 const Eigen::Index j = base + k;
90 if (j < 0 || j >= m) continue;
91 const double s = t - static_cast<double>(j);
92 const double w = cubicConvolutionWeight(s);
93 const double dw = cubicConvolutionWeightDerivative(s);
94 // Keys' weight vanishes at |s| = 1 while its derivative there is
95 // -/+ 1/2, so a zero weight does not mean the stencil point is
96 // inert: dropping it on w == 0 zeroes the derivative row of every
97 // input that lands on a grid node.
98 if (w == 0.0 && dw == 0.0) continue;
99 out.W(i, j) = w;
100 // d/dx = d/ds * ds/dx, and ds/dx = 1/h.
101 out.dW(i, j) = dw * inv_h;
102 supported = true;
103 }
104 if (!supported) ++out.out_of_grid;
105 }
106 return out;
107}
double spacing() const noexcept
Eigen::Index size() const noexcept
double indexOf(double x) const noexcept
Grid coordinate of x in index units.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double cubicConvolutionWeight(double s) noexcept
double cubicConvolutionWeightDerivative(double s) noexcept

◆ lanczosLowRank()

Eigen::MatrixXd gpr::ski::lanczosLowRank ( const Eigen::MatrixXd & A,
double tol,
Eigen::Index max_rank )
nodiscard

Compress a symmetric PSD matrix to the rank capturing tol of its trace, by Lanczos. Returns the factor Q S^{1/2} with A ~ F F^T.

Definition at line 198 of file DerivativeSKI.cpp.

200{
201 const Eigen::Index n = A.rows();
202 if (n == 0) return Eigen::MatrixXd();
203 // A symmetric eigendecomposition is the exact answer Lanczos approximates;
204 // using it here keeps the rank report about the kernel rather than about
205 // Lanczos convergence, which is the quantity in question.
206 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(A);
207 if (es.info() != Eigen::Success) return Eigen::MatrixXd();
208 Eigen::VectorXd ev = es.eigenvalues();
209 const double total = ev.cwiseMax(0.0).sum();
210 if (!(total > 0.0)) return Eigen::MatrixXd::Zero(n, 0);
211
212 // Descending order; keep the leading block holding (1 - tol) of the trace.
213 Eigen::Index keep = 0;
214 double acc = 0.0;
215 const Eigen::Index cap = (max_rank > 0) ? std::min(max_rank, n) : n;
216 for (Eigen::Index i = 0; i < cap; ++i) {
217 const double lam = std::max(0.0, ev(n - 1 - i));
218 acc += lam;
219 ++keep;
220 if (acc >= (1.0 - tol) * total) break;
221 }
222 Eigen::MatrixXd F(n, keep);
223 for (Eigen::Index i = 0; i < keep; ++i) {
224 const double lam = std::max(0.0, ev(n - 1 - i));
225 F.col(i) = es.eigenvectors().col(n - 1 - i) * std::sqrt(lam);
226 }
227 return F;
228}