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