Loading...
Searching...
No Matches
CusolverInverse.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 "CusolverInverse.h"
6
7#include <stdexcept>
8
9namespace gpr {
10namespace linalg {
11
13{
14 factored_ = cholesky(K, L_);
15 return factored_;
16}
17
18Eigen::VectorXd CusolverInverse::solve(const Eigen::VectorXd& b) const
19{
20 if (!factored_) {
21 throw std::logic_error(
22 "CusolverInverse: solve called before factorise");
23 }
24 // K x = b with K = L L^T: L y = b (forward), L^T x = y (back).
25 return back_solve(L_, forward_solve(L_, b));
26}
27
29{
30 if (!factored_) {
31 throw std::logic_error(
32 "CusolverInverse: solve_mat called before factorise");
33 }
34 return back_solve_mat(L_, forward_solve_mat(L_, B));
35}
36
38{
39 if (!factored_) {
40 throw std::logic_error(
41 "CusolverInverse: log_det called before factorise");
42 }
43 // log|K| = 2 sum_i log L_ii; log_det_cholesky returns sum_i log L_ii.
44 return 2.0 * log_det_cholesky(L_);
45}
46
48{
49 if (!factored_) {
50 throw std::logic_error(
51 "CusolverInverse: trace_inv_times called before factorise");
52 }
53 // tr(K^{-1} A) = tr(L^{-T} L^{-1} A). Solve K Z = A column-wise, sum
54 // the diagonal -- avoids forming K^{-1} explicitly, matching the
55 // DenseLLTInverse contract (A treated as general dense).
56 return solve_mat(A).trace();
57}
58
59} // namespace linalg
60} // namespace gpr
KernelInverse backed by the compile-time linalg backend free functions (Backend.h): under -Dlinalg_ba...
bool factorise(const EigenMatrix &K)
double trace_inv_times(const EigenMatrix &A) const override
Eigen::VectorXd solve(const Eigen::VectorXd &b) const override
Solve K x = b for a single right-hand side.
double log_det() const override
Returns the log determinant of K.
EigenMatrix solve_mat(const EigenMatrix &B) const override
Solve K X = B for a multi right-hand side.
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
EigenMatrix back_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Back triangular solve with matrix RHS (BLAS equivalent: dtrsm with side=L, uplo=L,...
Eigen::VectorXd forward_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
Forward triangular solve with vector RHS (BLAS equivalent: dtrsv).
EigenMatrix forward_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Forward triangular solve with matrix RHS (BLAS equivalent: dtrsm).
double log_det_cholesky(const EigenMatrix &L)
Half log-determinant from Cholesky factor.
Eigen::VectorXd back_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
Back triangular solve with vector RHS (BLAS equivalent: dtrsv).
bool cholesky(const EigenMatrix &C, EigenMatrix &L)
Cholesky decomposition (LAPACK equivalent: dpotrf).
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