Loading...
Searching...
No Matches
gpr::quantum::SearchKktFactor< SparseBackend > Class Template Reference

#include <gpr/quantum/SearchKktFactor.h>

Public Member Functions

 SearchKktFactor (bool sparseEnabled=false, double tolerance=1e-8)
void clear ()
void setSparseEnabled (bool enabled)
void setTolerance (double tolerance)
bool sparseEnabled () const noexcept
bool usingSparse () const noexcept
std::size_t sparseFactorizations () const noexcept
std::size_t denseFallbacks () const noexcept
std::size_t residualChecks () const noexcept
double residualCheckSeconds () const noexcept
const std::string & fallbackReason () const noexcept
const M & matrix () const noexcept
const SearchSparseMatrix & sparseMatrix () const noexcept
const Eigen::PartialPivLU< M > & denseFactor () const
void compute (M matrix)
V solve (const V &rhs)

Detailed Description

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
class gpr::quantum::SearchKktFactor< SparseBackend >

Definition at line 27 of file SearchKktFactor.h.

Constructor & Destructor Documentation

◆ SearchKktFactor()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
gpr::quantum::SearchKktFactor< SparseBackend >::SearchKktFactor ( bool sparseEnabled = false,
double tolerance = 1e-8 )
inlineexplicit

Definition at line 80 of file SearchKktFactor.h.

82 : sparseEnabled_(sparseEnabled)
83 {
85 }
void setTolerance(double tolerance)
bool sparseEnabled() const noexcept

Member Function Documentation

◆ clear()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
void gpr::quantum::SearchKktFactor< SparseBackend >::clear ( )
inline

Definition at line 86 of file SearchKktFactor.h.

87 {
88 matrix_.resize(0, 0);
89 sparse_.resize(0, 0);
90 sparseFactor_.reset();
91 denseReady_ = false;
92 sparseActive_ = false;
93 fallbackReason_.clear();
94 }

◆ compute()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
void gpr::quantum::SearchKktFactor< SparseBackend >::compute ( M matrix)
inline

Definition at line 150 of file SearchKktFactor.h.

151 {
152 if (matrix.rows() == 0 || matrix.rows() != matrix.cols() ||
153 !matrix.allFinite())
155 "A finite nonempty square search matrix is required");
156 clear();
157 matrix_ = std::move(matrix);
158 if (!sparseEnabled_) {
159 prepareDense();
160 return;
161 }
162 if (matrix_.rows() > std::numeric_limits<int>::max()) {
163 fallback("Search matrix exceeds sparse index capacity");
164 return;
165 }
167 for (Eigen::Index column = 0; column < matrix_.cols(); ++column)
168 for (Eigen::Index row = 0; row < matrix_.rows(); ++row)
169 if (matrix_(row, column) != 0.)
170 entries.emplace_back(static_cast<int>(row),
171 static_cast<int>(column),
172 matrix_(row, column));
173 sparse_.resize(matrix_.rows(), matrix_.cols());
174 sparse_.setFromTriplets(entries.begin(), entries.end());
175 sparse_.makeCompressed();
176 sparseFactor_ = std::make_unique<SparseBackend>();
177 sparseFactor_->compute(sparse_);
178 ++sparseFactorizations_;
179 if (sparseFactor_->info() != Eigen::Success) {
180 fallback("Sparse search factorization failed");
181 return;
182 }
183 sparseActive_ = true;
184 }
const M & matrix() const noexcept

◆ denseFactor()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
const Eigen::PartialPivLU< M > & gpr::quantum::SearchKktFactor< SparseBackend >::denseFactor ( ) const
inline

Definition at line 145 of file SearchKktFactor.h.

146 {
147 prepareDense();
148 return dense_;
149 }

◆ denseFallbacks()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
std::size_t gpr::quantum::SearchKktFactor< SparseBackend >::denseFallbacks ( ) const
inlinenoexcept

