Loading...
Searching...
No Matches
gpr::AdditivePairKernel Class Reference

#include <gpr/covariance_functions/AdditivePairKernel.h>

Inheritance diagram for gpr::AdditivePairKernel:

Classes

struct  Pair
struct  PairFeatures

Public Member Functions

 AdditivePairKernel (const AtomsConfiguration &configuration, Eigen::VectorXd length_scales, double magnitude_variance)
double magnitudeVariance () const noexcept
const Eigen::VectorXd & lengthScales () const noexcept
const std::vector< Pair > & pairs () const noexcept
Eigen::Index dimensions () const noexcept
PairFeatures features (const Eigen::VectorXd &positions) const
std::vector< PairFeatures > rowFeatures (const Eigen::MatrixXd &positions) const
Eigen::MatrixXd assemble (const std::vector< PairFeatures > &left, const std::vector< PairFeatures > &right) const
 Joint [E; gradient] covariance between the rows of two feature sets.
double directionalInverseLength (const Eigen::VectorXd &positions, const Eigen::VectorXd &direction) const
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.
Eigen::MatrixXd jointEnergyGradientCrossCovariance (const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions) const override
 Cross covariance in Observation order on each operand.
Eigen::MatrixXd jointEnergyGradientCovariance (const Eigen::MatrixXd &positions) const override
 Joint covariance in Observation order [E; G_col0; ...; G_colD-1].
Eigen::VectorXd energyDiagonal (const Eigen::MatrixXd &positions) const override
 Energy self covariance at each geometry, without an all-pairs matrix.
