ROL
ROL_SROMVector.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_SROMVECTOR_H
45 #define ROL_SROMVECTOR_H
46 
47 #include <algorithm>
48 #include <cstdlib>
49 
51 #include "ROL_AtomVector.hpp"
52 
58 namespace ROL {
59 
60 template <class Real>
61 class SROMVector : public Vector<Real> {
63 private:
64  const ROL::Ptr<ProbabilityVector<Real> > pvec_;
65  const ROL::Ptr<AtomVector<Real> > avec_;
66 
67  mutable ROL::Ptr<Vector<Real> > dual_pvec_;
68  mutable ROL::Ptr<Vector<Real> > dual_avec_;
69  mutable ROL::Ptr<SROMVector<Real> > dual_vec_;
70  mutable bool isDualInitialized_;
71 
72 public:
73 
74  SROMVector(const ROL::Ptr<ProbabilityVector<Real> > &pvec,
75  const ROL::Ptr<AtomVector<Real> > &avec)
76  : pvec_(pvec), avec_(avec), isDualInitialized_(false) {
77  dual_pvec_ = (pvec_->dual()).clone();
78  dual_avec_ = (avec_->dual()).clone();
79  }
80 
81  void set( const Vector<Real> &x ) {
82  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
83  pvec_->set(*(ex.getProbabilityVector()));
84  avec_->set(*(ex.getAtomVector()));
85  }
86 
87  void plus( const Vector<Real> &x ) {
88  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
89  pvec_->plus(*(ex.getProbabilityVector()));
90  avec_->plus(*(ex.getAtomVector()));
91  }
92 
93  void scale( const Real alpha ) {
94  pvec_->scale(alpha);
95  avec_->scale(alpha);
96  }
97 
98  void axpy( const Real alpha, const Vector<Real> &x ) {
99  const SROMVector &ex = dynamic_cast<const SROMVector&>(x);
100  pvec_->axpy(alpha,*(ex.getProbabilityVector()));
101  avec_->axpy(alpha,*(ex.getAtomVector()));
102  }
103 
104  Real dot( const Vector<Real> &x ) const {
105  const SROMVector & ex = dynamic_cast<const SROMVector&>(x);
106  Real pval = pvec_->dot(*(ex.getProbabilityVector()));
107  Real aval = avec_->dot(*(ex.getAtomVector()));
108  return pval + aval;
109  }
110 
111  Real norm() const {
112  Real val = 0;
113  val = std::sqrt( dot(*this) );
114  return val;
115  }
116 
117  ROL::Ptr<Vector<Real> > clone(void) const {
118  return ROL::makePtr<SROMVector>(
119  ROL::staticPtrCast<ProbabilityVector<Real> >(pvec_->clone()),
120  ROL::staticPtrCast<AtomVector<Real> >(avec_->clone()) );
121  }
122 
123  const Vector<Real> & dual(void) const {
124  if ( !isDualInitialized_ ) {
125  dual_vec_ = ROL::makePtr<SROMVector>(
126  ROL::staticPtrCast<ProbabilityVector<Real> >(dual_pvec_),
127  ROL::staticPtrCast<AtomVector<Real> >(dual_avec_) );
128  isDualInitialized_ = true;
129  }
130  dual_pvec_->set(pvec_->dual());
131  dual_avec_->set(avec_->dual());
132  return *dual_vec_;
133  }
134 
135  int dimension(void) const {
136  return avec_->dimension() + pvec_->dimension();
137  }
138 
139  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
140  pvec_->applyUnary(f);
141  avec_->applyUnary(f);
142  }
143 
144  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
145  const SROMVector & ex = dynamic_cast<const SROMVector&>(x);
146  pvec_->applyBinary(f,*(ex.getProbabilityVector()));
147  avec_->applyBinary(f,*(ex.getAtomVector()));
148  }
149 
150  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
151  Real result = r.initialValue();
152  Real pval = pvec_->reduce(r);
153  Real aval = avec_->reduce(r);
154  r.reduce(pval,result);
155  r.reduce(aval,result);
156  return result;
157  }
158 
159  const ROL::Ptr<const AtomVector<Real> > getAtomVector(void) const {
160  return avec_;
161  }
162 
163  const ROL::Ptr<const ProbabilityVector<Real> > getProbabilityVector(void) const {
164  return pvec_;
165  }
166 
167  ROL::Ptr<AtomVector<Real> > getAtomVector(void) {
168  return avec_;
169  }
170 
171  ROL::Ptr<ProbabilityVector<Real> > getProbabilityVector(void) {
172  return pvec_;
173  }
174 
175  void setAtomVector(const AtomVector<Real> &vec) {
176  avec_->set(vec);
177  }
178 
180  pvec_->set(vec);
181  }
182 
183 }; // class SROMVector
184 
185 } // namespace ROL
186 
187 #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:80
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)