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