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  const Vector<Real>& operator[]( size_type i ) const {
236  return *(vecs_[i]);
237  }
238 
240  return *(vecs_[i]);
241  }
242 
243  ROL::Ptr<const Vector<Real> > get(size_type i) const {
244  return vecs_[i];
245  }
246 
247  ROL::Ptr<Vector<Real> > get(size_type i) {
248  return vecs_[i];
249  }
250 
251  void set(size_type i, const V &x) {
252  vecs_[i]->set(x);
253  }
254 
255  void zero(size_type i) {
256  vecs_[i]->zero();
257  }
258 
260  return vecs_.size();
261  }
262 
263 public:
264 
265  // Make a new PartitionedVector from an initializer_list of pointers to vectors
266  static Ptr<PartitionedVector> create( std::initializer_list<Vp> vs ) {
267  std::vector<Vp> subvecs{vs};
268  return ROL::makePtr<PartitionedVector>( subvecs );
269  }
270 
271  // Make a new PartitionedVector by cloning the given vector N times
272  static Ptr<PartitionedVector> create( const V& x, size_type N ) {
273  std::vector<Vp> subvecs(N);
274  for( size_type i=0; i<N; ++i ) subvecs.at(i) = x.clone();
275  return ROL::makePtr<PartitionedVector>( subvecs );
276  }
277 
278 };
279 
280 // Helper methods
281 
282 template<typename Real>
284  return static_cast<PartitionedVector<Real>&>(x);
285 }
286 
287 template<typename Real>
288 inline const PartitionedVector<Real>& partition( const Vector<Real>& x ) {
289  return static_cast<const PartitionedVector<Real>&>(x);
290 }
291 
292 template<typename Real>
293 inline Ptr<PartitionedVector<Real>> partition( const Ptr<Vector<Real>>& x ) {
294  return staticPtrCast<PartitionedVector<Real>>(x);
295 }
296 
297 template<typename Real>
298 inline Ptr<const PartitionedVector<Real>> partition( const Ptr<const Vector<Real>>& x ) {
299  return staticPtrCast<const PartitionedVector<Real>>(x);
300 }
301 
302 
303 
304 template<class Real>
305 ROL::Ptr<Vector<Real>>
306 CreatePartitionedVector( const ROL::Ptr<Vector<Real>> &a ) {
307 
308  using Vp = ROL::Ptr<Vector<Real>>;
309  using PV = PartitionedVector<Real>;
310 
311  Vp temp[] = {a};
312  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
313 }
314 
315 template<class Real>
316 ROL::Ptr<const Vector<Real> >
317 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a ) {
318 
319  using Vp = ROL::Ptr<const Vector<Real>>;
320  using PV = const PartitionedVector<Real>;
321 
322  Vp temp[] = {a};
323  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
324 }
325 
326 template<class Real>
327 ROL::Ptr<Vector<Real>>
329  const ROL::Ptr<Vector<Real>> &b ) {
330  using Vp = ROL::Ptr<Vector<Real>>;
331  using PV = PartitionedVector<Real>;
332 
333  Vp temp[] = {a,b};
334  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
335 }
336 
337 template<class Real>
338 ROL::Ptr<const Vector<Real> >
339 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
340  const ROL::Ptr<const Vector<Real>> &b ) {
341  using Vp = ROL::Ptr<const Vector<Real>>;
342  using PV = const PartitionedVector<Real>;
343 
344  Vp temp[] = {a,b};
345  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
346 }
347 
348 
349 template<class Real>
350 ROL::Ptr<Vector<Real>>
352  const ROL::Ptr<Vector<Real>> &b,
353  const ROL::Ptr<Vector<Real>> &c ) {
354 
355  using Vp = ROL::Ptr<Vector<Real>>;
356  using PV = PartitionedVector<Real>;
357 
358  Vp temp[] = {a,b,c};
359  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
360 }
361 
362 template<class Real>
363 ROL::Ptr<const Vector<Real> >
364 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
365  const ROL::Ptr<const Vector<Real>> &b,
366  const ROL::Ptr<const Vector<Real>> &c ) {
367 
368  using Vp = ROL::Ptr<const Vector<Real>>;
369  using PV = const PartitionedVector<Real>;
370 
371  Vp temp[] = {a,b,c};
372  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
373 }
374 
375 template<class Real>
376 ROL::Ptr<Vector<Real> >
378  const ROL::Ptr<Vector<Real>> &b,
379  const ROL::Ptr<Vector<Real>> &c,
380  const ROL::Ptr<Vector<Real>> &d ) {
381 
382 
383  typedef ROL::Ptr<Vector<Real> > Vp;
384  typedef PartitionedVector<Real> PV;
385 
386  Vp temp[] = {a,b,c,d};
387  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
388 }
389 
390 template<class Real>
391 ROL::Ptr<const Vector<Real> >
392 CreatePartitionedVector( const ROL::Ptr<const Vector<Real>> &a,
393  const ROL::Ptr<const Vector<Real>> &b,
394  const ROL::Ptr<const Vector<Real>> &c,
395  const ROL::Ptr<const Vector<Real>> &d ) {
396 
397 
398  using Vp = ROL::Ptr<const Vector<Real>>;
399  using PV = const PartitionedVector<Real>;
400 
401  Vp temp[] = {a,b,c,d};
402  return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
403 }
404 
405 } // namespace ROL
406 
407 #endif // ROL_PARTITIONED_VECTOR_H
408 
PartitionedVector< Real > & partition(Vector< Real > &x)
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...
Vector< Real > & operator[](size_type i)
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.
const Vector< Real > & operator[](size_type i) const
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 .