9 #ifndef CUBBYFLOW_CG_IMPL_H 10 #define CUBBYFLOW_CG_IMPL_H 16 template <
typename BLASType>
18 const typename BLASType::MatrixType& A,
19 const typename BLASType::VectorType& b,
20 unsigned int maxNumberOfIterations,
22 typename BLASType::VectorType* x,
23 typename BLASType::VectorType* r,
24 typename BLASType::VectorType* d,
25 typename BLASType::VectorType* q,
26 typename BLASType::VectorType* s,
27 unsigned int* lastNumberOfIterations,
28 double* lastResidualNorm)
33 PCG<BLASType, PrecondType>(
36 maxNumberOfIterations,
44 lastNumberOfIterations,
48 template <
typename BLASType,
typename PrecondType>
50 const typename BLASType::MatrixType& A,
51 const typename BLASType::VectorType& b,
52 unsigned int maxNumberOfIterations,
55 typename BLASType::VectorType* x,
56 typename BLASType::VectorType* r,
57 typename BLASType::VectorType* d,
58 typename BLASType::VectorType* q,
59 typename BLASType::VectorType* s,
60 unsigned int* lastNumberOfIterations,
61 double* lastResidualNorm)
70 BLASType::Residual(A, *x, b, r);
76 double sigmaNew = BLASType::Dot(*r, *d);
78 unsigned int iter = 0;
81 while (sigmaNew >
Square(tolerance) && iter < maxNumberOfIterations)
84 BLASType::MVM(A, *d, q);
87 double alpha = sigmaNew / BLASType::Dot(*d, *q);
90 BLASType::AXPlusY(alpha, *d, *x, x);
93 if (trigger || (iter % 50 == 0 && iter > 0))
96 BLASType::Residual(A, *x, b, r);
102 BLASType::AXPlusY(-alpha, *q, *r, r);
109 double sigmaOld = sigmaNew;
112 sigmaNew = BLASType::Dot(*r, *s);
114 if (sigmaNew > sigmaOld)
120 double beta = sigmaNew / sigmaOld;
123 BLASType::AXPlusY(beta, *d, *s, d);
128 *lastNumberOfIterations = iter;
131 *lastResidualNorm = std::sqrt(std::fabs(sigmaNew));
T Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.h:111
Definition: pybind11Utils.h:24
No-op pre-conditioner for conjugate gradient.
Definition: CG.h:23
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.h:17
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.h:49