ROL
ROL_ScaledVector.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 #pragma once
11 #ifndef ROL_SCALEDVECTOR_HPP
12 #define ROL_SCALEDVECTOR_HPP
13 
28 #include "ROL_WrappedVector.hpp"
29 #include "ROL_VectorWorkspace.hpp"
30 
31 namespace ROL {
32 
33 // Forward declaration
34 template<typename Real> class PrimalScaledVector;
35 template<typename Real> class DualScaledVector;
36 
37 template<typename Real>
38 class PrimalScaledVector : public WrappedVector<Real> {
39 
40  using V = Vector<Real>;
43 
44 private:
45 
46  mutable Ptv<V> scaling_vec_;
48 
49  Elementwise::Multiply<Real> mult_;
50 
51 protected:
52 
54 
55  // y <- y*x elementwise
56  void multiply_scaling( const Ptr<V>& y ) const {
57  y->applyBinary( mult_, *scaling_vec_ );
58  }
59 
60 public:
61 
62  PrimalScaledVector( const Ptr<V>& vec, const Ptr<V>& scaling_vec ) :
63  WrappedVector<Real>(vec), scaling_vec_(scaling_vec) {}
64 
65  virtual ~PrimalScaledVector() {}
66 
67  virtual Real dot( const V& x ) const override {
68  auto y = workspace_.copy(x);
69  multiply_scaling( y );
70  return this->getVector()->dot(*y);
71  }
72 
73  virtual Ptr<V> clone() const override {
74  return makePtr<VPrim>( this->getVector()->clone(), scaling_vec_ );
75  }
76 
77  virtual Ptr<V> basis( const int i ) const override {
78  return makePtr<VPrim>( this->getVector()->basis(i), scaling_vec_ );
79  }
80 
81  virtual void const V& dual() const override {
82  auto dual_vec = workspace_.copy( this->getVector() );
83  multiply_scaling( dual_vec );
84  return *( makePtr<VDual>( dual_vec, scaling_vec ) );
85  }
86 
87  const Ptr<V>& getScalingVector() { return scaling_vec_; }
88  const Ptr<const V>& getScalingVector() const { return scaling_vec_; }
89 
90  void setScalingVector( const Ptr<const V&>& scaling_vec ) const {
91  scaling_vec_ = scaling_vec;
92  }
93 
94 }; // class PrimalScaledVector
95 
96 
97 
98 template<typename Real>
99 class DualScaledVector : public WrappedVector<Real> {
100 
101  using V = Vector<Real>;
104 
105 private:
106 
107  mutable Ptv<V> scaling_vec_;
109 
110  Elementwise::Divide<Real> div_;
111 
112 protected:
113 
115 
116  // y <- y/x elementwise
117  void divide_scaling( const <V>& y ) const {
118  y->applyBinary( div_, *scaling_vec_ );
119  }
120 
121 public:
122 
123  DualScaledVector( const Ptr<V>& vec, const Ptr<V>& scaling_vec ) :
124  WrappedVector<Real>(vec), scaling_vec_(scaling_vec) {}
125 
126  virtual ~DualScaledVector() {}
127 
128  virtual Real dot( const V& x ) const override {
129  auto y = workspace_.copy(x);
130  divide_scaling( y );
131  return this->getVector()->dot(*y);
132  }
133 
134  virtual Ptr<V> clone() const override {
135  return makePtr<VDual>( this->getVector()->clone(), scaling_vec_ );
136  }
137 
138  virtual Ptr<V> basis( const int i ) const override {
139  return makePtr<VDual>( this->getVector()->basis(i), scaling_vec_ );
140  }
141 
142  virtual void const V& dual() const override {
143  auto primal_vec = workspace_.copy( this->getVector() );
144  divide_scaling( primal_vec );
145  return *( makePtr<VPrim>( primal_vec, scaling_vec ) );
146  }
147 
148  const Ptr<V>& getScalingVector() { return scaling_vec_; }
149  const Ptr<const V>& getScalingVector() const { return scaling_vec_; }
150 
151  void setScalingVector( const Ptr<const V&>& scaling_vec ) const {
152  scaling_vec_ = scaling_vec;
153  }
154 
155 }; // class PrimalScaledVector
156 
157 } // namespace ROL
158 
159 
160 #endif
PrimalScaledVector(const Ptr< V > &vec, const Ptr< V > &scaling_vec)
Elementwise::Multiply< Real > mult_
virtual Ptr< V > basis(const int i) const override
const Ptr< V > & getScalingVector()
VectorWorkspace< Real > & getWorkspace() const
const Ptr< const V > & getScalingVector() const
const Ptr< const V > & getScalingVector() const
void multiply_scaling(const Ptr< V > &y) const
Elementwise::Divide< Real > div_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
const Ptr< V > & getVector()
Provides the implementation of the ROL::Vector interface that handles scalings in the inner product...
Provides the implementation of the ROL::Vector interface that handles scalings in the inner product...
virtual Ptr< V > clone() const override
void setScalingVector(const Ptr< const V & > &scaling_vec) const
virtual Real dot(const V &x) const override
void setScalingVector(const Ptr< const V & > &scaling_vec) const
VectorWorkspace< Real > & getWorkspace() const
virtual Ptr< V > basis(const int i) const override
virtual Real dot(const V &x) const override
const Ptr< V > & getScalingVector()
Provides an interface layer which encapulates a pointer to a ROL::Vector and has the default behavior...
void divide_scaling(const < V > &y) const
virtual Ptr< V > clone() const override
virtual void const V & dual() const override
VectorWorkspace< Real > workspace_
DualScaledVector(const Ptr< V > &vec, const Ptr< V > &scaling_vec)
VectorWorkspace< Real > workspace_
virtual void const V & dual() const override