14 #define ROL_UNUSED(x) (void) x
20 #include "ROL_Elementwise_Function.hpp"
22 #include "ROL_Ptr.hpp"
48 :
public std::enable_shared_from_this<Vector<Real>>
75 virtual void scale(
const Real alpha ) = 0;
85 virtual Real
dot(
const Vector &x )
const = 0;
94 virtual Real
norm()
const = 0;
105 virtual ROL::Ptr<Vector>
clone()
const = 0;
120 ROL::Ptr<Vector> ax = x.
clone();
134 this->
scale( (Real)0 );
148 virtual ROL::Ptr<Vector>
basis(
const int i )
const {
208 virtual void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
210 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
211 "The method applyUnary was called, but not implemented" << std::endl);
217 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
218 "The method applyBinary was called, but not implemented" << std::endl);
221 virtual Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
223 ROL_TEST_FOR_EXCEPTION(
true, std::logic_error,
224 "The method reduce was called, but not implemented" << std::endl);
227 virtual void print( std::ostream &outStream )
const {
228 outStream <<
"The method print was called, but not implemented" << std::endl;
258 virtual void randomize(
const Real l = 0.0,
const Real u = 1.0 ) {
259 Elementwise::UniformlyRandom<Real> ur(l,u);
293 const bool printToStream =
true,
294 std::ostream & outStream = std::cout )
const {
300 std::vector<Real> vCheck;
304 ROL::Ptr<std::ostream> pStream;
306 pStream = ROL::makePtrFromRef(outStream);
308 pStream = ROL::makePtrFromRef(bhs);
313 oldFormatState.copyfmt(*pStream);
315 ROL::Ptr<Vector> v = this->
clone();
316 ROL::Ptr<Vector> vtmp = this->
clone();
317 ROL::Ptr<Vector> xtmp = x.
clone();
318 ROL::Ptr<Vector> ytmp = y.
clone();
320 *pStream <<
"\n" << std::setw(width) << std::left << std::setfill(
'*') <<
"********** Begin verification of linear algebra. " <<
"\n\n";
321 headerFormatState.copyfmt(*pStream);
324 v->set(*
this); xtmp->set(x); ytmp->set(y);
325 v->plus(x); xtmp->plus(*
this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
326 *pStream << std::scientific << std::setprecision(12) << std::setfill('>
');
327 *pStream << std::setw(width) << std::left << "Commutativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
329 // Associativity of addition.
330 v->set(*this); xtmp->set(x); ytmp->set(y);
331 ytmp->plus(x); v->plus(*ytmp); xtmp->plus(*this); xtmp->plus(y); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
332 *pStream << std::setw(width) << std::left << "Associativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
334 // Identity element of addition.
335 v->set(*this); xtmp->set(x); ytmp->set(y);
336 v->zero(); v->plus(x); v->axpy(-one, x); vCheck.push_back(v->norm());
337 *pStream << std::setw(width) << std::left << "Identity element of addition. Consistency error: " << " " << vCheck.back() << "\n";
339 // Inverse elements of addition.
340 v->set(*this); xtmp->set(x); ytmp->set(y);
341 v->scale(-one); v->plus(*this); vCheck.push_back(v->norm());
342 *pStream << std::setw(width) << std::left << "Inverse elements of addition. Consistency error: " << " " << vCheck.back() << "\n";
344 // Identity element of scalar multiplication.
345 v->set(*this); xtmp->set(x); ytmp->set(y);
346 v->scale(one); v->axpy(-one, *this); vCheck.push_back(v->norm());
347 *pStream << std::setw(width) << std::left << "Identity element of scalar multiplication. Consistency error: " << " " << vCheck.back() << "\n";
349 // Consistency of scalar multiplication with field multiplication.
350 v->set(*this); vtmp->set(*this);
351 v->scale(b); v->scale(a); vtmp->scale(a*b); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
352 *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication with field multiplication. Consistency error: " << " " << vCheck.back() << "\n";
354 // Distributivity of scalar multiplication with respect to field addition.
355 v->set(*this); vtmp->set(*this);
356 v->scale(a+b); vtmp->scale(a); vtmp->axpy(b, *this); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
357 *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to field addition. Consistency error: " << " " << vCheck.back() << "\n";
359 // Distributivity of scalar multiplication with respect to vector addition.
360 v->set(*this); xtmp->set(x); ytmp->set(y);
361 v->plus(x); v->scale(a); xtmp->scale(a); xtmp->axpy(a, *this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
362 *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to vector addition. Consistency error: " << " " << vCheck.back() << "\n";
364 // Commutativity of dot (inner) product over the field of reals.
365 vCheck.push_back(std::abs(this->dot(x) - x.dot(*this)));
366 *pStream << std::setw(width) << std::left << "Commutativity of dot (inner) product over the field of reals. Consistency error: " << " " << vCheck.back() << "\n";
368 // Additivity of dot (inner) product.
370 xtmp->plus(y); vCheck.push_back(std::abs(this->dot(*xtmp) - this->dot(x) - this->dot(y))/std::max({static_cast<Real>(std::abs(this->dot(*xtmp))), static_cast<Real>(std::abs(this->dot(x))), static_cast<Real>(std::abs(this->dot(y))), one}));
371 *pStream << std::setw(width) << std::left << "Additivity of dot (inner) product. Consistency error: " << " " << vCheck.back() << "\n";
373 // Consistency of scalar multiplication and norm.
375 Real vnorm = v->norm();
378 vCheck.push_back(std::abs(v->norm() - zero));
381 vCheck.push_back(std::abs(v->norm() - one));
383 *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication and norm. Consistency error: " << " " << vCheck.back() << "\n";
387 xtmp = ROL::constPtrCast<Vector>(ROL::makePtrFromRef(this->dual()));
388 ytmp = ROL::constPtrCast<Vector>(ROL::makePtrFromRef(xtmp->dual()));
389 v->axpy(-one, *ytmp); vCheck.push_back(v->norm());
390 *pStream << std::setw(width) << std::left << "Reflexivity. Consistency error: " << " " << vCheck.back() << "\n";
392 // Consistency of apply and dual.
394 xtmp = x.dual().clone(); xtmp->set(x.dual());
395 Real vx = v->apply(*xtmp);
396 Real vxd = v->dot(xtmp->dual());
397 Real vdx = xtmp->dot(v->dual());
399 vCheck.push_back(std::max(std::abs(vx-vxd),std::abs(vx-vdx)));
402 vCheck.push_back(std::max(std::abs(vx-vxd),std::abs(vx-vdx))/std::abs(vx));
404 *pStream << std::setw(width) << std::left << "Consistency of apply and dual:" << " " << vCheck.back() << "\n\n";
406 //*pStream << "************ End verification of linear algebra.\n\n";
408 // Restore format state of pStream used for the header info.
409 pStream->copyfmt(headerFormatState);
410 *pStream << std::setw(width) << std::left << "********** End verification of linear algebra. " << "\n\n";
412 // Restore format state of the original pStream.
413 pStream->copyfmt(oldFormatState);
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
virtual void plus(const Vector &x)=0
Compute , where .
virtual void print(std::ostream &outStream) const
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
virtual Real reduce(const Elementwise::ReductionOp< Real > &r) const
virtual void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector &x)
virtual void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
virtual std::vector< Real > checkVector(const Vector< Real > &x, const Vector< Real > &y, const bool printToStream=true, std::ostream &outStream=std::cout) const
Verify vector-space methods.
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
virtual Real dot(const Vector &x) const =0
Compute where .
virtual void setScalar(const Real C)
Set where .
virtual void applyUnary(const Elementwise::UnaryFunction< Real > &f)
basic_nullstream< char, char_traits< char >> nullstream
virtual void set(const Vector &x)
Set where .
virtual Real norm() const =0
Returns where .