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> {
62  typedef typename std::vector<Real>::size_type uint;
63 private:
64  const Teuchos::RCP<ProbabilityVector<Real> > pvec_;
65  const Teuchos::RCP<AtomVector<Real> > avec_;
66 
67  mutable Teuchos::RCP<Vector<Real> > dual_pvec_;
68  mutable Teuchos::RCP<Vector<Real> > dual_avec_;
69  mutable Teuchos::RCP<SROMVector<Real> > dual_vec_;
70 
71 public:
72 
73  SROMVector(const Teuchos::RCP<ProbabilityVector<Real> > &pvec,
74  const Teuchos::RCP<AtomVector<Real> > &avec)
75  : pvec_(pvec), avec_(avec) {
76  dual_pvec_ = (pvec_->dual()).clone();
77  dual_avec_ = (avec_->dual()).clone();
78  }
79 
80  void set( const Vector<Real> &x ) {
81  const SROMVector &ex = Teuchos::dyn_cast<const SROMVector>(x);
82  pvec_->set(*(ex.getProbabilityVector()));
83  avec_->set(*(ex.getAtomVector()));
84  }
85 
86  void plus( const Vector<Real> &x ) {
87  const SROMVector &ex = Teuchos::dyn_cast<const SROMVector>(x);
88  pvec_->plus(*(ex.getProbabilityVector()));
89  avec_->plus(*(ex.getAtomVector()));
90  }
91 
92  void scale( const Real alpha ) {
93  pvec_->scale(alpha);
94  avec_->scale(alpha);
95  }
96 
97  void axpy( const Real alpha, const Vector<Real> &x ) {
98  const SROMVector &ex = Teuchos::dyn_cast<const SROMVector>(x);
99  pvec_->axpy(alpha,*(ex.getProbabilityVector()));
100  avec_->axpy(alpha,*(ex.getAtomVector()));
101  }
102 
103  Real dot( const Vector<Real> &x ) const {
104  const SROMVector & ex = Teuchos::dyn_cast<const SROMVector>(x);
105  Real pval = pvec_->dot(*(ex.getProbabilityVector()));
106  Real aval = avec_->dot(*(ex.getAtomVector()));
107  return pval + aval;
108  }
109 
110  Real norm() const {
111  Real val = 0;
112  val = std::sqrt( dot(*this) );
113  return val;
114  }
115 
116  Teuchos::RCP<Vector<Real> > clone(void) const {
117  return Teuchos::rcp( new SROMVector(
118  Teuchos::rcp_static_cast<ProbabilityVector<Real> >(pvec_->clone()),
119  Teuchos::rcp_static_cast<AtomVector<Real> >(avec_->clone())) );
120  }
121 
122  const Vector<Real> & dual(void) const {
123  dual_pvec_->set(pvec_->dual());
124  dual_avec_->set(avec_->dual());
125  dual_vec_ = Teuchos::rcp(new SROMVector(
126  Teuchos::rcp_static_cast<ProbabilityVector<Real> >(dual_pvec_),
127  Teuchos::rcp_static_cast<AtomVector<Real> >(dual_avec_)));
128  return *dual_vec_;
129  }
130 
131  int dimension(void) const {
132  return avec_->dimension() + pvec_->dimension();
133  }
134 
135  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
136  pvec_->applyUnary(f);
137  avec_->applyUnary(f);
138  }
139 
140  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
141  const SROMVector & ex = Teuchos::dyn_cast<const SROMVector>(x);
142  pvec_->applyBinary(f,*(ex.getProbabilityVector()));
143  avec_->applyBinary(f,*(ex.getAtomVector()));
144  }
145 
146  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
147  Real result = r.initialValue();
148  Real pval = pvec_->reduce(r);
149  Real aval = avec_->reduce(r);
150  r.reduce(pval,result);
151  r.reduce(aval,result);
152  return result;
153  }
154 
155  const Teuchos::RCP<const AtomVector<Real> > getAtomVector(void) const {
156  return avec_;
157  }
158 
159  const Teuchos::RCP<const ProbabilityVector<Real> > getProbabilityVector(void) const {
160  return pvec_;
161  }
162 
163  Teuchos::RCP<AtomVector<Real> > getAtomVector(void) {
164  return avec_;
165  }
166 
167  Teuchos::RCP<ProbabilityVector<Real> > getProbabilityVector(void) {
168  return pvec_;
169  }
170 
171  void setAtomVector(const AtomVector<Real> &vec) {
172  avec_->set(vec);
173  }
174 
176  pvec_->set(vec);
177  }
178 
179 }; // class SROMVector
180 
181 } // namespace ROL
182 
183 #endif
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
const Teuchos::RCP< AtomVector< Real > > avec_
Provides the std::vector implementation of the ROL::Vector interface.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
const Teuchos::RCP< const ProbabilityVector< Real > > getProbabilityVector(void) const
void plus(const Vector< Real > &x)
Compute , where .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Real dot(const Vector< Real > &x) const
Compute where .
Teuchos::RCP< 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 Teuchos::RCP< const AtomVector< Real > > getAtomVector(void) const
Teuchos::RCP< AtomVector< Real > > getAtomVector(void)
Real norm() const
Returns where .
const Teuchos::RCP< ProbabilityVector< Real > > pvec_
Provides the std::vector implementation of the ROL::Vector interface.
Teuchos::RCP< ProbabilityVector< Real > > getProbabilityVector(void)
Provides the std::vector implementation of the ROL::Vector interface.
Teuchos::RCP< SROMVector< Real > > dual_vec_
int dimension(void) const
Return dimension of the vector space.
void setProbabilityVector(const ProbabilityVector< Real > &vec)
Teuchos::RCP< Vector< Real > > dual_pvec_
void scale(const Real alpha)
Compute where .
void setAtomVector(const AtomVector< Real > &vec)
void set(const Vector< Real > &x)
Set where .
Teuchos::RCP< Vector< Real > > dual_avec_
SROMVector(const Teuchos::RCP< ProbabilityVector< Real > > &pvec, const Teuchos::RCP< AtomVector< Real > > &avec)
std::vector< Real >::size_type uint