ROL
ROL_ScaledStdVector.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_SCALEDSTDVECTOR_H
11#define ROL_SCALEDSTDVECTOR_H
12
13#include "ROL_StdVector.hpp"
14
25namespace ROL {
26
27template <class Real, class Element=Real>
28class PrimalScaledStdVector;
29
30template <class Real, class Element=Real>
31class DualScaledStdVector;
32
33template <class Real, class Element>
34class PrimalScaledStdVector : public StdVector<Real> {
35
36 typedef typename std::vector<Element>::size_type uint;
37
38private:
39
40 Ptr<std::vector<Element> > scaling_vec_;
41 mutable Ptr<DualScaledStdVector<Real> > dual_vec_;
42 mutable bool isDualInitialized_;
43
44public:
45
46 PrimalScaledStdVector(const Ptr<std::vector<Element> > & std_vec,
47 const Ptr<std::vector<Element> > & scaling_vec) :
48 StdVector<Real>(std_vec), scaling_vec_(scaling_vec),
49 isDualInitialized_(false) {}
50
51 Real dot( const Vector<Real> &x ) const {
52 const PrimalScaledStdVector & ex = dynamic_cast<const PrimalScaledStdVector&>(x);
53 const std::vector<Element>& xval = *ex.getVector();
54 const std::vector<Element>& yval = *(StdVector<Real>::getVector());
55 uint dimension = yval.size();
56 Real val = 0;
57 for (uint i=0; i<dimension; i++) {
58 val += yval[i]*xval[i]*(*scaling_vec_)[i];
59 }
60 return val;
61 }
62
63 Ptr<Vector<Real> > clone() const {
65 return makePtr<PrimalScaledStdVector>(
66 makePtr<std::vector<Element>>(dimension), scaling_vec_ );
67 }
68
69 const Vector<Real> & dual() const {
70 uint n = StdVector<Real>::getVector()->size();
71 if ( !isDualInitialized_ ) {
72 dual_vec_ = makePtr<DualScaledStdVector<Real>>(
73 makePtr<std::vector<Element>>(n),
75 isDualInitialized_ = true;
76 }
77 for (uint i = 0; i < n; i++) {
78 (*(dual_vec_->getVector()))[i]
79 = (*scaling_vec_)[i]*(*StdVector<Real>::getVector())[i];
80 }
81 return *dual_vec_;
82 }
83
84 Real apply( const Vector<Real> &x ) const {
85 const DualScaledStdVector<Real> & ex = dynamic_cast<const DualScaledStdVector<Real>&>(x);
86 return StdVector<Real>::dot(ex);
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];
93 //}
94 //return val;
95 }
96
97}; // class PrimalScaledStdVector
98
99
100
101template <class Real, class Element>
102class DualScaledStdVector : public StdVector<Real> {
103
104 typedef typename std::vector<Element>::size_type uint;
105
106private:
107
108 Ptr<std::vector<Element> > scaling_vec_;
109 mutable Ptr<PrimalScaledStdVector<Real> > primal_vec_;
110 mutable bool isDualInitialized_;
111
112public:
113
114 DualScaledStdVector(const Ptr<std::vector<Element> > & std_vec,
115 const Ptr<std::vector<Element> > & scaling_vec) :
116 StdVector<Real>(std_vec), scaling_vec_(scaling_vec),
117 isDualInitialized_(false) {}
118
119 Real dot( const Vector<Real> &x ) const {
120 const DualScaledStdVector & ex = dynamic_cast<const DualScaledStdVector&>(x);
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]/(*scaling_vec_)[i];
127 }
128 return val;
129 }
130
131 Ptr<Vector<Real> > clone() const {
133 return makePtr<DualScaledStdVector>(
134 makePtr<std::vector<Element>>(dimension), scaling_vec_ );
135 }
136
137 const Vector<Real> & dual() const {
138 uint n = StdVector<Real>::getVector()->size();
139 if ( !isDualInitialized_ ) {
140 primal_vec_ = makePtr<PrimalScaledStdVector<Real>>(
141 makePtr<std::vector<Element>>(n),
143 isDualInitialized_ = true;
144 }
145 for (uint i = 0; i < n; i++) {
146 (*(primal_vec_->getVector()))[i]
148 }
149 return *primal_vec_;
150 }
151
152 Real apply( const Vector<Real> &x ) const {
153 const PrimalScaledStdVector<Real> & ex = dynamic_cast<const PrimalScaledStdVector<Real>&>(x);
154 return StdVector<Real>::dot(ex);
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];
161// }
162// return val;
163 }
164
165}; // class DualScaledStdVector
166
167} // namespace ROL
168
169#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.