ROL
ROL_PartitionedVector.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 #include "ROL_Vector.hpp"
45 
46 #include <initializer_list>
47 
48 #ifndef ROL_PARTITIONED_VECTOR_H
49 #define ROL_PARTITIONED_VECTOR_H
50 
57 namespace ROL {
58 
59 template<class Real>
60 class PartitionedVector : public Vector<Real> {
61 
62  typedef Vector<Real> V;
63  typedef ROL::Ptr<V> Vp;
65 
66 private:
67  const std::vector<Vp> vecs_;
68  mutable std::vector<Vp> dual_vecs_;
69  mutable ROL::Ptr<PV> dual_pvec_;
70 public:
71 
73 
74  PartitionedVector( const std::vector<Vp> &vecs ) : vecs_(vecs) {
75  for( size_type i=0; i<vecs_.size(); ++i ) {
76  dual_vecs_.push_back((vecs_[i]->dual()).clone());
77  }
78  }
79 
80  void set( const V &x ) {
81  const PV &xs = dynamic_cast<const PV&>(x);
82  ROL_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
83  std::invalid_argument,
84  "Error: Vectors must have the same number of subvectors." );
85  for( size_type i=0; i<vecs_.size(); ++i ) {
86  vecs_[i]->set(*xs.get(i));
87  }
88  }
89 
90  void plus( const V &x ) {
91  const PV &xs = dynamic_cast<const PV&>(x);
92  ROL_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
93  std::invalid_argument,
94  "Error: Vectors must have the same number of subvectors." );
95  for( size_type i=0; i<vecs_.size(); ++i ) {
96  vecs_[i]->plus(*xs.get(i));
97  }
98  }
99 
100  void scale( const Real alpha ) {
101  for( size_type i=0; i<vecs_.size(); ++i ) {
102  vecs_[i]->scale(alpha);
103  }
104  }
105 
106  void axpy( const Real alpha, const V &x ) {
107  const PV &xs = dynamic_cast<const PV&>(x);
108  ROL_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
109  std::invalid_argument,
110  "Error: Vectors must have the same number of subvectors." );
111 
112  for( size_type i=0; i<vecs_.size(); ++i ) {
113  vecs_[i]->axpy(alpha,*xs.get(i));
114  }
115  }
116 
117  Real dot( const V &x ) const {
118  const PV &xs = dynamic_cast<const PV&>(x);
119  ROL_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
120  std::invalid_argument,
121  "Error: Vectors must have the same number of subvectors." );
122  Real result = 0;
123  for( size_type i=0; i<vecs_.size(); ++i ) {
124  result += vecs_[i]->dot(*xs.get(i));
125  }
126  return result;
127  }
128 
129  Real norm() const {
130  Real result = 0;
131  for( size_type i=0; i<vecs_.size(); ++i ) {
132  result += std::pow(vecs_[i]->norm(),2);
133  }
134  return std::sqrt(result);
135  }
136 
137  Vp clone() const {
138  std::vector<Vp> clonevec;
139  for( size_type i=0; i<vecs_.size(); ++i ) {
140  clonevec.push_back(vecs_[i]->clone());
141  }
142  return ROL::makePtr<PV>(clonevec);
143  }
144 
145  const V& dual(void) const {
146  for( size_type i=0; i<vecs_.size(); ++i ) {
147  dual_vecs_[i]->set(vecs_[i]->dual());
148  }
149  dual_pvec_ = ROL::makePtr<PV>( dual_vecs_ );
150  return *dual_pvec_;
151  }
152 
153  Vp basis( const int i ) const {
154  ROL_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
155  std::invalid_argument,
156  "Error: Basis index must be between 0 and vector dimension." );
157  Vp bvec = clone();
158  // Downcast
159  PV &eb = dynamic_cast<PV&>(*bvec);
160 
161  // Iterate over subvectors
162  int begin = 0, end = 0;
163  for( size_type j=0; j<vecs_.size(); ++j ) {
164  end += vecs_[j]->dimension();
165  if( begin<= i && i<end ) {
166  eb.set(j, *(vecs_[j]->basis(i-begin)) );
167  }
168  else {
169  eb.zero(j);
170  }
171  begin = end;
172  }
173  return bvec;
174  }
175 
176  int dimension() const {
177  int total_dim = 0;
178  for( size_type j=0; j<vecs_.size(); ++j ) {
179  total_dim += vecs_[j]->dimension();
180  }
181  return total_dim;
182  }
183 
184  void zero() {
185  for( size_type j=0; j<vecs_.size(); ++j ) {
186  vecs_[j]->zero();
187  }
188  }
189 
190  // Apply the same unary function to each subvector
191  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
192  for( size_type i=0; i<vecs_.size(); ++i ) {
193  vecs_[i]->applyUnary(f);
194  }
195  }
196 
197  // Apply the same binary function to each pair of subvectors in this vector and x
198  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const V &x ) {
199  const PV &xs = dynamic_cast<const PV&>(x);
200 
201  for( size_type i=0; i<vecs_.size(); ++i ) {
202  vecs_[i]->applyBinary(f,*xs.get(i));
203  }
204  }
205 
206  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
207  Real result = r.initialValue();
208 
209  for( size_type i=0; i<vecs_.size(); ++i ) {
210  r.reduce(vecs_[i]->reduce(r),result);
211  }
212  return result;
213  }
214 
215  void setScalar( const Real C ) {
216  for (size_type i=0; i<vecs_.size(); ++i) {
217  vecs_[i]->setScalar(C);
218  }
219  }
220 
221  void randomize( const Real l = 0.0, const Real u = 1.0 ) {
222  for (size_type i=0; i<vecs_.size(); ++i) {
223  vecs_[i]->randomize(l,u);
224  }
225  }
226 
227  void print( std::ostream &outStream ) const {
228  for( size_type i=0; i<vecs_.size(); ++i ) {
229  outStream << "V[" << i << "]: ";
230  vecs_[i]->print(outStream);
231  }
232  }
233 
234  // Methods that do not exist in the base class
235 
236  ROL::Ptr<const Vector<Real> > get(size_type i) const {
237  return vecs_[i];
238  }
239 
240  ROL::Ptr<Vector<Real> > get(size_type i) {
241  return vecs_[i];
242  }
243 
244  void set(size_type i, const V &x) {
245  vecs_[i]->set(x);
246  }
247 
248  void zero(size_type i) {
249  vecs_[i]->zero();
250  }
251 
253  return vecs_.size();
254  }
255 
256 public:
257 
258  // Make a new PartitionedVector from an initializer_list of pointers to vectors
259  static Ptr<PartitionedVector> create( std::initializer_list<Vp> vs ) {
260  std::vector<Vp> subvecs{vs};
261  return ROL::makePtr<PartitionedVector>( subvecs );
262  }
263 
264  // Make a new PartitionedVector by cloning the given vector N times
265  static Ptr<PartitionedVector> create( const V& x, size_type N ) {
266  std::vector<Vp> subvecs(N);
267  for( size_type i=0; i<N; ++i ) subvecs.at(i) = x.clone();
268  return ROL::makePtr<PartitionedVector>( subvecs );
269  }
270 
271 };
272 
273 // Helper methods
274 template<class Real>
275 ROL::Ptr<Vector<Real>>
276 CreatePartitionedVector( const ROL::Ptr<Vector<Real>> &a ) {
277 
278  using Vp = ROL::Ptr<Vector<Real>>;
279  using PV = PartitionedVector<Real>;
280 
281  Vp temp[] = {a};
282  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
283 }
284 
285 template<class Real>
286 ROL::Ptr<const Vector<Real> >
287 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a ) {
288 
289  using Vp = ROL::Ptr<const Vector<Real>>;
290  using PV = const PartitionedVector<Real>;
291 
292  Vp temp[] = {a};
293  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
294 }
295 
296 template<class Real>
297 ROL::Ptr<Vector<Real>>
299  const ROL::Ptr<Vector<Real>> &b ) {
300  using Vp = ROL::Ptr<Vector<Real>>;
301  using PV = PartitionedVector<Real>;
302 
303  Vp temp[] = {a,b};
304  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
305 }
306 
307 template<class Real>
308 ROL::Ptr<const Vector<Real> >
309 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
310  const ROL::Ptr<const Vector<Real>> &b ) {
311  using Vp = ROL::Ptr<const Vector<Real>>;
312  using PV = const PartitionedVector<Real>;
313 
314  Vp temp[] = {a,b};
315  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
316 }
317 
318 
319 template<class Real>
320 ROL::Ptr<Vector<Real>>
322  const ROL::Ptr<Vector<Real>> &b,
323  const ROL::Ptr<Vector<Real>> &c ) {
324 
325  using Vp = ROL::Ptr<Vector<Real>>;
326  using PV = PartitionedVector<Real>;
327 
328  Vp temp[] = {a,b,c};
329  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
330 }
331 
332 template<class Real>
333 ROL::Ptr<const Vector<Real> >
334 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
335  const ROL::Ptr<const Vector<Real>> &b,
336  const ROL::Ptr<const Vector<Real>> &c ) {
337 
338  using Vp = ROL::Ptr<const Vector<Real>>;
339  using PV = const PartitionedVector<Real>;
340 
341  Vp temp[] = {a,b,c};
342  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
343 }
344 
345 template<class Real>
346 ROL::Ptr<Vector<Real> >
348  const ROL::Ptr<Vector<Real>> &b,
349  const ROL::Ptr<Vector<Real>> &c,
350  const ROL::Ptr<Vector<Real>> &d ) {
351 
352 
353  typedef ROL::Ptr<Vector<Real> > Vp;
354  typedef PartitionedVector<Real> PV;
355 
356  Vp temp[] = {a,b,c,d};
357  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
358 }
359 
360 template<class Real>
361 ROL::Ptr<const Vector<Real> >
362 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
363  const ROL::Ptr<const Vector<Real>> &b,
364  const ROL::Ptr<const Vector<Real>> &c,
365  const ROL::Ptr<const Vector<Real>> &d ) {
366 
367 
368  using Vp = ROL::Ptr<const Vector<Real>>;
369  using PV = const PartitionedVector<Real>;
370 
371  Vp temp[] = {a,b,c,d};
372  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
373 }
374 
375 } // namespace ROL
376 
377 #endif // ROL_PARTITIONED_VECTOR_H
378 
PartitionedVector(const std::vector< Vp > &vecs)
static Ptr< PartitionedVector > create(std::initializer_list< Vp > vs)
PartitionedVector< Real > PV
typename PV< Real >::size_type size_type
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< const Vector< Real > > get(size_type i) const
Real norm() const
Returns where .
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real >> &a)
const V & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
void print(std::ostream &outStream) const
static Ptr< PartitionedVector > create(const V &x, size_type N)
Vp clone() const
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Real dot(const V &x) const
Compute where .
void zero()
Set to zero vector.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
void setScalar(const Real C)
Set where .
PartitionedVector< Real > PV
void set(size_type i, const V &x)
void scale(const Real alpha)
Compute where .
int dimension() const
Return dimension of the vector space.
void set(const V &x)
Set where .
Vp basis(const int i) const
Return i-th basis vector.
std::vector< PV >::size_type size_type
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
const std::vector< Vp > vecs_
void plus(const V &x)
Compute , where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void axpy(const Real alpha, const V &x)
Compute where .