ROL
ROL_SingletonVector.hpp
Go to the documentation of this file.
1 
2 // @HEADER
3 // ************************************************************************
4 //
5 // Rapid Optimization Library (ROL) Package
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact lead developers:
39 // Drew Kouri (dpkouri@sandia.gov) and
40 // Denis Ridzal (dridzal@sandia.gov)
41 //
42 // ************************************************************************
43 // @HEADER
44 
45 #ifndef ROL_SINGLETONVECTOR_H
46 #define ROL_SINGLETONVECTOR_H
47 
48 #include "ROL_Vector.hpp"
49 
56 namespace ROL {
57 
58 template<class Real>
59 class SingletonVector : public Vector<Real> {
60 
61  using V = Vector<Real>;
62 
63 private:
64 
65  Real value_;
66 
67  Real getValueX( const V& x ) const {
68  return dynamic_cast<const SingletonVector<Real>&>(x).getValue();
69  }
70 
71 public:
72 
73  SingletonVector(const Real& value=0) : value_(value) {}
74 
75  Real getValue() const { return value_; }
76  void setValue( const Real& v ) { value_=v; }
77 
78  void set( const V& x ) {
79  value_ = getValueX(x);
80  }
81 
82  void plus( const V& x ) {
83  value_ += getValueX(x);
84  }
85 
86  void axpy( const Real alpha, const V& x ) {
87  value_ += alpha*getValueX(x);
88  }
89 
90  void scale( const Real alpha ) {
91  value_ *= alpha;
92  }
93 
94  Real dot( const V& x ) const {
95  Real xv = getValueX(x);
96  xv *= value_;
97  return xv;
98  }
99 
100  Real norm() const {
101  return std::abs(value_);
102  }
103 
104  ROL::Ptr<V> clone() const {
105  return ROL::makePtr<SingletonVector>(0);
106  }
107 
108  ROL::Ptr<V> basis(const int i) const {
109  ROL_TEST_FOR_EXCEPTION( i >= 1 || i < 0,
110  std::invalid_argument,
111  "Error: Basis index must be between 0 and vector dimension." );
112  return ROL::makePtr<SingletonVector>(1);
113  }
114 
115  int dimension() const { return 1; };
116 
117  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
118  value_ = f.apply(value_);
119  }
120 
121  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const V& x ) {
122  value_ = f.apply(value_,getValueX(x));
123  }
124 
125  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
126  return value_;
127  }
128 
129  void setScalar( const Real C ) {
130  value_ = C;
131  }
132 
133  void randomize( const Real l=0.0, const Real u=1.0 ) {
134  Real a = (u-l);
135  Real b = l;
136  Real x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
137  value_ = a*x + b;
138  }
139 
140  void print( std::ostream& os ) const {
141  os << value_ << std::endl;
142  }
143 
144 };
145 
146 
147 } // namespace ROL
148 
149 
150 
151 
152 #endif // ROL_SINGLETONVECTOR_H
153 
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
ROL::Ptr< V > clone() const
Clone to make a new (uninitialized) vector.
ROL::Objective_SimOpt value
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void print(std::ostream &os) const
Real dot(const V &x) const
Compute where .
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
ROL::Ptr< V > basis(const int i) const
Return i-th basis vector.
int dimension() const
Return dimension of the vector space.
void scale(const Real alpha)
Compute where .
void set(const V &x)
Set where .
Real norm() const
Returns where .
void plus(const V &x)
Compute , where .
void axpy(const Real alpha, const V &x)
Compute where .
Real getValueX(const V &x) const
SingletonVector(const Real &value=0)
void setValue(const Real &v)
void setScalar(const Real C)
Set where .