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 "ROL_Elementwise_Function.hpp"
49 
50 #include "Teuchos_RefCountPtr.hpp"
51 #include "Teuchos_oblackholestream.hpp"
52 
71 namespace ROL {
72 
73 template <class Real>
74 class Vector {
75 public:
76 
77  virtual ~Vector() {}
78 
79 
88  virtual void plus( const Vector &x ) = 0;
89 
90 
99  virtual void scale( const Real alpha ) = 0;
100 
101 
109  virtual Real dot( const Vector &x ) const = 0;
110 
111 
118  virtual Real norm() const = 0;
119 
120 
129  virtual Teuchos::RCP<Vector> clone() const = 0;
130 
131 
143  virtual void axpy( const Real alpha, const Vector &x ) {
144  Teuchos::RCP<Vector> ax = x.clone();
145  ax->set(x);
146  ax->scale(alpha);
147  this->plus(*ax);
148  }
149 
157  virtual void zero() {
158  this->scale( (Real)0 );
159  }
160 
161 
172  virtual Teuchos::RCP<Vector> basis( const int i ) const {return Teuchos::null;}
173 
174 
183  virtual int dimension() const {return 0;}
184 
185 
196  virtual void set( const Vector &x ) {
197  this->zero();
198  this->plus(x);
199  }
200 
201 
213  virtual const Vector & dual() const {
214  return *this;
215  }
216 
217  virtual void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
218  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
219  "The method applyUnary is called but not implemented" << std::endl);
220  }
221 
222  virtual void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector &x ) {
223  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
224  "The method applyBinary is called but not implemented" << std::endl);
225  }
226 
227  virtual Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
228  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
229  "The method reduce is called but not implemented" << std::endl);
230  }
231 
259  virtual std::vector<Real> checkVector( const Vector<Real> &x,
260  const Vector<Real> &y,
261  const bool printToStream = true,
262  std::ostream & outStream = std::cout ) const {
263  Real zero = 0.0;
264  Real one = 1.0;
265  Real a = 1.234;
266  Real b = -432.1;
267  int width = 94;
268  std::vector<Real> vCheck;
269 
270  Teuchos::oblackholestream bhs; // outputs nothing
271 
272  Teuchos::RCP<std::ostream> pStream;
273  if (printToStream) {
274  pStream = Teuchos::rcp(&outStream, false);
275  } else {
276  pStream = Teuchos::rcp(&bhs, false);
277  }
278 
279  // Save the format state of the original pStream.
280  Teuchos::oblackholestream oldFormatState, headerFormatState;
281  oldFormatState.copyfmt(*pStream);
282 
283  Teuchos::RCP<Vector> v = this->clone();
284  Teuchos::RCP<Vector> vtmp = this->clone();
285  Teuchos::RCP<Vector> xtmp = x.clone();
286  Teuchos::RCP<Vector> ytmp = y.clone();
287 
288  //*pStream << "\n************ Begin verification of linear algebra.\n\n";
289  *pStream << "\n" << std::setw(width) << std::left << std::setfill('*') << "********** Begin verification of linear algebra. " << "\n\n";
290  headerFormatState.copyfmt(*pStream);
291 
292  // Commutativity of addition.
293  v->set(*this); xtmp->set(x); ytmp->set(y);
294  v->plus(x); xtmp->plus(*this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
295  *pStream << std::scientific << std::setprecision(12) << std::setfill('>');
296  *pStream << std::setw(width) << std::left << "Commutativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
297 
298  // Associativity of addition.
299  v->set(*this); xtmp->set(x); ytmp->set(y);
300  ytmp->plus(x); v->plus(*ytmp); xtmp->plus(*this); xtmp->plus(y); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
301  *pStream << std::setw(width) << std::left << "Associativity of addition. Consistency error: " << " " << vCheck.back() << "\n";
302 
303  // Identity element of addition.
304  v->set(*this); xtmp->set(x); ytmp->set(y);
305  v->zero(); v->plus(x); v->axpy(-one, x); vCheck.push_back(v->norm());
306  *pStream << std::setw(width) << std::left << "Identity element of addition. Consistency error: " << " " << vCheck.back() << "\n";
307 
308  // Inverse elements of addition.
309  v->set(*this); xtmp->set(x); ytmp->set(y);
310  v->scale(-one); v->plus(*this); vCheck.push_back(v->norm());
311  *pStream << std::setw(width) << std::left << "Inverse elements of addition. Consistency error: " << " " << vCheck.back() << "\n";
312 
313  // Identity element of scalar multiplication.
314  v->set(*this); xtmp->set(x); ytmp->set(y);
315  v->scale(one); v->axpy(-one, *this); vCheck.push_back(v->norm());
316  *pStream << std::setw(width) << std::left << "Identity element of scalar multiplication. Consistency error: " << " " << vCheck.back() << "\n";
317 
318  // Consistency of scalar multiplication with field multiplication.
319  v->set(*this); vtmp->set(*this);
320  v->scale(b); v->scale(a); vtmp->scale(a*b); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
321  *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication with field multiplication. Consistency error: " << " " << vCheck.back() << "\n";
322 
323  // Distributivity of scalar multiplication with respect to field addition.
324  v->set(*this); vtmp->set(*this);
325  v->scale(a+b); vtmp->scale(a); vtmp->axpy(b, *this); v->axpy(-one, *vtmp); vCheck.push_back(v->norm());
326  *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to field addition. Consistency error: " << " " << vCheck.back() << "\n";
327 
328  // Distributivity of scalar multiplication with respect to vector addition.
329  v->set(*this); xtmp->set(x); ytmp->set(y);
330  v->plus(x); v->scale(a); xtmp->scale(a); xtmp->axpy(a, *this); v->axpy(-one, *xtmp); vCheck.push_back(v->norm());
331  *pStream << std::setw(width) << std::left << "Distributivity of scalar multiplication with respect to vector addition. Consistency error: " << " " << vCheck.back() << "\n";
332 
333  // Commutativity of dot (inner) product over the field of reals.
334  vCheck.push_back(std::abs(this->dot(x) - x.dot(*this)));
335  *pStream << std::setw(width) << std::left << "Commutativity of dot (inner) product over the field of reals. Consistency error: " << " " << vCheck.back() << "\n";
336 
337  // Additivity of dot (inner) product.
338  xtmp->set(x);
339  xtmp->plus(y); vCheck.push_back(std::abs(this->dot(*xtmp) - x.dot(*this) - y.dot(*this)));
340  *pStream << std::setw(width) << std::left << "Additivity of dot (inner) product. Consistency error: " << " " << vCheck.back() << "\n";
341 
342  // Consistency of scalar multiplication and norm.
343  v->set(*this);
344  Real vnorm = v->norm();
345  if (vnorm == zero) {
346  v->scale(a);
347  vCheck.push_back(std::abs(v->norm() - zero));
348  } else {
349  v->scale(one/vnorm);
350  vCheck.push_back(std::abs(v->norm() - one));
351  }
352  *pStream << std::setw(width) << std::left << "Consistency of scalar multiplication and norm. Consistency error: " << " " << vCheck.back() << "\n";
353 
354  // Reflexivity.
355  v->set(*this);
356  xtmp = Teuchos::rcp_const_cast<Vector>(Teuchos::rcpFromRef(this->dual()));
357  ytmp = Teuchos::rcp_const_cast<Vector>(Teuchos::rcpFromRef(xtmp->dual()));
358  v->axpy(-one, *ytmp); vCheck.push_back(v->norm());
359  *pStream << std::setw(width) << std::left << "Reflexivity. Consistency error: " << " " << vCheck.back() << "\n\n";
360 
361  //*pStream << "************ End verification of linear algebra.\n\n";
362 
363  // Restore format state of pStream used for the header info.
364  pStream->copyfmt(headerFormatState);
365  *pStream << std::setw(width) << std::left << "********** End verification of linear algebra. " << "\n\n";
366 
367  // Restore format state of the original pStream.
368  pStream->copyfmt(oldFormatState);
369 
370  return vCheck;
371  }
372 
373 }; // class Vector
374 
375 } // namespace ROL
376 
377 #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:213
virtual void scale(const Real alpha)=0
Compute where .
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:183
virtual void plus(const Vector &x)=0
Compute , where .
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:143
virtual Real reduce(const Elementwise::ReductionOp< Real > &r) const
Definition: ROL_Vector.hpp:227
virtual void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector &x)
Definition: ROL_Vector.hpp:222
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:259
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
virtual Real dot(const Vector &x) const =0
Compute where .
virtual ~Vector()
Definition: ROL_Vector.hpp:77
virtual void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Definition: ROL_Vector.hpp:217
virtual Teuchos::RCP< Vector > basis(const int i) const
Return i-th basis vector.
Definition: ROL_Vector.hpp:172
virtual Real norm() const =0
Returns where .