class gpr::SexpatCF¶
Overview¶
Pair-type inverse-distance squared-exponential primitive. More…
#include <gpr/covariance_functions/SexpatCF.h>
class SexpatCF {
public:
// typedefs
typedef Sexpat::AlphaKPairInfo AlphaKPairInfo;
typedef Sexpat::PreparedAlphaK PreparedAlphaK;
typedef Sexpat::HessianPrecision HessianPrecision;
typedef Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ExpDistArray;
typedef Derivatives<std::vector<FieldMatrixd>> SharedDerivatives;
// structs
struct CrossBlockGeometry;
struct PriorState;
// construction
SexpatCF();
virtual ~SexpatCF();
// methods
void setHessianPrecision(HessianPrecision precision);
HessianPrecision hessianPrecision() const;
void setUseLongDoubleHessian(bool enabled);
bool useLongDoubleHessian() const;
bool meanHessianVector(const Coord& training, const Eigen::VectorXd& weights, const Eigen::VectorXd& position, const Eigen::VectorXd& direction, Eigen::VectorXd& action) const;
double calculateLogPrior();
FieldMatrixd calculateLogPriorGradient();
void calculateCovarianceMatrix(const Coord& x1, Coord& x2, FieldMatrixd& C);
void correlationDistances(const Coord& left, const Coord& right, const AtomsConfiguration& configuration, FieldMatrixd& distances) const;
void calculateTrainingCovarianceMatrix(const Coord& x, FieldMatrixd& C);
void calculateGradOfCovMatrix(const Coord& x1, Coord& x2, std::vector<FieldMatrixd>& DKff);
void calculateGradOfCovMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff);
void calculateGradOfCovMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff);
void calculateDirectionalCovarianceMatrix(const Coord& x1, Coord& x2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
void calculateDirectionalCovarianceMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
void calculateDirectionalCovarianceMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
bool buildCrossBlockGeometry(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const;
bool buildCrossBlockDerivativesOnly(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const;
bool buildSharedDistanceForTrainingGeometry(const Coord& x_loc, std::vector<FieldMatrixd>& shared_distance_neg_per_pt) const;
bool evaluateCrossBlockGeometry(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, FieldMatrixd& out) const;
bool evaluateCrossBlockGeometryShared(const CrossBlockGeometry& geometry, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, FieldMatrixd& out) const;
bool buildSharedKernel(const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, FieldMatrixd& kernel_out) const;
bool evaluateCrossBlockGeometryWithSharedKernel(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const Eigen::VectorXd& theta, FieldMatrixd& out) const;
bool calculateDirectionalCovarianceMatrixFromGeometry(const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const;
bool calculateDirectionalCovarianceMatrixWithDerivativesFromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const;
bool calculateDirectionalCovarianceMatrixWithDerivatives2FromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const;
bool calculateGradOfCovMatrixFromGeometry(const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const;
bool calculateGradOfCovMatrixWithDerivativesFromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const;
bool calculateGradOfCovMatrixWithDerivatives2FromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const;
bool applyCrossBlockGeometryToVector(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, const Eigen::VectorXd& weights, bool transpose_field, Eigen::VectorXd& out) const;
void ginput2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff, ExpDistArray* out_expdist = nullptr);
void ginput3(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff, ExpDistArray* out_expdist = nullptr, const SharedDerivatives* precomputed = nullptr);
void ginput4(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff, ExpDistArray* out_expdist = nullptr);
void predictMeanAlphaK(const Coord& x_train, const Coord& x_test, const Eigen::Ref<const Eigen::VectorXd>& alpha_E, const Eigen::Ref<const Eigen::VectorXd>& alpha_G_flat, Eigen::Ref<Eigen::VectorXd> mu_E_out, Eigen::Ref<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> mu_G_out);
bool prepareAlphaK(const Coord& x_train, const Eigen::Ref<const Eigen::VectorXd>& alpha_E, const Eigen::Ref<const Eigen::VectorXd>& alpha_G_flat, PreparedAlphaK& prepared) const;
void predictMeanAlphaKPrepared(const PreparedAlphaK& prepared, const Coord& x_test, Eigen::Ref<Eigen::VectorXd> mu_E_out, Eigen::Ref<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> mu_G_out) const;
void trainingGradientAlphaK(const Coord& x_train, const Eigen::Ref<const Eigen::VectorXd>& alpha_E, const Eigen::Ref<const Eigen::VectorXd>& alpha_G_flat, Eigen::Ref<Eigen::VectorXd> quad_out, bool distributed = false, std::size_t* local_pair_count = nullptr, const linalg::MpiContext* process_group = nullptr);
void setMagnSigma2(double value);
void setLengthScale(double value);
void setFarFieldRadius(double radius);
double farFieldRadius() const;
void setManifoldChart(std::shared_ptr<const geometry::InternalCoordinateChart> chart, double length_scale);
void clearManifoldChart();
bool usesManifoldChart() const;
const std::shared_ptr<const geometry::InternalCoordinateChart>& manifoldChart() const;
Eigen::Index manifoldDimensions() const;
FieldMatrixd manifoldJointEnergyGradientCovariance(const Coord& positions) const;
FieldMatrixd manifoldJointEnergyGradientCrossCovariance(const Coord& left_positions, const Coord& right_positions) const;
void resetManifoldGeometryRowsEvaluatedForTesting() const;
void addManifoldGeometryRowsEvaluatedForTesting(Eigen::Index left_rows, Eigen::Index right_rows) const;
std::size_t manifoldGeometryRowsEvaluatedForTesting() const;
void setGeodesicHeatReferencePath(const Coord& reference_path, double covariance_weight);
void setGeodesicHeatKernel(std::shared_ptr<const GeodesicHeatEnergyForceKernel> kernel);
const std::shared_ptr<const GeodesicHeatEnergyForceKernel>& geodesicHeatKernel() const;
void rescaleGeodesicHeatCovariance(double factor);
void clearGeodesicHeatKernel();
bool usesGeodesicHeatKernel() const;
Eigen::Index geodesicHeatDimensions() const;
FieldMatrixd geodesicHeatJointEnergyGradientCovariance(const Coord& positions) const;
FieldMatrixd geodesicHeatJointEnergyGradientCrossCovariance(const Coord& left_positions, const Coord& right_positions) const;
void setConfInfo(const AtomsConfiguration& value);
const AtomsConfiguration& getConfInfo() const;
void setParameters(const Eigen::VectorXd& w);
double getMagnSigma2();
double getMagnSigma2() const;
FieldMatrixd& getLengthScaleRef();
const FieldMatrixd& getLengthScaleRef() const;
FieldMatrixd& getAtomisticLengthScaleRef();
const FieldMatrixd& getAtomisticLengthScaleRef() const;
std::shared_ptr<const KernelOperator> assembledOperator() const;
void clear();
void setPriorParametersGaussian(const PriorBase& prior);
void setPriorParametersSqrtt(const PriorBase& prior);
void setPriorParametersLogNormal(const PriorBase& prior);
PriorBase getPriorParametersGaussian();
PriorBase getPriorParametersSqrtt();
PriorBase getPriorParametersLogNormal();
void setUseLogNormalMagnPrior(bool enabled);
bool useLogNormalMagnPrior() const;
void setModelProfile(GPModelProfile profile);
GPModelProfile getModelProfile() const;
void setLengthScalePriorWeights(const Eigen::VectorXd& weights);
const Eigen::VectorXd& lengthScalePriorWeights() const;
void setHierarchicalPrior(const Eigen::VectorXd& mu_log, double s2_log);
void setHierarchicalPrior(const Eigen::VectorXd& mu_log, double s2_log, const Eigen::VectorXd& s2_log_per);
const Eigen::VectorXd& hierarchicalPriorMu() const;
void setHierarchicalAnchorMask(std::vector<char> mask);
bool hierarchicalAnchored(Eigen::Index n) const;
double hierarchicalPriorVariance(Eigen::Index n) const;
void clearHierarchicalPrior();
void setPcLengthScalePrior(const Eigen::VectorXd& lambda);
void clearPcLengthScalePrior();
bool hasHierarchicalPrior() const;
bool hasPcLengthScalePrior() const;
PriorState priorState() const;
void restorePriorState(PriorState state);
Eigen::VectorXd combineParameters();
double calculateDirectionalLogPriorCurvature(const Eigen::VectorXd& direction) const;
void cacheDistances(const Coord& x1, const Coord& x2);
void clearDistanceCache();
bool hasDistanceCache() const;
};
Detailed Documentation¶
Pair-type inverse-distance squared-exponential primitive.
Geodesic add is assembled as Sum(Sexpat, GeodesicHeat). Manifold dispatch is assembled as Pullback(SE, chart). A geodesic summand can augment either base; chart and pair-metric length scales are independent.
Typedefs¶
typedef Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ExpDistArray
Calculates covariances between the derivatives.
Parameters:
x1 |
Matrix of coordinates |
x2 |
Matrix of coordinates |
dims |
Coordinate-dimension indices for the derivative covariance. |
DKff |
Output derivative-covariance blocks. |
out_expdist |
Optional output array of the shared exponential distance factors. Row-major array of exp(-0.5 * d^2), the shared squared-exponential factor. ginput2 and ginput3 both build one; the out-parameter below hands it to a caller that would otherwise recompute the same transcendental over the same distances. |
typedef Derivatives<std::vector<FieldMatrixd>> SharedDerivatives
Evaluate gradient of covariance function with respect to both input variables x1 and x2 (in different dimensions).
This function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of twice derivatived covariance matrix K(df,df) = dk(X1,X2)/dX1dX2 (cell array with matrix elements). The derivative is calculated in multidimensional problem between input’s observation dimensions which are not same. This subfunction is needed when using derivative observations.
Parameters:
x1 |
Configurations supplying the first covariance argument. |
x2 |
Configurations supplying the second covariance argument. |
dims1 |
Coordinate-dimension indices differentiated in |
dims2 |
Coordinate-dimension indices differentiated in |
DKff |
Output second-derivative covariance blocks. Optional precomputed D1/D2/D12 for ginput3. Its caller calculateGradOfCovMatrixWithDerivatives2 asks calculateDerivativesDiffDim for a superset of the same fields over the same inputs, and the direction argument that appears to distinguish the two calls is unread ( |
Methods¶
double calculateLogPrior()
Evaluate the log prior density of the parameters.
This function takes a covariance function structure GPCF and returns log(p(th)), where th collects the parameters. This is a mandatory subfunction used for example in energy computations.
Evaluate the prior contribution to the error. The parameters that are sampled are transformed, e.g., W = log(w) where w is all the “real” samples. On the other hand errors are evaluated in the W-space so we need take into account also the Jacobian of transformation, e.g., W -> w = exp(W). See Gelman et al. (2013), Bayesian Data Analysis, third edition, p. 21.
FieldMatrixd calculateLogPriorGradient()
Evaluate the gradient of the log prior density of the parameters with respect to parameters.
This function takes a covariance function structure GPCF and returns LPG = d log (p(th))/dth, where th is the vector of parameters. This is a mandatory subfunction used in gradient computations.
void calculateCovarianceMatrix(const Coord& x1, Coord& x2, FieldMatrixd& C)
Evaluate covariance matrix between two input vectors.
The function takes in covariance function of a Gaussian process GP and two matrixes TX and X that contain input vectors to GP. Returns covariance matrix C. Every element ij of C contains covariance between inputs i in TX and j in X. This is a mandatory subfunction used for example in prediction and energy computations.
Parameters:
x1 |
Input configurations supplying the rows of the covariance. |
x2 |
Input configurations supplying the columns of the covariance. |
C |
Output covariance matrix between |
void correlationDistances(const Coord& left, const Coord& right, const AtomsConfiguration& configuration, FieldMatrixd& distances) const
Dimensionless support scores from the selected prediction covariance. A score of one corresponds to normalized energy covariance exp(-1/2). Nonpositive composed covariance gives an infinite score.
void calculateTrainingCovarianceMatrix(const Coord& x, FieldMatrixd& C)
Evaluate the prior variance for each training input.
Function takes in covariance function of a Gaussian process GPCF and matrix TX that contains training inputs. Returns variance vector C. Every element i of C contains variance of input i in TX. This is a mandatory subfunction used for example in prediction and energy computations.
Parameters:
x |
Input configurations whose prior variances are evaluated. |
C |
Output column vector of prior variances, one per configuration. |
void calculateGradOfCovMatrix(const Coord& x1, Coord& x2, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function with respect to the parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of covariance matrix Kff = k(X,X) with respect to th (cell array with matrix elements). This is a mandatory subfunction used for example in gradient computations.
Parameters:
x1 |
Input configurations for the first covariance argument. |
x2 |
Input configurations for the second covariance argument. |
DKff |
Output covariance-matrix gradients with respect to the covariance hyperparameters. |
void calculateGradOfCovMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function, of which has been taken partial derivative with respect to x1, with respect to parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of derivatived covariance matrix dK(df,f)/dhyp = d(d k(X,X)/dx)/dhyp, with respect to the parameters.
- Evaluate: DKff{1:m} = d Kff / d magnSigma2 DKff{m+1:2m} = d Kff / d lengthScale_m m is the dimension of inputs. If ARD is used, then multiple lengthScales. This subfunction is needed when using derivative observations. dims - is a vector of input dimensions with respect to which the
derivatives of the covariance function have been calculated [by default dims=1:size(x,2)]
Note! When coding the derivatives of the covariance function, remember to double check them. See gp_cov for lines of code to check the matrices
Parameters:
x1 |
Configurations supplying the first covariance argument. |
x2 |
Configurations supplying the second covariance argument. |
dims |
Coordinate-dimension indices of the derivative observations. |
DKff |
Output covariance-derivative blocks for each hyperparameter. |
void calculateGradOfCovMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function, of which has been taken partial derivatives with respect to both input variables x1 and x2 with respect to parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of derivative covariance matrix dK(df,df)/dhyp = d(d^2 k(X1,X2)/dX1dX2)/dhyp with respect to the parameters.
Evaluate: DKff{1-m} = d Kff / d magnSigma2 DKff{m+1-2m} = d Kff / d lengthScale_m m is the dimension of inputs. If ARD is used, then multiple lengthScales. This subfunction is needed when using derivative observations.
Parameters:
x1 |
Configurations supplying the first covariance argument. |
x2 |
Configurations supplying the second covariance argument. |
dims1 |
Coordinate-dimension indices differentiated in |
dims2 |
Coordinate-dimension indices differentiated in |
DKff |
Output second-derivative covariance blocks for each hyperparameter. |
void calculateDirectionalCovarianceMatrix(const Coord& x1, Coord& x2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate the directional first and second derivatives of the covariance matrix along a parameter-space direction.
The direction is expressed in the packed log-parameter coordinates returned by combineParameters().
void calculateDirectionalCovarianceMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate directional first and second derivatives of the derivative-vs-value covariance block.
void calculateDirectionalCovarianceMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate directional first and second derivatives of the derivative-vs-derivative covariance block.
bool buildCrossBlockGeometry(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const
Build theta-independent pairtype geometry for a covariance block.
The stored fields are raw pairtype contributions. They can be evaluated at any packed Sexpat log-parameter vector without re-walking atomic coordinates.
bool buildCrossBlockDerivativesOnly(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const
Populate only the derivative fields (d1, d2, d12) of a CrossBlockGeometry, skipping the per-pairtype distance computation.
Companion to buildCrossBlockGeometry for callers that already hold a shared per-pairtype distance tensor across many blocks with identical atom positions (e.g. the SCG training-cov assembly, where each (block_n, block_m) (D+1)*N row slice contains the same coordinates with different derivative-dim tags by construction of assembleMatrixOfRepetitiveCoordinates). The shared distance must be applied at evaluate time via evaluateCrossBlockGeometryShared.
bool buildSharedDistanceForTrainingGeometry(const Coord& x_loc, std::vector<FieldMatrixd>& shared_distance_neg_per_pt) const
Build the per-pairtype distance tensor at unit lengthscale for a single coordinate slice (shared across all training-cov blocks). The output convention matches CrossBlockGeometry.distance : shared_distance_neg_per_pt[pt] = -dist_at_vec(x_loc, x_loc, unit)[pt].
bool evaluateCrossBlockGeometry(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, FieldMatrixd& out) const
Evaluate a cross-block geometry at packed Sexpat log parameters.
bool evaluateCrossBlockGeometryShared(const CrossBlockGeometry& geometry, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, FieldMatrixd& out) const
Same as evaluateCrossBlockGeometry but reads the per-pairtype distance from an externally-shared tensor (negated, matching the sign convention of buildCrossBlockGeometry). The supplied geometry’s .distance field is ignored; .d1, .d2, .d12 are still consulted.
Used together with buildCrossBlockDerivativesOnly to skip 946-fold redundant dist_at_vec calls in the SCG training-cov assembly when all (block_n, block_m) blocks share the same atom positions.
bool buildSharedKernel(const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, FieldMatrixd& kernel_out) const
Apply theta to a shared distance to produce the kernel value K(theta)[n, m] = exp(theta(0)) * exp(-0.5 * sum_pt exp(-2 theta(1+pt)).
(-shared_distance_neg[pt][n, m])). Pre-computed once per inner iter and reused by per-block apply.
bool evaluateCrossBlockGeometryWithSharedKernel(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const Eigen::VectorXd& theta, FieldMatrixd& out) const
Evaluate a derivative-only block against a pre-computed shared kernel matrix (from buildSharedKernel). No exp() per call.
bool calculateDirectionalCovarianceMatrixFromGeometry(const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const
Directional first and second derivatives for the Kff block from shared training geometry.
bool calculateDirectionalCovarianceMatrixWithDerivativesFromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const
Directional first and second derivatives for a Kdf block from shared training geometry.
bool calculateDirectionalCovarianceMatrixWithDerivatives2FromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second) const
Directional first and second derivatives for a D/Kdf2 block from shared training geometry.
bool calculateGradOfCovMatrixFromGeometry(const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const
Geometry-path replacement for calculateGradOfCovMatrix. Produces DKff[0] = K (untruncated kernel), DKff[1+pt] = K * inv_l_pt^2 * shared_distance_neg[pt] (== -K * dist_pt_actual in the legacy negated sign convention). Scalar-length case collapses to a single DKff[1] entry that sums across pairtypes. An optional nonempty parameter list selects output blocks in the given order. All parameters still contribute to the kernel and its Cartesian derivatives; only the requested hyperparameter derivatives are formed.
bool calculateGradOfCovMatrixWithDerivativesFromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const
Geometry-path replacement for calculateGradOfCovMatrixWithDerivatives (Kdf block dK).
The Kdf block geometry (built once at unit lengthscale and stored in geometry with derivative_options=D1) gives the per-pairtype d1 fields. DKff[0] = Kdf(theta) (== evaluateCrossBlockGeometryWithSharedKernel with options=D1). DKff[1+pt] is dKdf/dloglength_pt; the algebra matches the live calculateGradOfCovMatrixWithDerivatives (DK_pt[pt] = magnSigma2 * exp * (-D1_pt[pt] + 0.5 * D1[n] * dist_pt[pt])) with D1_pt being the per-pairtype gradient at unit lengthscale and D1 being the lengthscale-weighted sum across pairtypes.
bool calculateGradOfCovMatrixWithDerivatives2FromGeometry(const CrossBlockGeometry& geometry, const FieldMatrixd& shared_kernel, const std::vector<FieldMatrixd>& shared_distance_neg_per_pt, const Eigen::VectorXd& theta, std::vector<FieldMatrixd>& DKff, const std::vector<Eigen::Index>* parameter_indices = nullptr) const
Geometry-path replacement for calculateGradOfCovMatrixWithDerivatives2 (D and Kdf2 blocks).
Same shape as the original: DKff[0] is the block value at theta (== evaluateCrossBlockGeometryWithSharedKernel options=D1|D2|D12), DKff[1+pt] is the derivative wrt loglength_pt.
bool applyCrossBlockGeometryToVector(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, const Eigen::VectorXd& weights, bool transpose_field, Eigen::VectorXd& out) const
Apply a cross-block geometry directly to a vector.
When transpose_field is false, computes out = A(theta)^T * weights. When it is true, computes out = A(theta) * weights.
void ginput4(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff, ExpDistArray* out_expdist = nullptr)
Evaluate gradient of covariance function with respect to x.
Simplified and faster version of sexp_ginput, returns full matrices.
Returns DKff, the gradients of covariance matrix Kff = k(X,X2) with respect to dimensions DIMS of X.
Parameters:
x1 |
Configurations supplying the first covariance argument. |
x2 |
Configurations supplying the second covariance argument. |
dims |
Coordinate-dimension indices differentiated in |
DKff |
Output covariance-derivative blocks. |
out_expdist |
Optional output array of the shared exponential distance factors. |
void predictMeanAlphaK(const Coord& x_train, const Coord& x_test, const Eigen::Ref<const Eigen::VectorXd>& alpha_E, const Eigen::Ref<const Eigen::VectorXd>& alpha_G_flat, Eigen::Ref<Eigen::VectorXd> mu_E_out, Eigen::Ref<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> mu_G_out)
Per-test-point predict mean (energy + gradient) via the Schmitz/Chmiela JPCL 2022 eq.6 alpha-K early contraction applied at the per-pair level.
For each test point m, accumulates into mu_E_out[m] and mu_G_out(m, beta) the SexpAt contribution to predict mean:
mu_E[m] += Sum_i alpha_E[i] * k(x_i, x_m)
Sum_i Sum_d alpha_G[i, d] * dk/dx_i^d mu_G[m, beta] += Sum_i alpha_E[i] * dk/dx_m^beta
Sum_i Sum_d alpha_G[i, d] * d^2k/(dx_i^d dx_m^beta)
Cost: O(num_train * n_pairs) per test point instead of the O(num_train * num_test * D^2) of the legacy per-(d1, d2) loop.
alpha_G_flat layout matches the existing accumulator’s weights vector: alpha_G_flat[d * num_train + i] is the gradient alpha for training point i, atomic-coord dim d (1-based d, i.e. uDdim[k] for k in 0..D-1; entries at d=0 are alpha_E and live separately in alpha_E).
Outputs are ACCUMULATED in place (callers zero them first). RowMajor mu_G_out keeps the layout consistent with image1.G in calculatePotential.
Math notes:
Per pair p in a pairtype, the per-pair gradient du_p/dx_alpha is nonzero on only 6 atomic-coord slots (3 per atom in the pair; 3 for moving-frozen pairs).
The D12 fold collapses to a per-pair scalar c_i_p = Sum over 6 x-slots of alpha_G[i, d] * du_p/dx_i^d, verified rank-1 per pair by python/tests/test_sympy_alpha_k_d12.py.
Convention: dist returned by dist_at_cached_sq is 2 * Sum_p (1/l_p^2) * (u_p_i - v_p_m)^2; k = magn * exp(-0.5 * dist).
void trainingGradientAlphaK(const Coord& x_train, const Eigen::Ref<const Eigen::VectorXd>& alpha_E, const Eigen::Ref<const Eigen::VectorXd>& alpha_G_flat, Eigen::Ref<Eigen::VectorXd> quad_out, bool distributed = false, std::size_t* local_pair_count = nullptr, const linalg::MpiContext* process_group = nullptr)
Accumulate joint energy/force quadratic derivatives. Distributed execution is collective; local calls do not enter MPI. The optional count reports the unique training pairs evaluated by this rank.
void setFarFieldRadius(double radius)
Frozen atoms at or beyond radius use the dipole tensor. Zero keeps the exact inverse-distance pairs.
void setManifoldChart(std::shared_ptr<const geometry::InternalCoordinateChart> chart, double length_scale)
Attach a chart so assembly can wrap this primitive as Pullback(SE, chart). Sexpat itself stays the inverse-distance kernel.
void setGeodesicHeatReferencePath(const Coord& reference_path, double covariance_weight)
Attach a geodesic heat summand to the selected pair or chart base. A zero covariance weight disables it.
void setGeodesicHeatKernel(std::shared_ptr<const GeodesicHeatEnergyForceKernel> kernel)
Attach an immutable heat component with its own reference and metric.
void rescaleGeodesicHeatCovariance(double factor)
Convert the additive component to a different output variance unit.
void setParameters(const Eigen::VectorXd& w)
Set parameters of this covariance function using the provided vector.
Parameters:
w |
Vector of combined parameters from covariance functions and likelyhood |
FieldMatrixd& getAtomisticLengthScaleRef()
Pair-type metric retained independently of the chart’s fitted length.
std::shared_ptr<const KernelOperator> assembledOperator() const
The configured base and optional additive heat component.
void setPriorParametersLogNormal(const PriorBase& prior)
Parameters of the optional log-normal prior on magnSigma2. Inactive until setUseLogNormalMagnPrior(true).
void setUseLogNormalMagnPrior(bool enabled)
Opt-in log-normal prior on the positive signal variance magnSigma2. Off (the default) keeps the half-Student-t magnitude prior on classic profiles and the half-Cauchy magnitude prior in the MAP objective.
void setLengthScalePriorWeights(const Eigen::VectorXd& weights)
Enable a hierarchical-Bayes Gaussian prior on the log-space kernel parameters: ~log(theta_i) ~ N(mu_log, s2_log * I)~ where ~mu_log~ is the packed log-parameter target (~[log(magnSigma2), log(lengthScale[0]), …, log(lengthScale[K-1])]~, same layout as ~combineParameters()~) and ~s2_log~ is the shared variance. When enabled, ~calculateLogPrior~ / ~calculateLogPriorGradient~ use this hierarchical density instead of the global-GP zero-prior branch. The density operates in log-space directly, so no linear-to-log Jacobian terms are added (the SCG optimiser already runs on the packed log-parameters returned by ~combineParameters~).
Per-image GP fits use ~mu_log = theta_global~ from the session GP’s MAP and a shared ~s2_log = tau^2~ regulariser that pulls per-image ~theta_i~ back toward the global MAP when the per-image data set is small. Per-length-scale weights of the prior terms; empty restores one prior per length scale.
void setHierarchicalPrior(const Eigen::VectorXd& mu_log, double s2_log, const Eigen::VectorXd& s2_log_per)
The same prior with a variance per packed parameter (the shared ~s2_log~ where an entry is not positive): the population prior of a pair type sets its own width.
void setHierarchicalAnchorMask(std::vector<char> mask)
Length scales (index into the length-scale block) the population prior anchors: under the classic joint energy/force prior of the gprd profiles these take the per-type log-Gaussian in place of the classic term; the others keep the classic term.
double hierarchicalPriorVariance(Eigen::Index n) const
Variance of packed parameter ~n~ under the hierarchical prior.
void clearHierarchicalPrior()
Disable the hierarchical prior set via ~setHierarchicalPrior~. Restores the global-GP zero-prior branch.
void setPcLengthScalePrior(const Eigen::VectorXd& lambda)
Penalized-Complexity length-scale prior for the global GP. lambda is the per-pairtype PC rate (lambda_k = -rho0_k * ln(alpha)); element k applies to lengthScale[k]. magnSigma2 is left unpenalised.
Eigen::VectorXd combineParameters()
Combine parameters of the covariance function into one Eigen vector.
double calculateDirectionalLogPriorCurvature(const Eigen::VectorXd& direction) const
Evaluate the second directional derivative of log p(theta) along the packed log-parameter direction.
Pre-compute and cache inverse distances for the given positions.
Call once before an optimization loop. Subsequent covariance matrix evaluations will use the cached 1/r values, avoiding redundant coordinate extractions and norm computations.
void clearDistanceCache()
Invalidate the distance cache.
Call when positions change (e.g. after optimization completes and new observations are added).