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 49 /** \class ROL::PrimalScaledStdVector 50 \brief Provides the std::vector implementation of the ROL::Vector interface 51 that handles scalings in the inner product. Also see ROL::DualScaledStdVector. 52 */ 53 54 /** \class ROL::DualScaledStdVector 55 \brief Provides the std::vector implementation of the ROL::Vector interface 56 that handles scalings in the inner product. Also see ROL::PrimalScaledStdVector. 57 */ 58 59 namespace ROL { 60 61 template <class Real, class Element=Real> 62 class PrimalScaledStdVector; 63 64 template <class Real, class Element=Real> 65 class DualScaledStdVector; 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 Ptr<std::vector<Element> > scaling_vec_; 75 mutable Ptr<DualScaledStdVector<Real> > dual_vec_; 76 mutable bool isDualInitialized_; 77 78 public: 79 PrimalScaledStdVector(const Ptr<std::vector<Element>> & std_vec,const Ptr<std::vector<Element>> & scaling_vec)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 dot(const Vector<Real> & x) const85 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 clone() const97 Ptr<Vector<Real> > clone() const { 98 uint dimension = (StdVector<Real>::getVector())->size(); 99 return makePtr<PrimalScaledStdVector>( 100 makePtr<std::vector<Element>>(dimension), scaling_vec_ ); 101 } 102 dual() const103 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), 108 scaling_vec_); 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 }; // class PrimalScaledStdVector 119 120 121 122 template <class Real, class Element> 123 class DualScaledStdVector : public StdVector<Real> { 124 125 typedef typename std::vector<Element>::size_type uint; 126 127 private: 128 129 Ptr<std::vector<Element> > scaling_vec_; 130 mutable Ptr<PrimalScaledStdVector<Real> > primal_vec_; 131 mutable bool isDualInitialized_; 132 133 public: 134 DualScaledStdVector(const Ptr<std::vector<Element>> & std_vec,const Ptr<std::vector<Element>> & scaling_vec)135 DualScaledStdVector(const Ptr<std::vector<Element> > & std_vec, 136 const Ptr<std::vector<Element> > & scaling_vec) : 137 StdVector<Real>(std_vec), scaling_vec_(scaling_vec), 138 isDualInitialized_(false) {} 139 dot(const Vector<Real> & x) const140 Real dot( const Vector<Real> &x ) const { 141 const DualScaledStdVector & ex = dynamic_cast<const DualScaledStdVector&>(x); 142 const std::vector<Element>& xval = *ex.getVector(); 143 const std::vector<Element>& yval = *(StdVector<Real>::getVector()); 144 uint dimension = yval.size(); 145 Real val = 0; 146 for (uint i=0; i<dimension; i++) { 147 val += yval[i]*xval[i]/(*scaling_vec_)[i]; 148 } 149 return val; 150 } 151 clone() const152 Ptr<Vector<Real> > clone() const { 153 uint dimension = (StdVector<Real>::getVector())->size(); 154 return makePtr<DualScaledStdVector>( 155 makePtr<std::vector<Element>>(dimension), scaling_vec_ ); 156 } 157 dual() const158 const Vector<Real> & dual() const { 159 uint n = StdVector<Real>::getVector()->size(); 160 if ( !isDualInitialized_ ) { 161 primal_vec_ = makePtr<PrimalScaledStdVector<Real>>( 162 makePtr<std::vector<Element>>(n), 163 scaling_vec_); 164 isDualInitialized_ = true; 165 } 166 for (uint i = 0; i < n; i++) { 167 (*(primal_vec_->getVector()))[i] 168 = (*StdVector<Real>::getVector())[i]/(*scaling_vec_)[i]; 169 } 170 return *primal_vec_; 171 } 172 173 }; // class DualScaledStdVector 174 175 } // namespace ROL 176 177 #endif 178