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 
74  TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
75  std::invalid_argument,
76  "Error: Vectors must have the same dimension." );
77 
78  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
79  const std::vector<Element>& xval = *ex.getVector();
80  std::copy(xval.begin(),xval.end(),std_vec_->begin());
81  }
82 
83  void plus( const Vector<Real> &x ) {
84 
85  TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
86  std::invalid_argument,
87  "Error: Vectors must have the same dimension." );
88 
89  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
90  const std::vector<Element>& xval = *ex.getVector();
91  uint dim = std_vec_->size();
92  for (uint i=0; i<dim; i++) {
93  (*std_vec_)[i] += xval[i];
94  }
95  }
96 
97  void axpy( const Real alpha, const Vector<Real> &x ) {
98 
99  TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
100  std::invalid_argument,
101  "Error: Vectors must have the same dimension." );
102 
103  const StdVector &ex = Teuchos::dyn_cast<const StdVector>(x);
104  const std::vector<Element>& xval = *ex.getVector();
105  uint dim = std_vec_->size();
106  for (uint i=0; i<dim; i++) {
107  (*std_vec_)[i] += alpha*xval[i];
108  }
109  }
110 
111  void scale( const Real alpha ) {
112  uint dim = std_vec_->size();
113  for (uint i=0; i<dim; i++) {
114  (*std_vec_)[i] *= alpha;
115  }
116  }
117 
118  virtual Real dot( const Vector<Real> &x ) const {
119 
120  TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
121  std::invalid_argument,
122  "Error: Vectors must have the same dimension." );
123 
124  const StdVector & ex = Teuchos::dyn_cast<const StdVector>(x);
125  const std::vector<Element>& xval = *ex.getVector();
126  uint dim = std_vec_->size();
127  Real val = 0;
128  for (uint i=0; i<dim; i++) {
129  val += (*std_vec_)[i]*xval[i];
130  }
131  return val;
132  }
133 
134  Real norm() const {
135  Real val = 0;
136  val = std::sqrt( dot(*this) );
137  return val;
138  }
139 
140  virtual Teuchos::RCP<Vector<Real> > clone() const {
141  return Teuchos::rcp( new StdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size())) ));
142  }
143 
144  Teuchos::RCP<const std::vector<Element> > getVector() const {
145  return std_vec_;
146  }
147 
148  Teuchos::RCP<std::vector<Element> > getVector() {
149  return std_vec_;
150  }
151 
152  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
153 
154  TEUCHOS_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
155  std::invalid_argument,
156  "Error: Basis index must be between 0 and vector dimension." );
157 
158  Teuchos::RCP<StdVector> e = Teuchos::rcp( new StdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size(), 0.0)) ));
159  (*e->getVector())[i] = 1.0;
160  return e;
161  }
162 
163  int dimension() const {
164  return static_cast<int>(std_vec_->size());
165  }
166 
167  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
168  uint dim = std_vec_->size();
169  for(uint i=0; i<dim; ++i) {
170  (*std_vec_)[i] = f.apply((*std_vec_)[i]);
171  }
172  }
173 
174  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
175 
176  TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
177  std::invalid_argument,
178  "Error: Vectors must have the same dimension." );
179 
180  const StdVector & ex = Teuchos::dyn_cast<const StdVector>(x);
181  const std::vector<Element>& xval = *ex.getVector();
182  uint dim = std_vec_->size();
183  for (uint i=0; i<dim; i++) {
184  (*std_vec_)[i] = f.apply((*std_vec_)[i],xval[i]);
185  }
186 
187  }
188 
189  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
190  Real result = r.initialValue();
191  uint dim = std_vec_->size();
192  for(uint i=0; i<dim; ++i) {
193  r.reduce((*std_vec_)[i],result);
194  }
195  return result;
196  }
197 
198 
199 }; // class StdVector
200 
201 
202 
203 
204 } // namespace ROL
205 
206 #endif
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void scale(const Real alpha)
Compute where .
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:183
Teuchos::RCP< const std::vector< Element > > getVector() const
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()
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
void plus(const Vector< Real > &x)
Compute , where .
virtual Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
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)
virtual Real dot(const Vector< Real > &x) const
Compute where .