Definition at line 121 of file SearchKktFactor.h.

122 {
123 return fallbacks_;
124 }

◆ fallbackReason()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
const std::string & gpr::quantum::SearchKktFactor< SparseBackend >::fallbackReason ( ) const
inlinenoexcept

Definition at line 133 of file SearchKktFactor.h.

134 {
135 return fallbackReason_;
136 }

◆ matrix()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
const M & gpr::quantum::SearchKktFactor< SparseBackend >::matrix ( ) const
inlinenoexcept

Definition at line 137 of file SearchKktFactor.h.

138 {
139 return matrix_;
140 }

◆ residualChecks()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
std::size_t gpr::quantum::SearchKktFactor< SparseBackend >::residualChecks ( ) const
inlinenoexcept

Definition at line 125 of file SearchKktFactor.h.

126 {
127 return residualChecks_;
128 }

◆ residualCheckSeconds()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
double gpr::quantum::SearchKktFactor< SparseBackend >::residualCheckSeconds ( ) const
inlinenoexcept

Definition at line 129 of file SearchKktFactor.h.

130 {
131 return residualCheckSeconds_;
132 }

◆ setSparseEnabled()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
void gpr::quantum::SearchKktFactor< SparseBackend >::setSparseEnabled ( bool enabled)
inline

Definition at line 95 of file SearchKktFactor.h.

96 {
97 if (enabled != sparseEnabled_) {
98 clear();
99 sparseEnabled_ = enabled;
100 }
101 }

◆ setTolerance()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
void gpr::quantum::SearchKktFactor< SparseBackend >::setTolerance ( double tolerance)
inline

Definition at line 102 of file SearchKktFactor.h.

103 {
104 if (!(tolerance > 0) || !std::isfinite(tolerance))
106 "A positive finite search equation tolerance is required");
107 tolerance_ = tolerance;
108 }

◆ solve()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
V gpr::quantum::SearchKktFactor< SparseBackend >::solve ( const V & rhs)
inline

Definition at line 185 of file SearchKktFactor.h.

186 {
187 requireMatrix();
188 if (rhs.size() != matrix_.rows() || !rhs.allFinite())
190 "A finite matching search right-hand side is required");
191 if (sparseActive_) {
192 V result = sparseFactor_->solve(rhs);
193 if (sparseFactor_->info() == Eigen::Success &&
194 verified(result, rhs))
195 return result;
196 fallback(
197 "Sparse search solution failed its original-matrix residual");
198 }
199 prepareDense();
200 V result = dense_.solve(rhs);
201 if (sparseEnabled_ && !verified(result, rhs))
202 throw std::runtime_error(
203 "Dense search fallback failed its original-matrix residual");
204 return result;
205 }

◆ sparseEnabled()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
bool gpr::quantum::SearchKktFactor< SparseBackend >::sparseEnabled ( ) const
inlinenoexcept

Definition at line 109 of file SearchKktFactor.h.

110 {
111 return sparseEnabled_;
112 }

◆ sparseFactorizations()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
std::size_t gpr::quantum::SearchKktFactor< SparseBackend >::sparseFactorizations ( ) const
inlinenoexcept

Definition at line 117 of file SearchKktFactor.h.

118 {
119 return sparseFactorizations_;
120 }

◆ sparseMatrix()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
const SearchSparseMatrix & gpr::quantum::SearchKktFactor< SparseBackend >::sparseMatrix ( ) const
inlinenoexcept

Definition at line 141 of file SearchKktFactor.h.

142 {
143 return sparse_;
144 }

◆ usingSparse()

template<class SparseBackend = Eigen::SparseLU<SearchSparseMatrix>>
bool gpr::quantum::SearchKktFactor< SparseBackend >::usingSparse ( ) const
inlinenoexcept

Definition at line 113 of file SearchKktFactor.h.

114 {
115 return sparseActive_;
116 }

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