#ifndef FLENS_EXAMPLES_CG_BLAS_H #define FLENS_EXAMPLES_CG_BLAS_H 1 #include #include template int cg(const MA &A, const VB &b, VX &&x, double tol = std::numeric_limits::epsilon(), int maxIterations = std::numeric_limits::max()) { using namespace flens; typedef typename VB::ElementType ElementType; typedef typename VB::IndexType IndexType; typedef typename VB::NoView VectorType; ElementType alpha, beta, rNormSquare, rNormSquarePrev; VectorType Ap, r, p; const ElementType Zero(0), One(1); blas::copy(b, r); blas::mv(NoTrans, -One, A, x, One, r); blas::copy(r, p); rNormSquare = blas::dot(r, r); for (int k=1; k<=maxIterations; ++k) { std::cout << "k = " << k << std::endl; if (sqrt(rNormSquare)<=tol) { return k-1; } blas::mv(NoTrans, One, A, p, Zero, Ap); alpha = rNormSquare/blas::dot(p, Ap); blas::axpy(alpha, p, x); blas::axpy(-alpha, Ap, r); rNormSquarePrev = rNormSquare; rNormSquare = blas::dot(r, r); beta = rNormSquare/rNormSquarePrev; blas::scal(beta, p); blas::axpy(One, r, p); } return maxIterations; } #endif // FLENS_EXAMPLES_CG_BLAS_H