ROL
ROL_SingletonVector.hpp
Go to the documentation of this file.
1
2// @HEADER
3// *****************************************************************************
4// Rapid Optimization Library (ROL) Package
5//
6// Copyright 2014 NTESS and the ROL contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
11#ifndef ROL_SINGLETONVECTOR_H
12#define ROL_SINGLETONVECTOR_H
13
14#include "ROL_Vector.hpp"
15
22namespace ROL {
23
24template<class Real>
25class SingletonVector : public Vector<Real> {
26
27 using V = Vector<Real>;
28
29private:
30
31 Real value_;
32
33 Real getValueX( const V& x ) const {
34 return dynamic_cast<const SingletonVector<Real>&>(x).getValue();
35 }
36
37public:
38
39 SingletonVector(Real value = Real(0)) : value_(value) {}
40
41 Real getValue() const { return value_; }
42 void setValue( Real v ) { value_ = v; }
43
44 void set( const V& x ) {
45 value_ = getValueX(x);
46 }
47
48 void plus( const V& x ) {
49 value_ += getValueX(x);
50 }
51
52 void axpy( const Real alpha, const V& x ) {
53 value_ += alpha*getValueX(x);
54 }
55
56 void scale( const Real alpha ) {
57 value_ *= alpha;
58 }
59
60 Real dot( const V& x ) const {
61 Real xv = getValueX(x);
62 xv *= value_;
63 return xv;
64 }
65
66 Real norm() const {
67 return std::abs(value_);
68 }
69
70 ROL::Ptr<V> clone() const {
71 return ROL::makePtr<SingletonVector>(0);
72 }
73
74 ROL::Ptr<V> basis(const int i) const {
75 ROL_TEST_FOR_EXCEPTION( i >= 1 || i < 0,
76 std::invalid_argument,
77 "Error: Basis index must be between 0 and vector dimension." );
78 return ROL::makePtr<SingletonVector>(1);
79 }
80
81 int dimension() const { return 1; };
82
83 void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
84 value_ = f.apply(value_);
85 }
86
87 void applyBinary( const Elementwise::BinaryFunction<Real> &f, const V& x ) {
88 value_ = f.apply(value_,getValueX(x));
89 }
90
91 Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
92 return value_;
93 }
94
95 void setScalar( const Real C ) {
96 value_ = C;
97 }
98
99 void randomize( const Real l=0.0, const Real u=1.0 ) {
100 Real a = (u-l);
101 Real b = l;
102 Real x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
103 value_ = a*x + b;
104 }
105
106 void print( std::ostream& os ) const {
107 os << value_ << std::endl;
108 }
109
110};
111
112
113} // namespace ROL
114
115
116
117
118#endif // ROL_SINGLETONVECTOR_H
119
void print(std::ostream &os) const
void scale(const Real alpha)
Compute where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void setScalar(const Real C)
Set where .
void axpy(const Real alpha, const V &x)
Compute where .
Real dot(const V &x) const
Compute where .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
ROL::Ptr< V > clone() const
Clone to make a new (uninitialized) vector.
ROL::Ptr< V > basis(const int i) const
Return i-th basis vector.
void plus(const V &x)
Compute , where .
void set(const V &x)
Set where .
SingletonVector(Real value=Real(0))
Real getValueX(const V &x) const
Real norm() const
Returns where .
int dimension() const
Return dimension of the vector space.
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Defines the linear algebra or vector space interface.
ROL::Objective_SerialSimOpt Objective_SimOpt value(const V &u, const V &z, Real &tol) override