ROL
ROL_ScaledStdVector.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_SCALEDSTDVECTOR_H
45 #define ROL_SCALEDSTDVECTOR_H
46 
47 #include "ROL_StdVector.hpp"
48 
59 namespace ROL {
60 
61 template <class Real, class Element=Real>
63 
64 template <class Real, class Element=Real>
66 
67 template <class Real, class Element>
68 class PrimalScaledStdVector : public StdVector<Real> {
69 
70  typedef typename std::vector<Element>::size_type uint;
71 
72 private:
73 
74  Teuchos::RCP<std::vector<Element> > scaling_vec_;
75  mutable Teuchos::RCP<DualScaledStdVector<Real> > dual_vec_;
76 
77 public:
78 
79  PrimalScaledStdVector(const Teuchos::RCP<std::vector<Element> > & std_vec,
80  const Teuchos::RCP<std::vector<Element> > & scaling_vec) :
81  StdVector<Real>(std_vec), scaling_vec_(scaling_vec) {}
82 
83  Real dot( const Vector<Real> &x ) const {
84  const PrimalScaledStdVector & ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
85  const std::vector<Element>& xval = *ex.getVector();
86  const std::vector<Element>& yval = *(StdVector<Real>::getVector());
87  uint dimension = yval.size();
88  Real val = 0;
89  for (uint i=0; i<dimension; i++) {
90  val += yval[i]*xval[i]*(*scaling_vec_)[i];
91  }
92  return val;
93  }
94 
95  Teuchos::RCP<Vector<Real> > clone() const {
96  uint dimension = (StdVector<Real>::getVector())->size();
97  return Teuchos::rcp( new PrimalScaledStdVector(
98  Teuchos::rcp(new std::vector<Element>(dimension)), scaling_vec_ ) );
99  }
100 
101  const ROL::Vector<Real> & dual() const {
102  const std::vector<Element>& yval = *(StdVector<Real>::getVector());
103  uint dimension = yval.size();
104  std::vector<Element> tmp_vec(yval);
105  for (uint i = 0; i < dimension; i++) {
106  tmp_vec[i] *= (*scaling_vec_)[i];
107  }
108  dual_vec_ = Teuchos::rcp( new DualScaledStdVector<Real>(
109  Teuchos::rcp(new std::vector<Element>(tmp_vec)), scaling_vec_ ) );
110  return *dual_vec_;
111  }
112 
113 }; // class PrimalScaledStdVector
114 
115 
116 
117 template <class Real, class Element>
118 class DualScaledStdVector : public StdVector<Real> {
119 
120  typedef typename std::vector<Element>::size_type uint;
121 
122 private:
123 
124  Teuchos::RCP<std::vector<Element> > scaling_vec_;
125  mutable Teuchos::RCP<PrimalScaledStdVector<Real> > primal_vec_;
126 
127 public:
128 
129  DualScaledStdVector(const Teuchos::RCP<std::vector<Element> > & std_vec,
130  const Teuchos::RCP<std::vector<Element> > & scaling_vec) :
131  StdVector<Real>(std_vec), scaling_vec_(scaling_vec) {}
132 
133  Real dot( const Vector<Real> &x ) const {
134  const DualScaledStdVector & ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
135  const std::vector<Element>& xval = *ex.getVector();
136  const std::vector<Element>& yval = *(StdVector<Real>::getVector());
137  uint dimension = yval.size();
138  Real val = 0;
139  for (uint i=0; i<dimension; i++) {
140  val += yval[i]*xval[i]/(*scaling_vec_)[i];
141  }
142  return val;
143  }
144 
145  Teuchos::RCP<Vector<Real> > clone() const {
146  uint dimension = (StdVector<Real>::getVector())->size();
147  return Teuchos::rcp( new DualScaledStdVector(
148  Teuchos::rcp(new std::vector<Element>(dimension)), scaling_vec_ ) );
149  }
150 
151  const ROL::Vector<Real> & dual() const {
152  const std::vector<Element>& yval = *(StdVector<Real>::getVector());
153  uint dimension = yval.size();
154  std::vector<Element> tmp_vec(yval);
155  for (uint i = 0; i < dimension; i++) {
156  tmp_vec[i] /= (*scaling_vec_)[i];
157  }
158  primal_vec_ = Teuchos::rcp( new PrimalScaledStdVector<Real>(
159  Teuchos::rcp(new std::vector<Element>(tmp_vec)), scaling_vec_ ) );
160  return *primal_vec_;
161  }
162 
163 }; // class DualScaledStdVector
164 
165 } // namespace ROL
166 
167 #endif
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< const std::vector< Element > > getVector() const
std::vector< Element >::size_type uint
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< PrimalScaledStdVector< Real > > primal_vec_
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
PrimalScaledStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec, const Teuchos::RCP< std::vector< Element > > &scaling_vec)
std::vector< Element >::size_type uint
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real dot(const Vector< Real > &x) const
Compute where .
Real dot(const Vector< Real > &x) const
Compute where .
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Provides the std::vector implementation of the ROL::Vector interface.
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< DualScaledStdVector< Real > > dual_vec_
DualScaledStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec, const Teuchos::RCP< std::vector< Element > > &scaling_vec)
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Teuchos::RCP< std::vector< Element > > scaling_vec_
Teuchos::RCP< std::vector< Element > > scaling_vec_