ROL
ROL_SROMVector.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_SROMVECTOR_H
11 #define ROL_SROMVECTOR_H
12 
13 #include <algorithm>
14 #include <cstdlib>
15 
17 #include "ROL_AtomVector.hpp"
18 
24 namespace ROL {
25 
26 template <class Real>
27 class SROMVector : public Vector<Real> {
29 private:
30  const ROL::Ptr<ProbabilityVector<Real> > pvec_;
31  const ROL::Ptr<AtomVector<Real> > avec_;
32 
33  mutable ROL::Ptr<Vector<Real> > dual_pvec_;
34  mutable ROL::Ptr<Vector<Real> > dual_avec_;
35  mutable ROL::Ptr<SROMVector<Real> > dual_vec_;
36  mutable bool isDualInitialized_;
37 
38 public:
39 
40  SROMVector(const ROL::Ptr<ProbabilityVector<Real> > &pvec,
41  const ROL::Ptr<AtomVector<Real> > &avec)
42  : pvec_(pvec), avec_(avec), isDualInitialized_(false) {
43  dual_pvec_ = (pvec_->dual()).clone();
44  dual_avec_ = (avec_->dual()).clone();
45  }
46 
47  void set( const Vector<Real> &x ) {
48  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
49  pvec_->set(*(ex.getProbabilityVector()));
50  avec_->set(*(ex.getAtomVector()));
51  }
52 
53  void plus( const Vector<Real> &x ) {
54  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
55  pvec_->plus(*(ex.getProbabilityVector()));
56  avec_->plus(*(ex.getAtomVector()));
57  }
58 
59  void scale( const Real alpha ) {
60  pvec_->scale(alpha);
61  avec_->scale(alpha);
62  }
63 
64  void axpy( const Real alpha, const Vector<Real> &x ) {
65  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
66  pvec_->axpy(alpha,*(ex.getProbabilityVector()));
67  avec_->axpy(alpha,*(ex.getAtomVector()));
68  }
69 
70  Real dot( const Vector<Real> &x ) const {
71  const SROMVector & ex = dynamic_cast<const SROMVector&>(x);
72  Real pval = pvec_->dot(*(ex.getProbabilityVector()));
73  Real aval = avec_->dot(*(ex.getAtomVector()));
74  return pval + aval;
75  }
76 
77  Real norm() const {
78  Real val = 0;
79  val = std::sqrt( dot(*this) );
80  return val;
81  }
82 
83  ROL::Ptr<Vector<Real> > clone(void) const {
84  return ROL::makePtr<SROMVector>(
85  ROL::staticPtrCast<ProbabilityVector<Real> >(pvec_->clone()),
86  ROL::staticPtrCast<AtomVector<Real> >(avec_->clone()) );
87  }
88 
89  const Vector<Real> & dual(void) const {
90  if ( !isDualInitialized_ ) {
91  dual_vec_ = ROL::makePtr<SROMVector>(
92  ROL::staticPtrCast<ProbabilityVector<Real> >(dual_pvec_),
93  ROL::staticPtrCast<AtomVector<Real> >(dual_avec_) );
94  isDualInitialized_ = true;
95  }
96  dual_pvec_->set(pvec_->dual());
97  dual_avec_->set(avec_->dual());
98  return *dual_vec_;
99  }
100 
101  int dimension(void) const {
102  return avec_->dimension() + pvec_->dimension();
103  }
104 
105  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
106  pvec_->applyUnary(f);
107  avec_->applyUnary(f);
108  }
109 
110  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
111  const SROMVector & ex = dynamic_cast<const SROMVector&>(x);
112  pvec_->applyBinary(f,*(ex.getProbabilityVector()));
113  avec_->applyBinary(f,*(ex.getAtomVector()));
114  }
115 
116  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
117  Real result = r.initialValue();
118  Real pval = pvec_->reduce(r);
119  Real aval = avec_->reduce(r);
120  r.reduce(pval,result);
121  r.reduce(aval,result);
122  return result;
123  }
124 
125  const ROL::Ptr<const AtomVector<Real> > getAtomVector(void) const {
126  return avec_;
127  }
128 
129  const ROL::Ptr<const ProbabilityVector<Real> > getProbabilityVector(void) const {
130  return pvec_;
131  }
132 
133  ROL::Ptr<AtomVector<Real> > getAtomVector(void) {
134  return avec_;
135  }
136 
137  ROL::Ptr<ProbabilityVector<Real> > getProbabilityVector(void) {
138  return pvec_;
139  }
140 
141  void setAtomVector(const AtomVector<Real> &vec) {
142  avec_->set(vec);
143  }
144 
146  pvec_->set(vec);
147  }
148 
149 }; // class SROMVector
150 
151 } // namespace ROL
152 
153 #endif
typename PV< Real >::size_type size_type
const ROL::Ptr< const ProbabilityVector< Real > > getProbabilityVector(void) const
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Provides the std::vector implementation of the ROL::Vector interface.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
void plus(const Vector< Real > &x)
Compute , where .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
const ROL::Ptr< ProbabilityVector< Real > > pvec_
Real norm() const
Returns where .
const ROL::Ptr< AtomVector< Real > > avec_
Provides the std::vector implementation of the ROL::Vector interface.
Provides the std::vector implementation of the ROL::Vector interface.
ROL::Ptr< ProbabilityVector< Real > > getProbabilityVector(void)
ROL::Ptr< Vector< Real > > dual_avec_
const ROL::Ptr< const AtomVector< Real > > getAtomVector(void) const
ROL::Ptr< Vector< Real > > dual_pvec_
int dimension(void) const
Return dimension of the vector space.
void setProbabilityVector(const ProbabilityVector< Real > &vec)
void scale(const Real alpha)
Compute where .
void setAtomVector(const AtomVector< Real > &vec)
void set(const Vector< Real > &x)
Set where .
SROMVector(const ROL::Ptr< ProbabilityVector< Real > > &pvec, const ROL::Ptr< AtomVector< Real > > &avec)
std::vector< Real >::size_type uint
ROL::Ptr< SROMVector< Real > > dual_vec_
ROL::Ptr< AtomVector< Real > > getAtomVector(void)