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 <algorithm>
48 #include <cstdlib>
49 
50 #include "ROL_Vector.hpp"
51 
62 namespace ROL {
63 
64 template <class Real, class Element=Real>
66 
67 template <class Real, class Element=Real>
69 
70 template <class Real, class Element>
71 class PrimalScaledStdVector : public Vector<Real> {
72 
73  typedef typename std::vector<Element>::size_type uint;
74 
75 private:
76 
77  Teuchos::RCP<std::vector<Element> > std_vec_;
78  Teuchos::RCP<std::vector<Element> > scaling_vec_;
79  mutable Teuchos::RCP<DualScaledStdVector<Real> > dual_vec_;
80 
81 public:
82 
83  PrimalScaledStdVector(const Teuchos::RCP<std::vector<Element> > & std_vec,
84  const Teuchos::RCP<std::vector<Element> > & scaling_vec) :
85  std_vec_(std_vec), scaling_vec_(scaling_vec) {}
86 
87  void set( const Vector<Real> &x ) {
88  const PrimalScaledStdVector &ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
89  const std::vector<Element>& xval = *ex.getVector();
90  std::copy(xval.begin(),xval.end(),std_vec_->begin());
91  }
92 
93  void plus( const Vector<Real> &x ) {
94  const PrimalScaledStdVector &ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
95  const std::vector<Element>& xval = *ex.getVector();
96  uint dimension = std_vec_->size();
97  for (uint i=0; i<dimension; i++) {
98  (*std_vec_)[i] += xval[i];
99  }
100  }
101 
102  void axpy( const Real alpha, const Vector<Real> &x ) {
103  const PrimalScaledStdVector &ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
104  const std::vector<Element>& xval = *ex.getVector();
105  uint dimension = std_vec_->size();
106  for (uint i=0; i<dimension; i++) {
107  (*std_vec_)[i] += alpha*xval[i];
108  }
109  }
110 
111  void scale( const Real alpha ) {
112  uint dimension = std_vec_->size();
113  for (uint i=0; i<dimension; i++) {
114  (*std_vec_)[i] *= alpha;
115  }
116  }
117 
118  Real dot( const Vector<Real> &x ) const {
119  const PrimalScaledStdVector & ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
120  const std::vector<Element>& xval = *ex.getVector();
121  uint dimension = std_vec_->size();
122  Real val = 0;
123  for (uint i=0; i<dimension; i++) {
124  val += (*std_vec_)[i]*xval[i]*(*scaling_vec_)[i];
125  }
126  return val;
127  }
128 
129  Real norm() const {
130  Real val = 0;
131  val = std::sqrt( dot(*this) );
132  return val;
133  }
134 
135  Teuchos::RCP<Vector<Real> > clone() const {
136  return Teuchos::rcp( new PrimalScaledStdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size())), scaling_vec_ ));
137  }
138 
139  Teuchos::RCP<const std::vector<Element> > getVector() const {
140  return std_vec_;
141  }
142 
143  Teuchos::RCP<std::vector<Element> > getVector() {
144  return std_vec_;
145  }
146 
147  const ROL::Vector<Real> & dual() const {
148  std::vector<Element> tmp_vec(*std_vec_);
149  tmp_vec[0] *= (*scaling_vec_)[0];
150  tmp_vec[1] *= (*scaling_vec_)[1];
151  dual_vec_ = Teuchos::rcp( new DualScaledStdVector<Real>( Teuchos::rcp(new std::vector<Element>(tmp_vec)), scaling_vec_ ) );
152  return *dual_vec_;
153  }
154 
155  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
156  Teuchos::RCP<PrimalScaledStdVector> e = Teuchos::rcp( new PrimalScaledStdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size(), 0.0)), scaling_vec_ ));
157  (*e->getVector())[i] = 1.0;
158  return e;
159  }
160 
161  int dimension() const {
162  return std_vec_->size();
163  }
164 
165  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
166  uint dimension = std_vec_->size();
167  for(uint i=0; i<dimension; ++i) {
168  (*std_vec_)[i] = f.apply((*std_vec_)[i]);
169  }
170 
171  }
172 
173  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
174  Teuchos::RCP<const std::vector<Element> > xval = Teuchos::null;
175  try {
176  const PrimalScaledStdVector & ex = Teuchos::dyn_cast<const PrimalScaledStdVector>(x);
177  xval = ex.getVector();
178  }
179  catch (std::exception &e) {
180  const DualScaledStdVector<Real> & ex = Teuchos::dyn_cast<const DualScaledStdVector<Real> >(x);
181  xval = ex.getVector();
182  }
183  uint dimension = std_vec_->size();
184  for (uint i=0; i<dimension; i++) {
185  (*std_vec_)[i] = f.apply((*std_vec_)[i],(*xval)[i]);
186  }
187 
188  }
189 
190  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
191  Real result = r.initialValue();
192  uint dimension = std_vec_->size();
193  for(uint i=0; i<dimension; ++i) {
194  r.reduce((*std_vec_)[i],result);
195  }
196  return result;
197  }
198 
199 }; // class PrimalScaledStdVector
200 
201 
202 
203 template <class Real, class Element>
204 class DualScaledStdVector : public Vector<Real> {
205 
206  typedef typename std::vector<Element>::size_type uint;
207 
208 private:
209 
210  Teuchos::RCP<std::vector<Element> > std_vec_;
211  Teuchos::RCP<std::vector<Element> > scaling_vec_;
212  mutable Teuchos::RCP<PrimalScaledStdVector<Real> > dual_vec_;
213 
214 public:
215 
216  DualScaledStdVector(const Teuchos::RCP<std::vector<Element> > & std_vec,
217  const Teuchos::RCP<std::vector<Element> > & scaling_vec) :
218  std_vec_(std_vec), scaling_vec_(scaling_vec) {}
219 
220  void set( const Vector<Real> &x ) {
221  const DualScaledStdVector &ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
222  const std::vector<Element>& xval = *ex.getVector();
223  std::copy(xval.begin(),xval.end(),std_vec_->begin());
224  }
225 
226  void plus( const Vector<Real> &x ) {
227  const DualScaledStdVector &ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
228  const std::vector<Element>& xval = *ex.getVector();
229  uint dimension = std_vec_->size();
230  for (uint i=0; i<dimension; i++) {
231  (*std_vec_)[i] += xval[i];
232  }
233  }
234 
235  void axpy( const Real alpha, const Vector<Real> &x ) {
236  const DualScaledStdVector &ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
237  const std::vector<Element>& xval = *ex.getVector();
238  uint dimension = std_vec_->size();
239  for (uint i=0; i<dimension; i++) {
240  (*std_vec_)[i] += alpha*xval[i];
241  }
242  }
243 
244  void scale( const Real alpha ) {
245  uint dimension = std_vec_->size();
246  for (uint i=0; i<dimension; i++) {
247  (*std_vec_)[i] *= alpha;
248  }
249  }
250 
251  Real dot( const Vector<Real> &x ) const {
252  const DualScaledStdVector & ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
253  const std::vector<Element>& xval = *ex.getVector();
254  uint dimension = std_vec_->size();
255  Real val = 0;
256  for (uint i=0; i<dimension; i++) {
257  val += (*std_vec_)[i]*xval[i]/(*scaling_vec_)[i];
258  }
259  return val;
260  }
261 
262  Real norm() const {
263  Real val = 0;
264  val = std::sqrt( dot(*this) );
265  return val;
266  }
267 
268  Teuchos::RCP<Vector<Real> > clone() const {
269  return Teuchos::rcp( new DualScaledStdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size())), scaling_vec_ ));
270  }
271 
272  Teuchos::RCP<const std::vector<Element> > getVector() const {
273  return std_vec_;
274  }
275 
276  Teuchos::RCP<std::vector<Element> > getVector() {
277  return std_vec_;
278  }
279 
280  const ROL::Vector<Real> & dual() const {
281  std::vector<Element> tmp_vec(*std_vec_);
282  tmp_vec[0] /= (*scaling_vec_)[0];
283  tmp_vec[1] /= (*scaling_vec_)[1];
284  dual_vec_ = Teuchos::rcp( new PrimalScaledStdVector<Real>( Teuchos::rcp(new std::vector<Element>(tmp_vec)), scaling_vec_ ) );
285  return *dual_vec_;
286  }
287 
288  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
289  Teuchos::RCP<DualScaledStdVector> e = Teuchos::rcp( new DualScaledStdVector( Teuchos::rcp(new std::vector<Element>(std_vec_->size(), 0.0)), scaling_vec_ ));
290  (*e->getVector())[i] = 1.0;
291  return e;
292  }
293 
294  int dimension() const {
295  return std_vec_->size();
296  }
297 
298  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
299  uint dimension = std_vec_->size();
300  for(uint i=0; i<dimension; ++i) {
301  (*std_vec_)[i] = f.apply((*std_vec_)[i]);
302  }
303 
304  }
305 
306  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
307  Teuchos::RCP<const std::vector<Element> > xval = Teuchos::null;
308  try {
309  const DualScaledStdVector & ex = Teuchos::dyn_cast<const DualScaledStdVector>(x);
310  xval = ex.getVector();
311  }
312  catch (std::exception &e) {
313  const PrimalScaledStdVector<Real> & ex = Teuchos::dyn_cast<const PrimalScaledStdVector<Real> >(x);
314  xval = ex.getVector();
315  }
316  uint dimension = std_vec_->size();
317  for (uint i=0; i<dimension; i++) {
318  (*std_vec_)[i] = f.apply((*std_vec_)[i],(*xval)[i]);
319  }
320 
321  }
322 
323  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
324  Real result = r.initialValue();
325  uint dimension = std_vec_->size();
326  for(uint i=0; i<dimension; ++i) {
327  r.reduce((*std_vec_)[i],result);
328  }
329  return result;
330  }
331 
332 }; // class DualScaledStdVector
333 
334 
335 } // namespace ROL
336 
337 #endif
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
std::vector< Element >::size_type uint
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< PrimalScaledStdVector< Real > > dual_vec_
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Real norm() const
Returns where .
PrimalScaledStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec, const Teuchos::RCP< std::vector< Element > > &scaling_vec)
void scale(const Real alpha)
Compute where .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
std::vector< Element >::size_type uint
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Real dot(const Vector< Real > &x) const
Compute where .
Teuchos::RCP< std::vector< Element > > std_vec_
Real dot(const Vector< Real > &x) const
Compute where .
Teuchos::RCP< std::vector< Element > > getVector()
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< const std::vector< Element > > getVector() const
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< DualScaledStdVector< Real > > dual_vec_
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
DualScaledStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec, const Teuchos::RCP< std::vector< Element > > &scaling_vec)
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< const std::vector< Element > > getVector() const
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< std::vector< Element > > getVector()
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Teuchos::RCP< std::vector< Element > > scaling_vec_
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< std::vector< Element > > std_vec_
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
void scale(const Real alpha)
Compute where .
Teuchos::RCP< std::vector< Element > > scaling_vec_
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .