ROL
ROL_Vector_SimOpt.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_VECTOR_SIMOPT_HPP
45 #define ROL_VECTOR_SIMOPT_HPP
46 
47 #include "ROL_Vector.hpp"
48 
54 namespace ROL {
55 
56 template<class Real>
57 class Vector_SimOpt : public Vector<Real> {
58 private:
59  Teuchos::RCP<Vector<Real> > vec1_;
60  Teuchos::RCP<Vector<Real> > vec2_;
61  mutable Teuchos::RCP<Vector<Real> > dual_vec1_;
62  mutable Teuchos::RCP<Vector<Real> > dual_vec2_;
63  mutable Teuchos::RCP<Vector_SimOpt<Real> > dual_vec_;
64 
65 public:
66  Vector_SimOpt( const Teuchos::RCP<Vector<Real> > &vec1, const Teuchos::RCP<Vector<Real> > &vec2 )
67  : vec1_(vec1), vec2_(vec2) {
68  dual_vec1_ = (vec1_->dual()).clone();
69  dual_vec2_ = (vec2_->dual()).clone();
70  }
71 
72  void plus( const Vector<Real> &x ) {
73  const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
74  Teuchos::dyn_cast<const Vector<Real> >(x));
75  vec1_->plus(*(xs.get_1()));
76  vec2_->plus(*(xs.get_2()));
77  }
78 
79  void scale( const Real alpha ) {
80  vec1_->scale(alpha);
81  vec2_->scale(alpha);
82  }
83 
84  void axpy( const Real alpha, const Vector<Real> &x ) {
85  const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
86  Teuchos::dyn_cast<const Vector<Real> >(x));
87  vec1_->axpy(alpha,*(xs.get_1()));
88  vec2_->axpy(alpha,*(xs.get_2()));
89  }
90 
91  Real dot( const Vector<Real> &x ) const {
92  const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(
93  Teuchos::dyn_cast<const Vector<Real> >(x));
94  return vec1_->dot(*(xs.get_1())) + vec2_->dot(*(xs.get_2()));
95  }
96 
97  Real norm() const {
98  Real norm1 = vec1_->norm();
99  Real norm2 = vec2_->norm();
100  return sqrt( norm1*norm1 + norm2*norm2 );
101  }
102 
103  Teuchos::RCP<Vector<Real> > clone() const {
104  return Teuchos::rcp( new Vector_SimOpt(vec1_->clone(),vec2_->clone()) );
105  }
106 
107  const Vector<Real> & dual(void) const {
108  dual_vec1_->set(vec1_->dual());
109  dual_vec2_->set(vec2_->dual());
110  dual_vec_ = Teuchos::rcp( new Vector_SimOpt<Real>(dual_vec1_,dual_vec2_) );
111  return *dual_vec_;
112  }
113 
114  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
115  int n1 = (vec1_)->dimension();
116  if ( i < n1 ) {
117  Teuchos::RCP<Vector<Real> > e1 = (vec1_)->basis(i);
118  Teuchos::RCP<Vector<Real> > e2 = (vec2_)->clone(); e2->zero();
119  Teuchos::RCP<Vector<Real> > e = Teuchos::rcp(new Vector_SimOpt<Real>(e1,e2));
120  return e;
121  }
122  else {
123  Teuchos::RCP<Vector<Real> > e1 = (vec1_)->clone(); e1->zero();
124  Teuchos::RCP<Vector<Real> > e2 = (vec2_)->basis(i-n1);
125  Teuchos::RCP<Vector<Real> > e = Teuchos::rcp(new Vector_SimOpt<Real>(e1,e2));
126  return e;
127  }
128  }
129 
130  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
131 
132  vec1_->applyUnary(f);
133  vec2_->applyUnary(f);
134 
135  }
136 
137  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
138  const Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x);
139 
140  vec1_->applyBinary(f,*xs.get_1());
141  vec2_->applyBinary(f,*xs.get_2());
142 
143  }
144 
145  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
146 
147  Real result = r.initialValue();
148  r.reduce(vec1_->reduce(r),result);
149  r.reduce(vec2_->reduce(r),result);
150  return result;
151  }
152 
153 
154  int dimension() const {
155  return (vec1_)->dimension() + (vec2_)->dimension();
156  }
157 
158  Teuchos::RCP<const Vector<Real> > get_1() const {
159  return vec1_;
160  }
161 
162  Teuchos::RCP<const Vector<Real> > get_2() const {
163  return vec2_;
164  }
165 
166  Teuchos::RCP<Vector<Real> > get_1() {
167  return vec1_;
168  }
169 
170  Teuchos::RCP<Vector<Real> > get_2() {
171  return vec2_;
172  }
173 
174  void set_1(const Vector<Real>& vec) {
175  vec1_->set(vec);
176  }
177 
178  void set_2(const Vector<Real>& vec) {
179  vec2_->set(vec);
180  }
181 };
182 
183 }
184 
185 #endif
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface for simulation-based optimization.
Teuchos::RCP< const Vector< Real > > get_2() const
void set_1(const Vector< Real > &vec)
Teuchos::RCP< Vector< Real > > vec2_
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
int dimension() const
Return dimension of the vector space.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< Vector< Real > > dual_vec2_
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Real dot(const Vector< Real > &x) const
Compute where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void scale(const Real alpha)
Compute where .
Teuchos::RCP< Vector< Real > > vec1_
Teuchos::RCP< const Vector< Real > > get_1() const
Teuchos::RCP< Vector< Real > > get_1()
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Real norm() const
Returns where .
Teuchos::RCP< Vector< Real > > get_2()
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Vector_SimOpt(const Teuchos::RCP< Vector< Real > > &vec1, const Teuchos::RCP< Vector< Real > > &vec2)
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< Vector< Real > > dual_vec1_
Teuchos::RCP< Vector_SimOpt< Real > > dual_vec_
void set_2(const Vector< Real > &vec)