本文整理汇总了C++中Thyra::V_V方法的典型用法代码示例。如果您正苦于以下问题:C++ Thyra::V_V方法的具体用法?C++ Thyra::V_V怎么用?C++ Thyra::V_V使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thyra
的用法示例。
在下文中一共展示了Thyra::V_V方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sillyCgSolve
bool sillyCgSolve(
const Thyra::LinearOpBase<Scalar> &A,
const Thyra::VectorBase<Scalar> &b,
const int maxNumIters,
const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance,
const Teuchos::Ptr<Thyra::VectorBase<Scalar> > &x,
std::ostream &out
)
{
// Create some typedefs and some other stuff to make the code cleaner
typedef Teuchos::ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
const Scalar one = ST::one(), zero = ST::zero(); using Teuchos::as;
using Teuchos::RCP; using Thyra::VectorSpaceBase; using Thyra::VectorBase;
using Thyra::NOTRANS; using Thyra::V_V; using Thyra::apply;
// Validate input
THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES("sillyCgSolve()", A, Thyra::NOTRANS, *x, &b);
Teuchos::EVerbosityLevel vl = Teuchos::VERB_MEDIUM;
out << "\nStarting CG solver ...\n" << std::scientific << "\ndescribe A:\n"<<describe(A, vl)
<< "\ndescribe b:\n"<<describe(b, vl)<<"\ndescribe x:\n"<<describe(*x, vl)<<"\n";
// Initialization
const RCP<const VectorSpaceBase<Scalar> > space = A.domain();
const RCP<VectorBase<Scalar> > r = createMember(space);
// r = -A*x + b
V_V(r.ptr(), b); apply<Scalar>(A, NOTRANS, *x, r.ptr(), -one, one);
const ScalarMag r0_nrm = norm(*r);
if (r0_nrm==zero) return true;
const RCP<VectorBase<Scalar> > p = createMember(space), q = createMember(space);
Scalar rho_old = -one;
// Perform the iterations
for( int iter = 0; iter <= maxNumIters; ++iter ) {
// Check convergence and output iteration
const ScalarMag r_nrm = norm(*r);
const bool isConverged = r_nrm/r0_nrm <= tolerance;
if( iter%(maxNumIters/10+1) == 0 || iter == maxNumIters || isConverged ) {
out << "Iter = " << iter << ", ||b-A*x||/||b-A*x0|| = " << (r_nrm/r0_nrm) << std::endl;
if( r_nrm/r0_nrm < tolerance ) return true; // Success!
}
// Compute iteration
const Scalar rho = inner(*r, *r); // <r,r> -> rho
if (iter==0) V_V(p.ptr(), *r); // r -> p (iter == 0)
else Vp_V( p.ptr(), *r, rho/rho_old ); // r+(rho/rho_old)*p -> p (iter > 0)
apply<Scalar>(A, NOTRANS, *p, q.ptr()); // A*p -> q
const Scalar alpha = rho/inner(*p, *q); // rho/<p,q> -> alpha
Vp_StV( x, +alpha, *p ); // +alpha*p + x -> x
Vp_StV( r.ptr(), -alpha, *q ); // -alpha*q + r -> r
rho_old = rho; // rho -> rho_old (for next iter)
}
return false; // Failure
} // end sillyCgSolve
示例2:
void DefaultPolyLineSearchPointEvaluator<Scalar>::computePoint( const ScalarMag &alpha,
const Ptr<Thyra::VectorBase<Scalar> > &p
) const
{
typedef ScalarTraits<Scalar> ST;
using Teuchos::as;
using Thyra::V_V;
using Thyra::Vp_StV;
V_V( p, *vecs_[0] );
if (alpha != ST::zero()) {
ScalarMag alpha_i = alpha;
const int n = vecs_.size();
for (int i = 1; i < n; ++i, alpha_i *= alpha) {
Vp_StV(p, alpha_i, *vecs_[i]);
}
}
}