MeanFunction prepareMean (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
CrossCovarianceFunction prepareCrossCovariance (const Eigen::MatrixXd &training) const override
Eigen::VectorXd applyJointCrossCovariance (const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions, const Eigen::VectorXd &weights) const override
MeanHessianFunction prepareMeanHessianVector (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
MeanHessianPreparation prepareMeanHessian (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
MeanCurvature meanCurvature () const noexcept override
bool meanHessianVector (const Coord &training, const Eigen::VectorXd &weights, const Eigen::VectorXd &position, const Eigen::VectorXd &direction, Eigen::VectorXd &action) const override
Public Member Functions inherited from gpr::KernelOperator
virtual ~KernelOperator ()=default
virtual JointCovarianceTileFunction prepareJointCovarianceTiles (const Eigen::MatrixXd &) const
Eigen::MatrixXd energyCorrelationDistances (const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions) const
virtual Eigen::VectorXd jointEnergyGradientDiagonal (const Eigen::MatrixXd &positions) const
 Marginal prior variances in the same channel order as the joint matrix.

Static Public Member Functions

static std::vector< Pair > enumeratePairs (const AtomsConfiguration &configuration, Eigen::Index length_count)
static std::vector< Eigen::MatrixXd > jointWithLogLengthDerivatives (const std::vector< PairFeatures > &features, const std::vector< Pair > &pairs, Eigen::Index dimensions, const Eigen::VectorXd &length_scales, double magnitude_variance, const std::vector< Eigen::Index > &types, const linalg::MpiContext &communication=linalg::MpiContext::local(), const linalg::LocalExecutor &execution=linalg::LocalExecutor{})
static Eigen::VectorXd contractJointLogLengthDerivatives (const std::vector< PairFeatures > &features, const std::vector< Pair > &pairs, Eigen::Index dimensions, const Eigen::VectorXd &length_scales, double magnitude_variance, const std::vector< Eigen::Index > &types, const Eigen::MatrixXd &weight, const linalg::MpiContext &communication=linalg::MpiContext::local(), const linalg::LocalExecutor &execution=linalg::LocalExecutor{})

Additional Inherited Members

Public Types inherited from gpr::KernelOperator
enum class  MeanHessianStatus { Computed , Refused , Unavailable }
enum class  MeanCurvature { Unknown , Exact , Refused }
using MeanFunction = std::function<Eigen::VectorXd(const Eigen::MatrixXd&)>
using CrossCovarianceFunction
using JointCovarianceTileFunction
using MeanHessianFunction
using HessianAction
using MeanHessianPreparation
using MeanHessianMatrixFunction

Detailed Description

Sum over pairs of one squared-exponential covariance per pair type on the inverse distance,

k(x, x') = m sum_t sum_{p in t(x)} sum_{q in t(x')} exp(-(u_p - u'_q)^2 / (2 l_t^2)), u = 1 / r,

with the pair types of the Sexpat kernel. The joint exponential over all pair coordinates treats "compress pair p" and "compress pair q" as unrelated directions, so one measured bond compression teaches the posterior nothing about the other bonds of the same type; here every pair of a type shares one scalar process, so a single row moves them all.

Definition at line 30 of file AdditivePairKernel.h.

Constructor & Destructor Documentation

◆ AdditivePairKernel()

gpr::AdditivePairKernel::AdditivePairKernel ( const AtomsConfiguration & configuration,
Eigen::VectorXd length_scales,
double magnitude_variance )

One length scale per pair type of the configuration; pairs whose type has no length scale are inactive.

Definition at line 172 of file AdditivePairKernel.cpp.

175 : pairs_(enumeratePairs(configuration, length_scales.size())),
176 length_scales_(std::move(length_scales)),
177 magnitude_variance_(magnitude_variance),
178 dof_(3 * configuration.atoms_mov.type.size()),
179 frozen_positions_(configuration.atoms_froz_active.positions),
180 box_(configuration.cell)
181{
182 if (!(magnitude_variance_ >= 0.0) || !std::isfinite(magnitude_variance_))
183 throw std::invalid_argument("pair kernel magnitude must be finite");
184 for (Eigen::Index t = 0; t < length_scales_.size(); ++t)
185 if (!(length_scales_(t) > 0.0) || !std::isfinite(length_scales_(t)))
186 throw std::invalid_argument("pair kernel lengths must be positive");
187 if (dof_ <= 0)
188 throw std::invalid_argument("pair kernel needs moving atoms");
189}
static std::vector< Pair > enumeratePairs(const AtomsConfiguration &configuration, Eigen::Index length_count)

Member Function Documentation

◆ applyJointCrossCovariance()

Eigen::VectorXd gpr::AdditivePairKernel::applyJointCrossCovariance ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Contract the joint cross covariance with coefficients in [E; G] order. Operators can apply their feature contractions without a dense tensor.

Reimplemented from gpr::KernelOperator.

Definition at line 633 of file AdditivePairKernel.cpp.

637{
638 return prepareMean(right_positions, weights)(left_positions);
639}
MeanFunction prepareMean(const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override

◆ assemble()

Eigen::MatrixXd gpr::AdditivePairKernel::assemble ( const std::vector< PairFeatures > & left,
const std::vector< PairFeatures > & right ) const
nodiscard

Joint [E; gradient] covariance between the rows of two feature sets.

Definition at line 262 of file AdditivePairKernel.cpp.

265{
266 const Eigen::Index nl = static_cast<Eigen::Index>(left.size());
267 const Eigen::Index nr = static_cast<Eigen::Index>(right.size());
268 Eigen::MatrixXd result =
269 Eigen::MatrixXd::Zero(nl * (1 + dof_), nr * (1 + dof_));
270 std::vector<Eigen::MatrixXd> blocks;
271 for (Eigen::Index i = 0; i < nl; ++i)
272 for (Eigen::Index j = 0; j < nr; ++j) {
273 configurationPairBlocks(left[static_cast<std::size_t>(i)],
274 right[static_cast<std::size_t>(j)], pairs_,
275 dof_, length_scales_, magnitude_variance_,
276 {}, blocks);
277 const auto& block = blocks.front();
278 for (Eigen::Index lc = -1; lc < dof_; ++lc)
279 for (Eigen::Index rc = -1; rc < dof_; ++rc)
280 result(channelOffset(lc, nl) + i,
281 channelOffset(rc, nr) + j) = block(1 + lc, 1 + rc);
282 }
283 return result;
284}

◆ contractJointLogLengthDerivatives()

Eigen::VectorXd gpr::AdditivePairKernel::contractJointLogLengthDerivatives ( const std::vector< PairFeatures > & features,
const std::vector< Pair > & pairs,
Eigen::Index dimensions,
const Eigen::VectorXd & length_scales,
double magnitude_variance,
const std::vector< Eigen::Index > & types,
const Eigen::MatrixXd & weight,
const linalg::MpiContext & communication = linalg::MpiContext::local(),
const linalg::LocalExecutor & execution = linalg::LocalExecutor{} )
staticnodiscard

Frobenius products with the log-amplitude and requested log-length derivatives, without allocating a covariance matrix per parameter.

Definition at line 330 of file AdditivePairKernel.cpp.

336{
337 const Eigen::Index n = static_cast<Eigen::Index>(features.size());
338 const auto owned = ownedConfigurationPairs(n, communication);
339 const std::size_t workers = std::min(
340 owned.size(), static_cast<std::size_t>(execution.concurrency()));
341 std::array<Eigen::MatrixXd, 1> products{
342 Eigen::MatrixXd::Zero(1 + length_scales.size(), n * (n + 1) / 2)};
343 linalg::assembleCollectively(communication, products, [&] {
344 if (weight.rows() != n * (1 + dimensions) ||
345 weight.cols() != weight.rows())
346 throw std::invalid_argument(
347 "pair derivative weight has invalid shape");
348 std::vector<std::vector<Eigen::Index>> by_type(length_scales.size());
349 for (std::size_t p = 0; p < pairs.size(); ++p)
350 by_type.at(pairs[p].type).push_back(static_cast<Eigen::Index>(p));
351 std::vector<bool> selected(length_scales.size(), false);
352 for (const auto type: types)
353 selected.at(type) = true;
355 owned.size(), std::memory_order_relaxed);
356 execution.forEach(workers, [&](std::size_t worker) {
357 Eigen::MatrixXd block(1 + dimensions, 1 + dimensions);
358 for (std::size_t task = worker; task < owned.size();
359 task += workers) {
360 const auto [i, j, index] = owned[task];
361 auto part = products.front().col(index);
362 const auto& left = features[i];
363 const auto& right = features[j];
364 // Off-diagonal configuration blocks include their transposes.
365 // The adjoint need not be symmetric.
366 for (Eigen::Index lc = -1; lc < dimensions; ++lc)
367 for (Eigen::Index rc = -1; rc < dimensions; ++rc) {
368 const auto row = channelOffset(lc, n) + i;
369 const auto col = channelOffset(rc, n) + j;
370 block(1 + lc, 1 + rc) =
371 weight(row, col) +
372 (i != j ? weight(col, row) : 0.0);
373 }
374 for (Eigen::Index type = 0; type < length_scales.size();
375 ++type) {
376 const double s =
377 1.0 / (length_scales(type) * length_scales(type));
378 for (const auto p: by_type[type])
379 for (const auto q: by_type[type]) {
380 const auto& a = pairs[p];
381 const auto& b = pairs[q];
382 double l = 0.0, r = 0.0, mixed = 0.0;
383 for (int c = 0; c < 3; ++c) {
384 const auto ac = 1 + 3 * a.first + c;
385 const auto bc = 1 + 3 * b.first + c;
386 l += left.gradient(p, c) *
387 (block(ac, 0) -
388 (a.second >= 0
389 ? block(1 + 3 * a.second + c, 0)
390 : 0.0));
391 r += right.gradient(q, c) *
392 (block(0, bc) -
393 (b.second >= 0
394 ? block(0, 1 + 3 * b.second + c)
395 : 0.0));
396 for (int d = 0; d < 3; ++d) {
397 const auto bd = 1 + 3 * b.first + d;
398 double w = block(ac, bd);
399 if (a.second >= 0)
400 w -= block(1 + 3 * a.second + c, bd);
401 if (b.second >= 0)
402 w -= block(ac, 1 + 3 * b.second + d);
403 if (a.second >= 0 && b.second >= 0)
404 w += block(1 + 3 * a.second + c,
405 1 + 3 * b.second + d);
406 mixed += left.gradient(p, c) *
407 right.gradient(q, d) * w;
408 }
409 }
410 const double delta = left.u(p) - right.u(q);
411 const double k = magnitude_variance *
412 std::exp(-0.5 * s * delta * delta);
413 const auto contract =
414 [&](const PairTermWeights& w) {
415 return w.energy * block(0, 0) + w.left * l +
416 w.right * r + w.product * mixed;
417 };
418 part(0) +=
419 contract(pairTermWeights(k, s, delta, false));
420 if (selected[type])
421 part(type + 1) += contract(
422 pairTermWeights(k, s, delta, true));
423 }
424 }
425 }
426 });
427 });
428 // A fixed configuration-pair order makes the final contraction identical
429 // for local workers and for any partition of the fitting communicator.
430 Eigen::VectorXd total(products.front().rows());
431 for (Eigen::Index parameter = 0; parameter < total.size(); ++parameter) {
432 linalg::CompensatedSum sum;
433 for (Eigen::Index index = 0; index < products.front().cols(); ++index)
434 sum.add(products.front()(parameter, index));
435 total(parameter) = sum.value();
436 }
437 Eigen::VectorXd result(1 + types.size());
438 result(0) = total(0);
439 for (std::size_t i = 0; i < types.size(); ++i)
440 result(static_cast<Eigen::Index>(i) + 1) = total(types[i] + 1);
441 return result;
442}
PairFeatures features(const Eigen::VectorXd &positions) const
const std::vector< Pair > & pairs() const noexcept
Eigen::Index dimensions() const noexcept
void assembleCollectively(const MpiContext &context, Matrices &result, const Body &body)
static std::atomic_size_t pair_contraction_tiles
Configuration pairs contracted by this process, excluding transposes.

◆ covarianceBlock()

Eigen::MatrixXd gpr::AdditivePairKernel::covarianceBlock ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions,
Eigen::Index left_channel,
Eigen::Index right_channel ) const
nodiscardoverridevirtual

Covariance between one channel at each set of geometry rows.

Implements gpr::KernelOperator.

Definition at line 482 of file AdditivePairKernel.cpp.

486{
487 if (left_channel < -1 || left_channel >= dof_ || right_channel < -1 ||
488 right_channel >= dof_)
489 throw std::invalid_argument("pair covariance channel is invalid");
490 const Eigen::MatrixXd joint =
491 jointEnergyGradientCrossCovariance(left_positions, right_positions);
492 return joint.block(channelOffset(left_channel, left_positions.rows()),
493 channelOffset(right_channel, right_positions.rows()),
494 left_positions.rows(), right_positions.rows());
495}
Eigen::MatrixXd jointEnergyGradientCrossCovariance(const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions) const override
Cross covariance in Observation order on each operand.

◆ dimensions()

Eigen::Index gpr::AdditivePairKernel::dimensions ( ) const
inlinenodiscardnoexcept

Definition at line 71 of file AdditivePairKernel.h.

72 {
73 return dof_;
74 }

◆ directionalInverseLength()

double gpr::AdditivePairKernel::directionalInverseLength ( const Eigen::VectorXd & positions,
const Eigen::VectorXd & direction ) const
nodiscard

The largest inverse Cartesian length induced along a direction: maximize the magnitude of each pair coordinate's directional derivative divided by that pair type's fitted length scale.

Definition at line 444 of file AdditivePairKernel.cpp.

446{
447 if (direction.size() != dof_ || !direction.allFinite())
448 throw std::invalid_argument("invalid pair-kernel direction");
449 const PairFeatures query = features(positions);
450 double inverse_length = 0.0;
451 for (std::size_t p = 0; p < pairs_.size(); ++p) {
452 const Pair& pair = pairs_[p];
453 double projected = 0.0;
454 for (int c = 0; c < 3; ++c) {
455 projected +=
456 query.gradient(static_cast<Eigen::Index>(p), c) *
457 (direction(3 * pair.first + c) -
458 (pair.second >= 0 ? direction(3 * pair.second + c) : 0.0));
459 }
460 inverse_length = std::max(
461 inverse_length, std::abs(projected) / length_scales_(pair.type));
462 }
463 return inverse_length;
464}

◆ energyDiagonal()

Eigen::VectorXd gpr::AdditivePairKernel::energyDiagonal ( const Eigen::MatrixXd & positions) const
nodiscardoverridevirtual

Energy self covariance at each geometry, without an all-pairs matrix.

Reimplemented from gpr::KernelOperator.

Definition at line 497 of file AdditivePairKernel.cpp.

499{
500 const auto rows = rowFeatures(positions);
501 Eigen::VectorXd result(positions.rows());
502 for (Eigen::Index i = 0; i < positions.rows(); ++i) {
503 const auto& f = rows[static_cast<std::size_t>(i)];
504 double sum = 0.0;
505 for (std::size_t p = 0; p < pairs_.size(); ++p) {
506 const double s = 1.0 / (length_scales_(pairs_[p].type) *
507 length_scales_(pairs_[p].type));
508 for (std::size_t q = 0; q < pairs_.size(); ++q) {
509 if (pairs_[q].type != pairs_[p].type) continue;
510 const double delta = f.u(static_cast<Eigen::Index>(p)) -
511 f.u(static_cast<Eigen::Index>(q));
512 sum += std::exp(-0.5 * s * delta * delta);
513 }
514 }
515 result(i) = magnitude_variance_ * sum;
516 }
517 return result;
518}
std::vector< PairFeatures > rowFeatures(const Eigen::MatrixXd &positions) const

◆ enumeratePairs()

std::vector< AdditivePairKernel::Pair > gpr::AdditivePairKernel::enumeratePairs ( const AtomsConfiguration & configuration,
Eigen::Index length_count )
staticnodiscard

Definition at line 191 of file AdditivePairKernel.cpp.

193{
194 std::vector<Pair> pairs;
195 const Eigen::Index moving = configuration.atoms_mov.type.size();
196 const Eigen::Index frozen = configuration.atoms_froz_active.type.size();
197 const auto active = [&](Index_t type) {
198 return type != static_cast<Index_t>(EMPTY) &&
199 static_cast<Eigen::Index>(type) < length_count;
200 };
201 for (Eigen::Index j = 0; j + 1 < moving; ++j)
202 for (Eigen::Index i = j + 1; i < moving; ++i) {
203 const Index_t type =
204 configuration.pairtype(configuration.atoms_mov.type(0, i),
205 configuration.atoms_mov.type(0, j));
206 if (!active(type)) continue;
207 pairs.push_back({j, i, -1, static_cast<Eigen::Index>(type)});
208 }
209 for (Eigen::Index j = 0; j < moving; ++j)
210 for (Eigen::Index i = 0; i < frozen; ++i) {
211 const Index_t type = configuration.pairtype(
212 configuration.atoms_froz_active.type(0, i),
213 configuration.atoms_mov.type(0, j));
214 if (!active(type)) continue;
215 pairs.push_back({j, -1, i, static_cast<Eigen::Index>(type)});
216 }
217 return pairs;
218}
#define EMPTY
Definition Macros.h:39
uint32_t Index_t
Definition Macros.h:17

◆ features()

AdditivePairKernel::PairFeatures gpr::AdditivePairKernel::features ( const Eigen::VectorXd & positions) const
nodiscard

Definition at line 220 of file AdditivePairKernel.cpp.

222{
223 if (positions.size() != dof_ || !positions.allFinite())
224 throw std::invalid_argument("pair kernel positions have invalid shape");
225 PairFeatures result;
226 const Eigen::Index count = static_cast<Eigen::Index>(pairs_.size());
227 result.u.resize(count);
228 result.gradient.resize(count, 3);
229 for (Eigen::Index p = 0; p < count; ++p) {
230 const Pair& pair = pairs_[static_cast<std::size_t>(p)];
231 const double* a = positions.data() + 3 * pair.first;
232 const double* b = pair.second >= 0
233 ? positions.data() + 3 * pair.second
234 : frozen_positions_.data() + 3 * pair.frozen;
235 double dr[3];
236 box_.displacement(a, b, dr);
237 const double r2 = dr[0] * dr[0] + dr[1] * dr[1] + dr[2] * dr[2];
238 if (!(r2 > 0.0))
239 throw std::invalid_argument("pair kernel met a coincident pair");
240 const double r = std::sqrt(r2);
241 result.u(p) = 1.0 / r;
242 // d(1/r)/dx_a = -(x_a - x_b) / r^3 = dr / r^3 with dr = x_b - x_a.
243 const double scale = 1.0 / (r2 * r);
244 result.gradient(p, 0) = dr[0] * scale;
245 result.gradient(p, 1) = dr[1] * scale;
246 result.gradient(p, 2) = dr[2] * scale;
247 }
248 return result;
249}

◆ jointEnergyGradientCovariance()

Eigen::MatrixXd gpr::AdditivePairKernel::jointEnergyGradientCovariance ( const Eigen::MatrixXd & positions) const
nodiscardoverridevirtual

Joint covariance in Observation order [E; G_col0; ...; G_colD-1].

Reimplemented from gpr::KernelOperator.

Definition at line 473 of file AdditivePairKernel.cpp.

475{
476 return jointWithLogLengthDerivatives(rowFeatures(positions), pairs_, dof_,
477 length_scales_, magnitude_variance_,
478 {})
479 .front();
480}
static std::vector< Eigen::MatrixXd > jointWithLogLengthDerivatives(const std::vector< PairFeatures > &features, const std::vector< Pair > &pairs, Eigen::Index dimensions, const Eigen::VectorXd &length_scales, double magnitude_variance, const std::vector< Eigen::Index > &types, const linalg::MpiContext &communication=linalg::MpiContext::local(), const linalg::LocalExecutor &execution=linalg::LocalExecutor{})

◆ jointEnergyGradientCrossCovariance()

Eigen::MatrixXd gpr::AdditivePairKernel::jointEnergyGradientCrossCovariance ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions ) const
nodiscardoverridevirtual

Cross covariance in Observation order on each operand.

Reimplemented from gpr::KernelOperator.

Definition at line 466 of file AdditivePairKernel.cpp.

469{
470 return assemble(rowFeatures(left_positions), rowFeatures(right_positions));
471}
Eigen::MatrixXd assemble(const std::vector< PairFeatures > &left, const std::vector< PairFeatures > &right) const
Joint [E; gradient] covariance between the rows of two feature sets.

◆ jointWithLogLengthDerivatives()

std::vector< Eigen::MatrixXd > gpr::AdditivePairKernel::jointWithLogLengthDerivatives ( const std::vector< PairFeatures > & features,
const std::vector< Pair > & pairs,
Eigen::Index dimensions,
const Eigen::VectorXd & length_scales,
double magnitude_variance,
const std::vector< Eigen::Index > & types,
const linalg::MpiContext & communication = linalg::MpiContext::local(),
const linalg::LocalExecutor & execution = linalg::LocalExecutor{} )
staticnodiscard

Joint covariance of one feature set followed by its derivative with respect to the log length of every listed type, in that order.

Definition at line 286 of file AdditivePairKernel.cpp.

292{
293 const Eigen::Index n = static_cast<Eigen::Index>(features.size());
294 const std::size_t channels = 1 + types.size();
295 std::vector<Eigen::MatrixXd> result(
296 channels,
297 Eigen::MatrixXd::Zero(n * (1 + dimensions), n * (1 + dimensions)));
298 const auto owned = ownedConfigurationPairs(n, communication);
299 const std::size_t workers = std::min(
300 owned.size(), static_cast<std::size_t>(execution.concurrency()));
301 linalg::assembleCollectively(communication, result, [&] {
302 execution.forEach(workers, [&](std::size_t worker) {
303 std::vector<Eigen::MatrixXd> blocks;
304 for (std::size_t task = worker; task < owned.size();
305 task += workers) {
306 const auto [i, j, index] = owned[task];
307 configurationPairBlocks(features[static_cast<std::size_t>(i)],
308 features[static_cast<std::size_t>(j)],
309 pairs, dimensions, length_scales,
310 magnitude_variance, types, blocks);
311 for (std::size_t channel = 0; channel < channels; ++channel) {
312 const auto& block = blocks[channel];
313 auto& out = result[channel];
314 for (Eigen::Index lc = -1; lc < dimensions; ++lc)
315 for (Eigen::Index rc = -1; rc < dimensions; ++rc) {
316 const double value = block(1 + lc, 1 + rc);
317 out(channelOffset(lc, n) + i,
318 channelOffset(rc, n) + j) = value;
319 if (i != j)
320 out(channelOffset(rc, n) + j,
321 channelOffset(lc, n) + i) = value;
322 }
323 }
324 }
325 });
326 });
327 return result;
328}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

◆ lengthScales()

const Eigen::VectorXd & gpr::AdditivePairKernel::lengthScales ( ) const
inlinenodiscardnoexcept

Definition at line 63 of file AdditivePairKernel.h.

64 {
65 return length_scales_;
66 }

◆ magnitudeVariance()

double gpr::AdditivePairKernel::magnitudeVariance ( ) const
inlinenodiscardnoexcept

Definition at line 59 of file AdditivePairKernel.h.

60 {
61 return magnitude_variance_;
62 }

◆ meanCurvature()

MeanCurvature gpr::AdditivePairKernel::meanCurvature ( ) const
inlinenodiscardoverridevirtualnoexcept

Reimplemented from gpr::KernelOperator.

Definition at line 139 of file AdditivePairKernel.h.

◆ meanHessianVector()

bool gpr::AdditivePairKernel::meanHessianVector ( const Coord & training,
const Eigen::VectorXd & weights,
const Eigen::VectorXd & position,
const Eigen::VectorXd & direction,
Eigen::VectorXd & action ) const
nodiscardoverridevirtual

Hessian action of the scalar kernel expansion with joint [E; G] training coefficients. False denotes an unsupported derivative.

Reimplemented from gpr::KernelOperator.

Definition at line 695 of file AdditivePairKernel.cpp.

700{
701 if (position.size() != dof_ || direction.size() != dof_ ||
702 !position.allFinite() || !direction.allFinite())
703 return false;
704 const auto apply = prepareMeanHessian(training, weights)(position);
705 return static_cast<bool>(apply) && apply(direction, action);
706}
MeanHessianPreparation prepareMeanHessian(const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override

◆ pairs()

const std::vector< Pair > & gpr::AdditivePairKernel::pairs ( ) const
inlinenodiscardnoexcept

Definition at line 67 of file AdditivePairKernel.h.

68 {
69 return pairs_;
70 }

◆ prepareCrossCovariance()

KernelOperator::CrossCovarianceFunction gpr::AdditivePairKernel::prepareCrossCovariance ( const Eigen::MatrixXd & training) const
nodiscardoverridevirtual

Immutable observation functionals for a fixed right operand. Query geometries are evaluated afresh; an empty function requests fallback.

Reimplemented from gpr::KernelOperator.

Definition at line 624 of file AdditivePairKernel.cpp.

626{
627 return [kernel = *this, right = rowFeatures(training)](
628 const Eigen::MatrixXd& query) -> Eigen::MatrixXd {
629 return kernel.assemble(kernel.rowFeatures(query), right);
630 };
631}

◆ prepareMean()

KernelOperator::MeanFunction gpr::AdditivePairKernel::prepareMean ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Immutable fitted coefficients with fresh evaluation at every query. An empty function requests the owning generic covariance product.

Reimplemented from gpr::KernelOperator.

Definition at line 593 of file AdditivePairKernel.cpp.

595{
596 return [kernel = *this, basis = prepareMeanBasis(training, weights)](
597 const Eigen::MatrixXd& query) -> Eigen::VectorXd {
598 requireDimension(query, kernel.dof_, "pair kernel mean query");
599 const Eigen::Index nl = query.rows();
600 Eigen::VectorXd result = Eigen::VectorXd::Zero(nl * (kernel.dof_ + 1));
601 Eigen::VectorXd value, first, second;
602 for (Eigen::Index i = 0; i < nl; ++i) {
603 const PairFeatures f = kernel.features(query.row(i).transpose());
604 kernel.meanDerivatives(f, basis, value, first, second);
605 result(i) = value.sum();
606 for (std::size_t p = 0; p < kernel.pairs_.size(); ++p) {
607 const Pair& pair = kernel.pairs_[p];
608 const double coefficient = first(static_cast<Eigen::Index>(p));
609 for (int c = 0; c < 3; ++c) {
610 const double g =
611 coefficient *
612 f.gradient(static_cast<Eigen::Index>(p), c);
613 result(nl * (1 + 3 * pair.first + c) + i) += g;
614 if (pair.second >= 0)
615 result(nl * (1 + 3 * pair.second + c) + i) -= g;
616 }
617 }
618 }
619 return result;
620 };
621}

◆ prepareMeanHessian()

KernelOperator::MeanHessianPreparation gpr::AdditivePairKernel::prepareMeanHessian ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Own the fitted mean coefficients and construct a scoped linear Hessian operator at each requested geometry. No query is retained by the fitted model. An empty preparation requests the directional path.

Reimplemented from gpr::KernelOperator.

Definition at line 641 of file AdditivePairKernel.cpp.

643{
644 auto basis =
645 std::make_shared<const MeanBasis>(prepareMeanBasis(training, weights));
646 return [kernel = *this, basis = std::move(basis)](
647 const Eigen::VectorXd& position) -> HessianAction {
648 if (position.size() != kernel.dof_ || !position.allFinite()) return {};
649 const PairFeatures f = kernel.features(position);
650 Eigen::VectorXd value, first, second;
651 kernel.meanDerivatives(f, *basis, value, first, second);
652 std::vector<priors::PriorHessianPair> pair_blocks;
653 pair_blocks.reserve(kernel.pairs_.size());
654 for (std::size_t p = 0; p < kernel.pairs_.size(); ++p) {
655 const Pair& pair = kernel.pairs_[p];
656 const Eigen::Index row = static_cast<Eigen::Index>(p);
657 const Eigen::Vector3d g = f.gradient.row(row).transpose();
658 // g = dr / r^3 with |g| = 1 / r^2; the Hessian of 1 / r is
659 // (3 dr dr^T / r^2 - I) / r^3.
660 const double inverse_r2 = g.norm();
661 const double inverse_r = std::sqrt(inverse_r2);
662 const Eigen::Vector3d unit = g / inverse_r2;
663 const Eigen::Matrix3d curvature =
664 inverse_r2 * inverse_r *
665 (3.0 * unit * unit.transpose() - Eigen::Matrix3d::Identity());
666 const Eigen::Matrix3d block =
667 second(row) * (g * g.transpose()) + first(row) * curvature;
668 if (!block.allFinite()) return {};
669 pair_blocks.push_back({3 * pair.first,
670 pair.second >= 0 ? 3 * pair.second : -1,
671 block});
672 }
673 return [pair_blocks = std::move(pair_blocks), dof = kernel.dof_](
674 const Eigen::VectorXd& direction, Eigen::VectorXd& action) {
675 if (direction.size() != dof || !direction.allFinite()) return false;
676 action = priors::applyPriorHessianPairs(pair_blocks, direction);
677 return action.allFinite();
678 };
679 };
680}
std::function< bool(const Eigen::VectorXd &, Eigen::VectorXd &)> HessianAction
Eigen::MatrixXd applyPriorHessianPairs(const std::vector< PriorHessianPair > &pairs, const Eigen::MatrixBase< Derived > &directions)
Apply physical prior curvature without forming a dense Hessian.

◆ prepareMeanHessianVector()

KernelOperator::MeanHessianFunction gpr::AdditivePairKernel::prepareMeanHessianVector ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Owning fitted derivative coefficients; query geometry is evaluated afresh. An empty function requests the generic scalar-kernel action.

Reimplemented from gpr::KernelOperator.

Definition at line 683 of file AdditivePairKernel.cpp.

685{
686 auto prepare = prepareMeanHessian(training, weights);
687 return [prepare = std::move(prepare)](const Eigen::VectorXd& position,
688 const Eigen::VectorXd& direction,
689 Eigen::VectorXd& action) {
690 const auto apply = prepare(position);
691 return static_cast<bool>(apply) && apply(direction, action);
692 };
693}

◆ rowFeatures()

std::vector< AdditivePairKernel::PairFeatures > gpr::AdditivePairKernel::rowFeatures ( const Eigen::MatrixXd & positions) const
nodiscard

Definition at line 251 of file AdditivePairKernel.cpp.

253{
254 requireDimension(positions, dof_, "pair kernel positions");
255 std::vector<PairFeatures> result;
256 result.reserve(static_cast<std::size_t>(positions.rows()));
257 for (Eigen::Index row = 0; row < positions.rows(); ++row)
258 result.push_back(features(positions.row(row).transpose()));
259 return result;
260}

The documentation for this class was generated from the following files: