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 #include <initializer_list>
50 #include "ROL_Vector.hpp"
51 
57 namespace ROL {
58 
59 template <class Real, class Element=Real>
60 class StdVector : public Vector<Real> {
61 
63 
64 public:
65 
66  StdVector( const Ptr<std::vector<Element>> & std_vec ) : std_vec_(std_vec) {}
67 
68  StdVector( const int dim, const Element val=0.0 ) {
69  std_vec_ = makePtr<std::vector<Element>>(dim,val);
70  }
71 
72  StdVector( std::initializer_list<Element> ilist ) :
73  std_vec_( makePtr<std::vector<Element>>(ilist) ) {}
74 
75  Real& operator[] ( int i ) { return (*std_vec_)[i]; }
76  const Real& operator[] ( int i ) const { return (*std_vec_)[i]; }
77 
78  void set( const Vector<Real> &x ) {
79 
80  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
81  std::invalid_argument,
82  "Error: Vectors must have the same dimension." );
83 
84  const StdVector &ex = static_cast<const StdVector&>(x);
85  const std::vector<Element>& xval = *ex.getVector();
86  std::copy(xval.begin(),xval.end(),std_vec_->begin());
87  }
88 
89  void plus( const Vector<Real> &x ) {
90 
91  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
92  std::invalid_argument,
93  "Error: Vectors must have the same dimension." );
94 
95  const StdVector &ex = static_cast<const StdVector&>(x);
96  const std::vector<Element>& xval = *ex.getVector();
97  size_type dim = std_vec_->size();
98  for (size_type i=0; i<dim; i++) {
99  (*std_vec_)[i] += xval[i];
100  }
101  }
102 
103  void axpy( const Real alpha, const Vector<Real> &x ) {
104 
105  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
106  std::invalid_argument,
107  "Error: Vectors must have the same dimension." );
108 
109  const StdVector &ex = static_cast<const StdVector&>(x);
110  const std::vector<Element>& xval = *ex.getVector();
111  size_type dim = std_vec_->size();
112  for (size_type i=0; i<dim; i++) {
113  (*std_vec_)[i] += alpha*xval[i];
114  }
115  }
116 
117  void scale( const Real alpha ) {
118  for( auto& e : *std_vec_ ) e *= alpha;
119 // size_type dim = std_vec_->size();
120 // for (size_type i=0; i<dim; i++) {
121 // (*std_vec_)[i] *= alpha;
122 // }
123  }
124 
125  virtual Real dot( const Vector<Real> &x ) const {
126 
127  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
128  std::invalid_argument,
129  "Error: Vectors must have the same dimension." );
130 
131  const StdVector& ex = static_cast<const StdVector&>(x);
132  const std::vector<Element>& xval = *ex.getVector();
133 // size_type dim = std_vec_->size();
134 // Real val = 0;
135 // for (size_type i=0; i<dim; i++) {
136 // val += (*std_vec_)[i]*xval[i];
137 // }
138 // return val;
139  return std::inner_product(std_vec_->begin(), std_vec_->end(), xval.begin(), Real(0));
140  }
141 
142  Real norm() const {
143  Real val = 0;
144  val = std::sqrt( dot(*this) );
145  return val;
146  }
147 
148  virtual Ptr<Vector<Real> > clone() const {
149  return makePtr<StdVector>( makePtr<std::vector<Element>>(std_vec_->size(), static_cast<Element>(0)));
150  }
151 
152  Ptr<const std::vector<Element> > getVector() const {
153  return std_vec_;
154  }
155 
156  Ptr<std::vector<Element> > getVector() {
157  return std_vec_;
158  }
159 
160  Ptr<Vector<Real> > basis( const int i ) const {
161 
162  ROL_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
163  std::invalid_argument,
164  "Error: Basis index must be between 0 and vector dimension." );
165  auto e = clone();
166  (*staticPtrCast<StdVector>(e)->getVector())[i] = 1.0;
167  return e;
168  }
169 
170  int dimension() const {
171  return static_cast<int>(std_vec_->size());
172  }
173 
174  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
175 // size_type dim = std_vec_->size();
176 // for(size_type i=0; i<dim; ++i) {
177 // (*std_vec_)[i] = f.apply((*std_vec_)[i]);
178 // }
179  for( auto& e : *std_vec_ ) e = f.apply(e);
180  }
181 
182  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
183 
184  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
185  std::invalid_argument,
186  "Error: Vectors must have the same dimension." );
187 
188  const StdVector & ex = static_cast<const StdVector&>(x);
189  const std::vector<Element>& xval = *ex.getVector();
190  size_type dim = std_vec_->size();
191  for (size_type i=0; i<dim; i++) {
192  (*std_vec_)[i] = f.apply((*std_vec_)[i],xval[i]);
193  }
194 
195  }
196 
197  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
198  Real result = r.initialValue();
199  size_type dim = std_vec_->size();
200  for(size_type i=0; i<dim; ++i) {
201  r.reduce((*std_vec_)[i],result);
202  }
203  return result;
204  }
205 
206  void setScalar( const Real C ) {
207  size_type dim = std_vec_->size();
208  std_vec_->assign(dim,C);
209  }
210 
211  void randomize( const Real l = 0.0, const Real u = 1.0 ) {
212  Real a = (u-l);
213  Real b = l;
214 // Real x(0);
215 // size_type dim = std_vec_->size();
216 // for (size_type i=0; i<dim; ++i) {
217 // x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
218 // (*std_vec_)[i] = a*x + b;
219 // }
220  auto get_rand = [a,b]( Real& e ) {
221  auto x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
222  e = a*x+b;
223  };
224  std::for_each( std_vec_->begin(), std_vec_->end(), get_rand );
225  }
226 
227  virtual void print( std::ostream &outStream ) const {
228 // size_type dim = std_vec_->size();
229 // for(size_type i=0; i<dim; ++i) {
230 // outStream << (*std_vec_)[i] << " ";
231 // }
232  for( auto e : *std_vec_ ) outStream << e << " ";
233  outStream << std::endl;
234  }
235 
236 private:
237 
238  Ptr<std::vector<Element>> std_vec_;
239 
240 }; // class StdVector
241 
242 
243 } // namespace ROL
244 
245 #endif
typename PV< Real >::size_type size_type
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void scale(const Real alpha)
Compute where .
StdVector(const int dim, const Element val=0.0)
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:196
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
StdVector(const Ptr< std::vector< Element >> &std_vec)
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Ptr< const std::vector< Element > > getVector() const
typename std::vector< Real >::size_type size_type
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void print(std::ostream &outStream) const
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void plus(const Vector< Real > &x)
Compute , where .
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real reduce(const Elementwise::ReductionOp< Real > &r) const
int dimension() const
Return dimension of the vector space.
Real & operator[](int i)
void set(const Vector< Real > &x)
Set where .
void setScalar(const Real C)
Set where .
Ptr< std::vector< Element > > std_vec_
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
constexpr auto dim
virtual Real dot(const Vector< Real > &x) const
Compute where .
Ptr< std::vector< Element > > getVector()
StdVector(std::initializer_list< Element > ilist)