ROL
ROL_SROMVector.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_SROMVECTOR_H
45 #define ROL_SROMVECTOR_H
46 
47 #include <algorithm>
48 #include <cstdlib>
49 
50 #include "ROL_Vector.hpp"
51 
57 namespace ROL {
58 
59 template <class Real, class Element=Real>
60 class SROMVector : public Vector<Real> {
61 private:
62 
63  Teuchos::RCP<std::vector<Element> > pts_vec_;
64  Teuchos::RCP<std::vector<Element> > wts_vec_;
65  size_t dimension_;
66  size_t numSamples_;
67 
68  Real const_pt_;
69  Real const_wt_;
70 
71 public:
72 
73  SROMVector(const Teuchos::RCP<std::vector<Element> > &pts_vec,
74  const Teuchos::RCP<std::vector<Element> > &wts_vec,
75  const Real const_pt = 1.0, const Real const_wt = 1.0)
76  : pts_vec_(pts_vec), wts_vec_(wts_vec),
77  const_pt_(const_pt), const_wt_(const_wt) {
78  numSamples_ = wts_vec_->size();
79  dimension_ = pts_vec_->size()/numSamples_;
80  }
81 
82  void set( const Vector<Real> &x ) {
83  const SROMVector &ex = Teuchos::dyn_cast<const SROMVector>(x);
84  for (size_t i = 0; i < numSamples_; i++) {
85  for (size_t j = 0; j < dimension_; j++) {
86  (*pts_vec_)[i*dimension_ + j] = (*ex.getPoint(i))[j];
87  }
88  (*wts_vec_)[i] = (ex.getWeight(i));
89  }
90  }
91 
92  void plus( const Vector<Real> &x ) {
93  const SROMVector &ex = Teuchos::dyn_cast<const SROMVector>(x);
94  for (size_t i = 0; i < numSamples_; i++) {
95  for (size_t j = 0; j < dimension_; j++) {
96  (*pts_vec_)[i*dimension_ + j] += (*ex.getPoint(i))[j];
97  }
98  (*wts_vec_)[i] += (ex.getWeight(i));
99  }
100  }
101 
102  void scale( const Real alpha ) {
103  for (size_t i = 0; i < numSamples_; i++) {
104  for (size_t j = 0; j < dimension_; j++) {
105  (*pts_vec_)[i*dimension_ + j] *= alpha;
106  }
107  (*wts_vec_)[i] *= alpha;
108  }
109  }
110 
111  Real dot( const Vector<Real> &x ) const {
112  const SROMVector & ex = Teuchos::dyn_cast<const SROMVector>(x);
113  Real pt_val = 0, wt_val = 0;
114  for (size_t i = 0; i < numSamples_; i++) {
115  for (size_t j = 0; j < dimension_; j++) {
116  pt_val += (*pts_vec_)[i*dimension_ + j] * (*ex.getPoint(i))[j];
117  }
118  wt_val += (*wts_vec_)[i] * (ex.getWeight(i));
119  }
120  return const_pt_*pt_val + const_wt_*wt_val;
121  }
122 
123  Real norm() const {
124  Real val = 0;
125  val = std::sqrt( dot(*this) );
126  return val;
127  }
128 
129  Teuchos::RCP<Vector<Real> > clone() const {
130  return Teuchos::rcp( new SROMVector( Teuchos::rcp(new std::vector<Element>(pts_vec_->size())),
131  Teuchos::rcp(new std::vector<Element>(wts_vec_->size())),
132  const_pt_, const_wt_ ) );
133  }
134 
135  Teuchos::RCP<const std::vector<Element> > getPoint(const size_t i) const {
136  std::vector<Element> pt(dimension_,0.);
137  for (size_t j = 0; j < dimension_; j++) {
138  pt[j] = (*pts_vec_)[i*dimension_ + j];
139  }
140  return Teuchos::rcp(new std::vector<Element>(pt));
141  }
142 
143  void setPoint(const size_t i, const std::vector<Element> &pt) {
144  for (size_t j = 0; j < dimension_; j++) {
145  (*pts_vec_)[i*dimension_ + j] = pt[j];
146  }
147  }
148 
149  const Element getWeight(const size_t i) const {
150  return (*wts_vec_)[i];
151  }
152 
153  void setWeight(const size_t i, const Element wt) {
154  (*wts_vec_)[i] = wt;
155  }
156 
157  const size_t getDimension(void) const {
158  return dimension_;
159  }
160 
161  const size_t getNumSamples(void) const {
162  return numSamples_;
163  }
164 
165 }; // class SROMVector
166 
167 } // namespace ROL
168 
169 #endif
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< const std::vector< Element > > getPoint(const size_t i) const
const size_t getDimension(void) const
const Element getWeight(const size_t i) const
void setWeight(const size_t i, const Element wt)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
const size_t getNumSamples(void) const
Teuchos::RCP< std::vector< Element > > pts_vec_
Provides the std::vector implementation of the ROL::Vector interface.
Real dot(const Vector< Real > &x) const
Compute where .
SROMVector(const Teuchos::RCP< std::vector< Element > > &pts_vec, const Teuchos::RCP< std::vector< Element > > &wts_vec, const Real const_pt=1.0, const Real const_wt=1.0)
Teuchos::RCP< std::vector< Element > > wts_vec_
void scale(const Real alpha)
Compute where .
void plus(const Vector< Real > &x)
Compute , where .
void setPoint(const size_t i, const std::vector< Element > &pt)
Real norm() const
Returns where .