42 #ifndef THYRA_SILLY_CG_SOLVE_HPP
43 #define THYRA_SILLY_CG_SOLVE_HPP
45 #include "Thyra_LinearOpBase.hpp"
46 #include "Thyra_VectorStdOps.hpp"
47 #include "Thyra_AssertOp.hpp"
61 template<
class Scalar>
65 const int maxNumIters,
83 std::ios::fmtflags fmt(out.flags());
85 out <<
"\nStarting CG solver ...\n" << std::scientific <<
"\ndescribe A:\n"<<describe(A, vl)
86 <<
"\ndescribe b:\n"<<describe(b, vl)<<
"\ndescribe x:\n"<<describe(*x, vl)<<
"\n";
89 const RCP<const VectorSpaceBase<Scalar> > space = A.
domain();
92 V_V(r.ptr(), b); apply<Scalar>(A,
NOTRANS, *x, r.ptr(), -one, one);
93 const ScalarMag r0_nrm =
norm(*r);
94 if (r0_nrm==
zero)
return true;
96 Scalar rho_old = -one;
99 for(
int iter = 0; iter <= maxNumIters; ++iter ) {
102 const ScalarMag r_nrm =
norm(*r);
103 const bool isConverged = r_nrm/r0_nrm <= tolerance;
104 if( iter%(maxNumIters/10+1) == 0 || iter == maxNumIters || isConverged ) {
105 out <<
"Iter = " << iter <<
", ||b-A*x||/||b-A*x0|| = " << (r_nrm/r0_nrm) << std::endl;
106 if( r_nrm/r0_nrm < tolerance ) {
113 const Scalar rho =
inner(*r, *r);
114 if (iter==0)
V_V(p.ptr(), *r);
115 else Vp_V( p.ptr(), *r, rho/rho_old );
116 apply<Scalar>(A,
NOTRANS, *p, q.ptr());
117 const Scalar alpha = rho/
inner(*p, *q);
119 Vp_StV( r.ptr(), -alpha, *q );
128 #endif // THYRA_SILLY_CG_SOLVE_HPP
void Vp_V(const Ptr< MultiVectorBase< Scalar > > &Z, const MultiVectorBase< Scalar > &X)
Z(i,j) += X(i,j), i = 0...Z->range()->dim()-1, j = 0...Z->domain()->dim()-1.
Use the non-transposed operator.
RCP< const LinearOpBase< Scalar > > zero(const RCP< const VectorSpaceBase< Scalar > > &range, const RCP< const VectorSpaceBase< Scalar > > &domain)
Create a zero linear operator with given range and domain spaces.
Abstract interface for objects that represent a space for vectors.
void V_V(const Ptr< VectorBase< Scalar > > &y, const VectorBase< Scalar > &x)
y(i) = x(i), i = 0...y->space()->dim()-1.
RCP< VectorBase< Scalar > > createMember(const RCP< const VectorSpaceBase< Scalar > > &vs, const std::string &label="")
Create a vector member from the vector space.
Abstract interface for finite-dimensional dense vectors.
Base class for all linear operators.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
TypeTo as(const TypeFrom &t)
#define THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES(FUNC_NAME, M, M_T, X, Y)
This is a very useful macro that should be used to validate that the spaces for the vector version of...
void Vp_StV(const Ptr< VectorBase< Scalar > > &y, const Scalar &alpha, const VectorBase< Scalar > &x)
AXPY: y(i) = alpha * x(i) + y(i), i = 0...y->space()->dim()-1.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm(const VectorBase< Scalar > &v)
Natural norm: result = sqrt(<v,v>).
Scalar inner(const VectorBase< Scalar > &x, const VectorBase< Scalar > &y)
Inner/Scalar product result = <x,y>.