GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
CovarianceEquilibration.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
4
#ifndef GPR_LINALG_COVARIANCE_EQUILIBRATION_H_
5
#define GPR_LINALG_COVARIANCE_EQUILIBRATION_H_
6
7
#include <Eigen/Core>
8
#include <cmath>
9
#include <vector>
10
11
namespace
gpr::linalg
{
12
16
class
CovarianceEquilibration
{
17
public
:
18
template
<
typename
Derived>
19
bool
compute
(
const
Eigen::MatrixBase<Derived>& diagonal)
20
{
21
powers_.resize(
static_cast<
std::size_t
>
(diagonal.size()));
22
scales_.resize(powers_.size());
23
inverse_scales_.resize(powers_.size());
24
bounded_products_ =
true
;
25
for
(Eigen::Index i = 0; i < diagonal.size(); ++i) {
26
const
double
value = diagonal(i);
27
if
(!(value > 0.0) || !std::isfinite(value))
return
false
;
28
const
int
exponent = std::ilogb(value);
29
// Integer division rounds toward zero; negative exponents need
30
// floor.
31
const
int
half =
32
exponent >= 0 ? exponent / 2 : -((-exponent + 1) / 2);
33
const
auto
row =
static_cast<
std::size_t
>
(i);
34
powers_[row] = -half;
35
scales_[row] = std::scalbn(1.0, -half);
36
inverse_scales_[row] = std::scalbn(1.0, half);
37
bounded_products_ = bounded_products_ && std::abs(half) <= 511;
38
}
39
return
true
;
40
}
41
42
[[nodiscard]]
double
covariance
(
double
value, Eigen::Index row,
43
Eigen::Index column)
const
noexcept
44
{
45
// Normal diagonals give representable, exact products of binary scales.
46
if
(bounded_products_)
47
return
value * (scales_[
static_cast<
std::size_t
>
(row)] *
48
scales_[
static_cast<
std::size_t
>
(column)]);
49
// Subnormal diagonals can overflow W_ii * W_jj; shift only once.
50
return
std::scalbn(value, power(row) + power(column));
51
}
52
53
[[nodiscard]]
double
physicalFactor
(
double
value,
54
Eigen::Index row)
const
noexcept
55
{
56
return
value * inverse_scales_[
static_cast<
std::size_t
>
(row)];
57
}
58
59
template
<
typename
Derived>
60
void
scaleLower
(Eigen::MatrixBase<Derived>& matrix)
const
61
{
62
for
(Eigen::Index j = 0; j < matrix.cols(); ++j)
63
for
(Eigen::Index i = j; i < matrix.rows(); ++i)
64
matrix(i, j) =
covariance
(matrix(i, j), i, j);
65
}
66
67
template
<
typename
Derived>
68
void
restoreFactor
(Eigen::MatrixBase<Derived>& factor)
const
69
{
70
for
(Eigen::Index j = 0; j < factor.cols(); ++j)
71
for
(Eigen::Index i = j; i < factor.rows(); ++i)
72
factor(i, j) =
physicalFactor
(factor(i, j), i);
73
}
74
75
private
:
76
[[nodiscard]]
int
power(Eigen::Index row)
const
noexcept
77
{
78
return
powers_[
static_cast<
std::size_t
>
(row)];
79
}
80
std::vector<int> powers_;
81
std::vector<double> scales_;
82
std::vector<double> inverse_scales_;
83
bool
bounded_products_ =
true
;
84
};
85
86
}
// namespace gpr::linalg
87
88
#endif
// GPR_LINALG_COVARIANCE_EQUILIBRATION_H_
gpr::linalg::CovarianceEquilibration
Definition
CovarianceEquilibration.h:16
gpr::linalg::CovarianceEquilibration::compute
bool compute(const Eigen::MatrixBase< Derived > &diagonal)
Definition
CovarianceEquilibration.h:19
gpr::linalg::CovarianceEquilibration::scaleLower
void scaleLower(Eigen::MatrixBase< Derived > &matrix) const
Definition
CovarianceEquilibration.h:60
gpr::linalg::CovarianceEquilibration::restoreFactor
void restoreFactor(Eigen::MatrixBase< Derived > &factor) const
Definition
CovarianceEquilibration.h:68
gpr::linalg::CovarianceEquilibration::physicalFactor
double physicalFactor(double value, Eigen::Index row) const noexcept
Definition
CovarianceEquilibration.h:53
gpr::linalg::CovarianceEquilibration::covariance
double covariance(double value, Eigen::Index row, Eigen::Index column) const noexcept
Definition
CovarianceEquilibration.h:42
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr
linalg
CovarianceEquilibration.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf