ROL
ROL_StdVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_STDVECTOR_H
11 #define ROL_STDVECTOR_H
12 
13 #include <algorithm>
14 #include <cstdlib>
15 #include <numeric>
16 #include <initializer_list>
17 #include "ROL_Vector.hpp"
18 
24 namespace ROL {
25 
26 template <class Real, class Element=Real>
27 class StdVector : public Vector<Real> {
28 
30 
31 public:
32 
33  StdVector( const Ptr<std::vector<Element>> & std_vec ) : std_vec_(std_vec) {}
34 
35  StdVector( const int dim, const Element val=0.0 ) {
36  std_vec_ = makePtr<std::vector<Element>>(dim,val);
37  }
38 
39  StdVector( std::initializer_list<Element> ilist ) :
40  std_vec_( makePtr<std::vector<Element>>(ilist) ) {}
41 
42  Real& operator[] ( int i ) { return (*std_vec_)[i]; }
43  const Real& operator[] ( int i ) const { return (*std_vec_)[i]; }
44 
45  void set( const Vector<Real> &x ) {
46 
47  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
48  std::invalid_argument,
49  "Error: Vectors must have the same dimension." );
50 
51  const StdVector &ex = static_cast<const StdVector&>(x);
52  const std::vector<Element>& xval = *ex.getVector();
53  std::copy(xval.begin(),xval.end(),std_vec_->begin());
54  }
55 
56  void plus( const Vector<Real> &x ) {
57 
58  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
59  std::invalid_argument,
60  "Error: Vectors must have the same dimension." );
61 
62  const StdVector &ex = static_cast<const StdVector&>(x);
63  const std::vector<Element>& xval = *ex.getVector();
64  size_type dim = std_vec_->size();
65  for (size_type i=0; i<dim; i++) {
66  (*std_vec_)[i] += xval[i];
67  }
68  }
69 
70  void axpy( const Real alpha, const Vector<Real> &x ) {
71 
72  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
73  std::invalid_argument,
74  "Error: Vectors must have the same dimension." );
75 
76  const StdVector &ex = static_cast<const StdVector&>(x);
77  const std::vector<Element>& xval = *ex.getVector();
78  size_type dim = std_vec_->size();
79  for (size_type i=0; i<dim; i++) {
80  (*std_vec_)[i] += alpha*xval[i];
81  }
82  }
83 
84  void scale( const Real alpha ) {
85  for( auto& e : *std_vec_ ) e *= alpha;
86 // size_type dim = std_vec_->size();
87 // for (size_type i=0; i<dim; i++) {
88 // (*std_vec_)[i] *= alpha;
89 // }
90  }
91 
92  virtual Real dot( const Vector<Real> &x ) const {
93 
94  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
95  std::invalid_argument,
96  "Error: Vectors must have the same dimension." );
97 
98  const StdVector& ex = static_cast<const StdVector&>(x);
99  const std::vector<Element>& xval = *ex.getVector();
100 // size_type dim = std_vec_->size();
101 // Real val = 0;
102 // for (size_type i=0; i<dim; i++) {
103 // val += (*std_vec_)[i]*xval[i];
104 // }
105 // return val;
106  return std::inner_product(std_vec_->begin(), std_vec_->end(), xval.begin(), Real(0));
107  }
108 
109  Real norm() const {
110  Real val = 0;
111  val = std::sqrt( dot(*this) );
112  return val;
113  }
114 
115  virtual Ptr<Vector<Real> > clone() const {
116  return makePtr<StdVector>( makePtr<std::vector<Element>>(std_vec_->size(), static_cast<Element>(0)));
117  }
118 
119  Ptr<const std::vector<Element> > getVector() const {
120  return std_vec_;
121  }
122 
123  Ptr<std::vector<Element> > getVector() {
124  return std_vec_;
125  }
126 
127  Ptr<Vector<Real> > basis( const int i ) const {
128 
129  ROL_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
130  std::invalid_argument,
131  "Error: Basis index must be between 0 and vector dimension." );
132  auto e = clone();
133  (*staticPtrCast<StdVector>(e)->getVector())[i] = 1.0;
134  return e;
135  }
136 
137  int dimension() const {
138  return static_cast<int>(std_vec_->size());
139  }
140 
141  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
142 // size_type dim = std_vec_->size();
143 // for(size_type i=0; i<dim; ++i) {
144 // (*std_vec_)[i] = f.apply((*std_vec_)[i]);
145 // }
146  for( auto& e : *std_vec_ ) e = f.apply(e);
147  }
148 
149  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
150 
151  ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
152  std::invalid_argument,
153  "Error: Vectors must have the same dimension." );
154 
155  const StdVector & ex = static_cast<const StdVector&>(x);
156  const std::vector<Element>& xval = *ex.getVector();
157  size_type dim = std_vec_->size();
158  for (size_type i=0; i<dim; i++) {
159  (*std_vec_)[i] = f.apply((*std_vec_)[i],xval[i]);
160  }
161 
162  }
163 
164  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
165  Real result = r.initialValue();
166  size_type dim = std_vec_->size();
167  for(size_type i=0; i<dim; ++i) {
168  r.reduce((*std_vec_)[i],result);
169  }
170  return result;
171  }
172 
173  void setScalar( const Real C ) {
174  size_type dim = std_vec_->size();
175  std_vec_->assign(dim,C);
176  }
177 
178  void randomize( const Real l = 0.0, const Real u = 1.0 ) {
179  Real a = (u-l);
180  Real b = l;
181 // Real x(0);
182 // size_type dim = std_vec_->size();
183 // for (size_type i=0; i<dim; ++i) {
184 // x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
185 // (*std_vec_)[i] = a*x + b;
186 // }
187  auto get_rand = [a,b]( Real& e ) {
188  auto x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
189  e = a*x+b;
190  };
191  std::for_each( std_vec_->begin(), std_vec_->end(), get_rand );
192  }
193 
194  virtual void print( std::ostream &outStream ) const {
195 // size_type dim = std_vec_->size();
196 // for(size_type i=0; i<dim; ++i) {
197 // outStream << (*std_vec_)[i] << " ";
198 // }
199  for( auto e : *std_vec_ ) outStream << e << " ";
200  outStream << std::endl;
201  }
202 
203 private:
204 
205  Ptr<std::vector<Element>> std_vec_;
206 
207 }; // class StdVector
208 
209 
210 } // namespace ROL
211 
212 #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:162
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:46
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)