ROL
ROL_StdVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_STDVECTOR_H
45 #define ROL_STDVECTOR_H
46 
47 #include <algorithm>
48 #include <cstdlib>
49 
50 #include "ROL_Vector.hpp"
51 
57 namespace ROL {
58 
59 template <class Real, class Element=Real>
60 class StdVector : public Vector<Real> {
61 
62  typedef typename std::vector<Real>::size_type uint;
63 
64 private:
65 
66  Teuchos::RCP<std::vector<Element> > std_vec_;
67 
68 public:
69 
70  StdVector(const Teuchos::RCP<std::vector<Element> > & std_vec) : std_vec_(std_vec) {}
71 
72  void set( const Vector<Real> &x ) {
73  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
74  const std::vector<Element>& xval = *ex.getVector();
75  std::copy(xval.begin(),xval.end(),std_vec_->begin());
76  }
77 
78  void plus( const Vector<Real> &x ) {
79  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
80  const std::vector<Element>& xval = *ex.getVector();
81  unsigned dimension = std_vec_->size();
82  for (unsigned i=0; i<dimension; i++) {
83  (*std_vec_)[i] += xval[i];
84  }
85  }
86 
87  void axpy( const Real alpha, const Vector<Real> &x ) {
88  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
89  const std::vector<Element>& xval = *ex.getVector();
90  uint dimension = std_vec_->size();
91  for (uint i=0; i<dimension; i++) {
92  (*std_vec_)[i] += alpha*xval[i];
93  }
94  }
95 
96  void scale( const Real alpha ) {
97  uint dimension = std_vec_->size();
98  for (uint i=0; i<dimension; i++) {
99  (*std_vec_)[i] *= alpha;
100  }
101  }
102 
103  Real dot( const Vector<Real> &x ) const {
104  const StdVector & ex = Teuchos::dyn_cast<const StdVector>(x);
105  const std::vector<Element>& xval = *ex.getVector();
106  uint dimension = std_vec_->size();
107  Real val = 0;
108  for (uint i=0; i<dimension; i++) {
109  val += (*std_vec_)[i]*xval[i];
110  }
111  return val;
112  }
113 
114  Real norm() const {
115  Real val = 0;
116  val = std::sqrt( dot(*this) );
117  return val;
118  }
119 
120  Teuchos::RCP<Vector<Real> > clone() const {
121  return Teuchos::rcp( new StdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size())) ));
122  }
123 
124  Teuchos::RCP<const std::vector<Element> > getVector() const {
125  return std_vec_;
126  }
127 
128  Teuchos::RCP<std::vector<Element> > getVector() {
129  return std_vec_;
130  }
131 
132  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
133  Teuchos::RCP<StdVector> e = Teuchos::rcp( new StdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size(), 0.0)) ));
134  (*e->getVector())[i] = 1.0;
135  return e;
136  }
137 
138  int dimension() const {
139  return static_cast<int>(std_vec_->size());
140  }
141 
142  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
143  uint dimension = std_vec_->size();
144  for(uint i=0; i<dimension; ++i) {
145  (*std_vec_)[i] = f.apply((*std_vec_)[i]);
146  }
147 
148  }
149 
150  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
151  const StdVector & ex = Teuchos::dyn_cast<const StdVector>(x);
152  const std::vector<Element>& xval = *ex.getVector();
153  uint dimension = std_vec_->size();
154  for (uint i=0; i<dimension; i++) {
155  (*std_vec_)[i] = f.apply((*std_vec_)[i],xval[i]);
156  }
157 
158  }
159 
160  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
161  Real result = r.initialValue();
162  uint dimension = std_vec_->size();
163  for(uint i=0; i<dimension; ++i) {
164  r.reduce((*std_vec_)[i],result);
165  }
166  return result;
167  }
168 
169 
170 }; // class StdVector
171 
172 
173 
174 
175 } // namespace ROL
176 
177 #endif
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void scale(const Real alpha)
Compute where .
Teuchos::RCP< const std::vector< Element > > getVector() const
Real dot(const Vector< Real > &x) const
Compute where .
StdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< std::vector< Element > > getVector()
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
void plus(const Vector< Real > &x)
Compute , where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Teuchos::RCP< std::vector< Element > > std_vec_
int dimension() const
Return dimension of the vector space.
std::vector< Real >::size_type uint
void applyUnary(const Elementwise::UnaryFunction< Real > &f)