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
59namespace ROL {
60
61template <class Real, class Element=Real>
62class PrimalScaledStdVector;
63
64template <class Real, class Element=Real>
65class DualScaledStdVector;
66
67template <class Real, class Element>
68class PrimalScaledStdVector : public StdVector<Real> {
69
70 typedef typename std::vector<Element>::size_type uint;
71
72private:
73
74 Ptr<std::vector<Element> > scaling_vec_;
75 mutable Ptr<DualScaledStdVector<Real> > dual_vec_;
76 mutable bool isDualInitialized_;
77
78public:
79
80 PrimalScaledStdVector(const Ptr<std::vector<Element> > & std_vec,
81 const Ptr<std::vector<Element> > & scaling_vec) :
82 StdVector<Real>(std_vec), scaling_vec_(scaling_vec),
83 isDualInitialized_(false) {}
84
85 Real dot( const Vector<Real> &x ) const {
86 const PrimalScaledStdVector & ex = dynamic_cast<const PrimalScaledStdVector&>(x);
87 const std::vector<Element>& xval = *ex.getVector();
88 const std::vector<Element>& yval = *(StdVector<Real>::getVector());
89 uint dimension = yval.size();
90 Real val = 0;
91 for (uint i=0; i<dimension; i++) {
92 val += yval[i]*xval[i]*(*scaling_vec_)[i];
93 }
94 return val;
95 }
96
97 Ptr<Vector<Real> > clone() const {
99 return makePtr<PrimalScaledStdVector>(
100 makePtr<std::vector<Element>>(dimension), scaling_vec_ );
101 }
102
103 const Vector<Real> & dual() const {
104 uint n = StdVector<Real>::getVector()->size();
105 if ( !isDualInitialized_ ) {
106 dual_vec_ = makePtr<DualScaledStdVector<Real>>(
107 makePtr<std::vector<Element>>(n),
109 isDualInitialized_ = true;
110 }
111 for (uint i = 0; i < n; i++) {
112 (*(dual_vec_->getVector()))[i]
113 = (*scaling_vec_)[i]*(*StdVector<Real>::getVector())[i];
114 }
115 return *dual_vec_;
116 }
117
118 Real apply( const Vector<Real> &x ) const {
119 const DualScaledStdVector<Real> & ex = dynamic_cast<const DualScaledStdVector<Real>&>(x);
120 return StdVector<Real>::dot(ex);
121 //const std::vector<Element>& xval = *ex.getVector();
122 //const std::vector<Element>& yval = *(StdVector<Real>::getVector());
123 //uint dimension = yval.size();
124 //Real val = 0;
125 //for (uint i=0; i<dimension; i++) {
126 // val += yval[i]*xval[i];
127 //}
128 //return val;
129 }
130
131}; // class PrimalScaledStdVector
132
133
134
135template <class Real, class Element>
136class DualScaledStdVector : public StdVector<Real> {
137
138 typedef typename std::vector<Element>::size_type uint;
139
140private:
141
142 Ptr<std::vector<Element> > scaling_vec_;
143 mutable Ptr<PrimalScaledStdVector<Real> > primal_vec_;
144 mutable bool isDualInitialized_;
145
146public:
147
148 DualScaledStdVector(const Ptr<std::vector<Element> > & std_vec,
149 const Ptr<std::vector<Element> > & scaling_vec) :
150 StdVector<Real>(std_vec), scaling_vec_(scaling_vec),
151 isDualInitialized_(false) {}
152
153 Real dot( const Vector<Real> &x ) const {
154 const DualScaledStdVector & ex = dynamic_cast<const DualScaledStdVector&>(x);
155 const std::vector<Element>& xval = *ex.getVector();
156 const std::vector<Element>& yval = *(StdVector<Real>::getVector());
157 uint dimension = yval.size();
158 Real val = 0;
159 for (uint i=0; i<dimension; i++) {
160 val += yval[i]*xval[i]/(*scaling_vec_)[i];
161 }
162 return val;
163 }
164
165 Ptr<Vector<Real> > clone() const {
167 return makePtr<DualScaledStdVector>(
168 makePtr<std::vector<Element>>(dimension), scaling_vec_ );
169 }
170
171 const Vector<Real> & dual() const {
172 uint n = StdVector<Real>::getVector()->size();
173 if ( !isDualInitialized_ ) {
174 primal_vec_ = makePtr<PrimalScaledStdVector<Real>>(
175 makePtr<std::vector<Element>>(n),
177 isDualInitialized_ = true;
178 }
179 for (uint i = 0; i < n; i++) {
180 (*(primal_vec_->getVector()))[i]
182 }
183 return *primal_vec_;
184 }
185
186 Real apply( const Vector<Real> &x ) const {
187 const PrimalScaledStdVector<Real> & ex = dynamic_cast<const PrimalScaledStdVector<Real>&>(x);
188 return StdVector<Real>::dot(ex);
189// const std::vector<Element>& xval = *ex.getVector();
190// const std::vector<Element>& yval = *(StdVector<Real>::getVector());
191// uint dimension = yval.size();
192// Real val = 0;
193// for (uint i=0; i<dimension; i++) {
194// val += yval[i]*xval[i];
195// }
196// return val;
197 }
198
199}; // class DualScaledStdVector
200
201} // namespace ROL
202
203#endif
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Real dot(const Vector< Real > &x) const
Compute where .
DualScaledStdVector(const Ptr< std::vector< Element > > &std_vec, const Ptr< std::vector< Element > > &scaling_vec)
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
Ptr< PrimalScaledStdVector< Real > > primal_vec_
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
std::vector< Element >::size_type uint
Ptr< std::vector< Element > > scaling_vec_
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
PrimalScaledStdVector(const Ptr< std::vector< Element > > &std_vec, const Ptr< std::vector< Element > > &scaling_vec)
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
std::vector< Element >::size_type uint
Ptr< DualScaledStdVector< Real > > dual_vec_
Real dot(const Vector< Real > &x) const
Compute where .
Ptr< std::vector< Element > > scaling_vec_
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< const std::vector< Element > > getVector() const
int dimension() const
Return dimension of the vector space.
virtual Real dot(const Vector< Real > &x) const
Compute where .
Defines the linear algebra or vector space interface.