Loading...
Searching...
No Matches
DenseLLTInverse.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "DenseLLTInverse.h"
6
7#include <stdexcept>
8
10
11namespace gpr {
12namespace linalg {
13namespace {
14// LLT blocking follows Eigen::nbThreads. One thread is the product the
15// compact posterior compares against; a larger team is a different sum.
16using SerialEigenFactor = ScopedSerialEigenThreads;
17} // namespace
18
20{
21 [[maybe_unused]] const SerialEigenFactor serial_factor;
22 llt_.compute(K);
23 factored_ = (llt_.info() == Eigen::Success);
24 return factored_;
25}
26
27Eigen::VectorXd DenseLLTInverse::solve(const Eigen::VectorXd& b) const
28{
29 if (!factored_) {
30 throw std::logic_error(
31 "DenseLLTInverse: solve called before factorise");
32 }
33 [[maybe_unused]] const SerialEigenFactor serial_factor;
34 return llt_.solve(b);
35}
36
38{
39 if (!factored_) {
40 throw std::logic_error(
41 "DenseLLTInverse: solve_mat called before factorise");
42 }
43 [[maybe_unused]] const SerialEigenFactor serial_factor;
44 return llt_.solve(B);
45}
46
48{
49 if (!factored_) {
50 throw std::logic_error(
51 "DenseLLTInverse: log_det called before factorise");
52 }
53 // log |K| = log |L L^T| = 2 sum_i log L_ii. matrixLLT() returns the
54 // packed in-place LLT storage (lower triangle holds L); .diagonal()
55 // is a non-allocating view of L's diagonal entries. Cost is O(n)
56 // reads, no allocation. Numerically identical to reading the
57 // diagonal of the materialised lower factor.
58 return 2.0 * llt_.matrixLLT().diagonal().array().log().sum();
59}
60
62{
63 if (!factored_) {
64 throw std::logic_error(
65 "DenseLLTInverse: trace_inv_times called before factorise");
66 }
67 // tr(K^{-1} A) = tr(L^{-T} L^{-1} A). Solve K Z = A then sum the
68 // diagonal of Z. The two-step trsm route avoids materialising
69 // K^{-1} explicitly, so the cost is one O(n^2) solve per RHS column
70 // times n columns = O(n^3) total -- same as forming K^{-1} A but
71 // without the upper-triangle pollution of dpotri.
72 [[maybe_unused]] const SerialEigenFactor serial_factor;
73 const EigenMatrix Z = llt_.solve(A);
74 return Z.trace();
75}
76
77} // namespace linalg
78} // namespace gpr
Rank-local Eigen LLT implementation of KernelInverse.
double trace_inv_times(const EigenMatrix &A) const override
EigenMatrix solve_mat(const EigenMatrix &B) const override
Solve K X = B for a multi right-hand side.
Eigen::VectorXd solve(const Eigen::VectorXd &b) const override
Solve K x = b for a single right-hand side.
bool factorise(const EigenMatrix &K)
double log_det() const override
Returns the log determinant of K.
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20