11 #ifndef CUBBYFLOW_CG_IMPL_HPP 12 #define CUBBYFLOW_CG_IMPL_HPP 18 template <
typename BLASType>
19 void CG(
const typename BLASType::MatrixType& A,
20 const typename BLASType::VectorType& b,
21 unsigned int maxNumberOfIterations,
double tolerance,
22 typename BLASType::VectorType* x,
typename BLASType::VectorType* r,
23 typename BLASType::VectorType* d,
typename BLASType::VectorType* q,
24 typename BLASType::VectorType* s,
unsigned int* lastNumberOfIterations,
25 double* lastResidualNorm)
30 PCG<BLASType, PrecondType>(A, b, maxNumberOfIterations, tolerance, &precond,
31 x, r, d, q, s, lastNumberOfIterations,
35 template <
typename BLASType,
typename PrecondType>
36 void PCG(
const typename BLASType::MatrixType& A,
37 const typename BLASType::VectorType& b,
38 unsigned int maxNumberOfIterations,
double tolerance, PrecondType* M,
39 typename BLASType::VectorType* x,
typename BLASType::VectorType* r,
40 typename BLASType::VectorType* d,
typename BLASType::VectorType* q,
41 typename BLASType::VectorType* s,
unsigned int* lastNumberOfIterations,
42 double* lastResidualNorm)
51 BLASType::Residual(A, *x, b, r);
57 double sigmaNew = BLASType::Dot(*r, *d);
59 unsigned int iter = 0;
62 while (sigmaNew >
Square(tolerance) && iter < maxNumberOfIterations)
65 BLASType::MVM(A, *d, q);
68 double alpha = sigmaNew / BLASType::Dot(*d, *q);
71 BLASType::AXPlusY(alpha, *d, *x, x);
74 if (trigger || (iter % 50 == 0 && iter > 0))
77 BLASType::Residual(A, *x, b, r);
83 BLASType::AXPlusY(-alpha, *q, *r, r);
90 const double sigmaOld = sigmaNew;
93 sigmaNew = BLASType::Dot(*r, *s);
95 if (sigmaNew > sigmaOld)
101 double beta = sigmaNew / sigmaOld;
104 BLASType::AXPlusY(beta, *d, *s, d);
109 *lastNumberOfIterations = iter;
112 *lastResidualNorm = std::sqrt(std::fabs(sigmaNew));
std::enable_if_t< std::is_arithmetic< T >::value, T > Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.hpp:154
Definition: pybind11Utils.hpp:20
No-op pre-conditioner for conjugate gradient.
Definition: CG.hpp:25
void CG(const typename BLASType::MatrixType &A, const typename BLASType::VectorType &b, unsigned int maxNumberOfIterations, double tolerance, typename BLASType::VectorType *x, typename BLASType::VectorType *r, typename BLASType::VectorType *d, typename BLASType::VectorType *q, typename BLASType::VectorType *s, unsigned int *lastNumberOfIterations, double *lastResidualNorm)
Solves conjugate gradient.
Definition: CG-Impl.hpp:19
void PCG(const typename BLASType::MatrixType &A, const typename BLASType::VectorType &b, unsigned int maxNumberOfIterations, double tolerance, PrecondType *M, typename BLASType::VectorType *x, typename BLASType::VectorType *r, typename BLASType::VectorType *d, typename BLASType::VectorType *q, typename BLASType::VectorType *s, unsigned int *lastNumberOfIterations, double *lastResidualNorm)
Solves pre-conditioned conjugate gradient.
Definition: CG-Impl.hpp:36