ROL
ROL_Vector.hpp
Go to the documentation of this file.
1 
2 // @HEADER
3 // ************************************************************************
4 //
5 // Rapid Optimization Library (ROL) Package
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact lead developers:
39 // Drew Kouri (dpkouri@sandia.gov) and
40 // Denis Ridzal (dridzal@sandia.gov)
41 //
42 // ************************************************************************
43 // @HEADER
44 
45 #ifndef ROL_VECTOR_H
46 #define ROL_VECTOR_H
47 
48 #include "Teuchos_RefCountPtr.hpp"
49 #include "Teuchos_oblackholestream.hpp"
50 
69 namespace ROL {
70 
71 template <class Real>
72 class Vector {
73 public:
74 
75  virtual ~Vector() {}
76 
77 
86  virtual void plus( const Vector &x ) = 0;
87 
88 
97  virtual void scale( const Real alpha ) = 0;
98 
99 
107  virtual Real dot( const Vector &x ) const = 0;
108 
109 
116  virtual Real norm() const = 0;
117 
118 
127  virtual Teuchos::RCP<Vector> clone() const = 0;
128 
129 
141  virtual void axpy( const Real alpha, const Vector &x ) {
142  Teuchos::RCP<Vector> ax = x.clone();
143  ax->set(x);
144  ax->scale(alpha);
145  this->plus(*ax);
146  }
147 
155  virtual void zero() {
156  this->scale( (Real)0 );
157  }
158 
159 
170  virtual Teuchos::RCP<Vector> basis( const int i ) const {return Teuchos::null;}
171 
172 
181  virtual int dimension() const {return 0;}
182 
183 
194  virtual void set( const Vector &x ) {
195  this->zero();
196  this->plus(x);
197  }
198 
199 
211  virtual const Vector & dual() const {
212  return *this;
213  }
214 
242  virtual std::vector<Real> checkVector( const Vector<Real> &x,
243  const Vector<Real> &y,
244  const bool printToStream = true,
245  std::ostream & outStream = std::cout ) const {
246  Real zero = 0.0;
247  Real one = 1.0;
248  Real a = 1.234;
249  Real b = -432.1;
250  int width = 94;
251  std::vector<Real> vCheck;
252 
253  Teuchos::oblackholestream bhs; // outputs nothing
254 
255  Teuchos::RCP<std::ostream> pStream;
256  if (printToStream) {
257  pStream = Teuchos::rcp(&outStream, false);
258  } else {
259  pStream = Teuchos::rcp(&bhs, false);
260  }
261 
262  // Save the format state of the original pStream.
263  Teuchos::oblackholestream oldFormatState, headerFormatState;
264  oldFormatState.copyfmt(*pStream);
265 
266  Teuchos::RCP<Vector> v = this->clone();
267  Teuchos::RCP<Vector> vtmp = this->clone();
268  Teuchos::RCP<Vector> xtmp = x.clone();
269  Teuchos::RCP<Vector> ytmp = y.clone();
270 
271  //*pStream << "\n************ Begin verification of linear algebra.\n\n";
272  *pStream << "\n" << std::setw(width) << std::left << std::setfill('*') << "********** Begin verification of linear algebra. " << "\n\n";
273  headerFormatState.copyfmt(*pStream);
274 
275  // Commutativity of addition.
276  v->set(*this); xtmp->set(x); ytmp->set(y);
277  v->plus(x); xtmp->plus(*this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
278  *pStream << std::scientific << std::setprecision(12) << std::setfill('>');
279  *pStream << std::setw(width) << std::left << "Commutativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
280 
281  // Associativity of addition.
282  v->set(*this); xtmp->set(x); ytmp->set(y);
283  ytmp->plus(x); v->plus(*ytmp); xtmp->plus(*this); xtmp->plus(y); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
284  *pStream << std::setw(width) << std::left << "Associativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
285 
286  // Identity element of addition.
287  v->set(*this); xtmp->set(x); ytmp->set(y);
288  v->zero(); v->plus(x); v->axpy(-one, x); vCheck.push_back(v->norm());
289  *pStream << std::setw(width) << std::left << "Identity element of addition. Consistency error: " << " " << vCheck.back() << "\n";
290 
291  // Inverse elements of addition.
292  v->set(*this); xtmp->set(x); ytmp->set(y);
293  v->scale(-one); v->plus(*this); vCheck.push_back(v->norm());
294  *pStream << std::setw(width) << std::left << "Inverse elements of addition. Consistency error: " << " " << vCheck.back() << "\n";
295 
296  // Identity element of scalar multiplication.
297  v->set(*this); xtmp->set(x); ytmp->set(y);
298  v->scale(one); v->axpy(-one, *this); vCheck.push_back(v->norm());
299  *pStream << std::setw(width) << std::left << "Identity element of scalar multiplication. Consistency error: " << " " << vCheck.back() << "\n";
300 
301  // Consistency of scalar multiplication with field multiplication.
302  v->set(*this); vtmp->set(*this);
303  v->scale(b); v->scale(a); vtmp->scale(a*b); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
304  *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication with field multiplication. Consistency error: " << " " << vCheck.back() << "\n";
305 
306  // Distributivity of scalar multiplication with respect to field addition.
307  v->set(*this); vtmp->set(*this);
308  v->scale(a+b); vtmp->scale(a); vtmp->axpy(b, *this); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
309  *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to field addition. Consistency error: " << " " << vCheck.back() << "\n";
310 
311  // Distributivity of scalar multiplication with respect to vector addition.
312  v->set(*this); xtmp->set(x); ytmp->set(y);
313  v->plus(x); v->scale(a); xtmp->scale(a); xtmp->axpy(a, *this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
314  *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to vector addition. Consistency error: " << " " << vCheck.back() << "\n";
315 
316  // Commutativity of dot (inner) product over the field of reals.
317  vCheck.push_back(std::abs(this->dot(x) - x.dot(*this)));
318  *pStream << std::setw(width) << std::left << "Commutativity of dot (inner) product over the field of reals. Consistency error: " << " " << vCheck.back() << "\n";
319 
320  // Additivity of dot (inner) product.
321  xtmp->set(x);
322  xtmp->plus(y); vCheck.push_back(std::abs(this->dot(*xtmp) - x.dot(*this) - y.dot(*this)));
323  *pStream << std::setw(width) << std::left << "Additivity of dot (inner) product. Consistency error: " << " " << vCheck.back() << "\n";
324 
325  // Consistency of scalar multiplication and norm.
326  v->set(*this);
327  Real vnorm = v->norm();
328  if (vnorm == zero) {
329  v->scale(a);
330  vCheck.push_back(std::abs(v->norm() - zero));
331  } else {
332  v->scale(one/vnorm);
333  vCheck.push_back(std::abs(v->norm() - one));
334  }
335  *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication and norm. Consistency error: " << " " << vCheck.back() << "\n";
336 
337  // Reflexivity.
338  v->set(*this);
339  xtmp = Teuchos::rcp_const_cast<Vector>(Teuchos::rcpFromRef(this->dual()));
340  ytmp = Teuchos::rcp_const_cast<Vector>(Teuchos::rcpFromRef(xtmp->dual()));
341  v->axpy(-one, *ytmp); vCheck.push_back(v->norm());
342  *pStream << std::setw(width) << std::left << "Reflexivity. Consistency error: " << " " << vCheck.back() << "\n\n";
343 
344  //*pStream << "************ End verification of linear algebra.\n\n";
345 
346  // Restore format state of pStream used for the header info.
347  pStream->copyfmt(headerFormatState);
348  *pStream << std::setw(width) << std::left << "********** End verification of linear algebra. " << "\n\n";
349 
350  // Restore format state of the original pStream.
351  pStream->copyfmt(oldFormatState);
352 
353  return vCheck;
354  }
355 
356 }; // class Vector
357 
358 } // namespace ROL
359 
360 #endif
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:211
virtual void scale(const Real alpha)=0
Compute where .
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:181
virtual void plus(const Vector &x)=0
Compute , where .
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
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.
Definition: ROL_Vector.hpp:242
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
virtual Real dot(const Vector &x) const =0
Compute where .
virtual ~Vector()
Definition: ROL_Vector.hpp:75
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
virtual Teuchos::RCP< Vector > basis(const int i) const
Return i-th basis vector.
Definition: ROL_Vector.hpp:170
virtual Real norm() const =0
Returns where .