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