Debian Codegraph @

packages/rol/adapters/teuchos/src/vector/ROL_TeuchosVector.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef ROL_TEUCHOSVECTOR_H
11 #define ROL_TEUCHOSVECTOR_H
12
13 #include <type_traits>
14
15 #include "Teuchos_SerialDenseVector.hpp"
16 #include "ROL_ElementwiseVector.hpp"
17
18
19 /** \class ROL::TeuchosVector
20 \brief Implements the ROL::Vector interface for a Teuchos::SerialDenseVector
21 */
22
23 namespace ROL {
24
25 template<class Ordinal, class Real>
26 class TeuchosVector : public ElementwiseVector<Real> {
27
28 using SerialDenseVector = Teuchos::SerialDenseVector<Ordinal,Real>;
29
30 using UnaryFunction = ROL::Elementwise::UnaryFunction<Real>;
31 using BinaryFunction = ROL::Elementwise::BinaryFunction<Real>;
32 using ReductionOp = ROL::Elementwise::ReductionOp<Real>;
33
34 private:
35
36 Teuchos::RCP<SerialDenseVector> vec_;
37
38 public:
39
40 TeuchosVector( const Teuchos::RCP<SerialDenseVector> &vec ) :
41 vec_(vec) { }
42
43 TeuchosVector( Ordinal length, bool zeroOut=true ) :
44 vec_( Teuchos::rcp( new SerialDenseVector(length,zeroOut) ) ) {}
45
46 Real dot( const Vector<Real>& x ) const override {
47 return dynamic_cast<const TeuchosVector&>(x).getVector()->dot(*vec_);
48 }
49
50 void plus( const Vector<Real>& x ) override {
51 *vec_ += *(dynamic_cast<const TeuchosVector&>(x).getVector());
52 }
53
54 void scale( const Real alpha ) override {
55 vec_->scale(alpha);
56 }
57
58 int dimension() const override { return static_cast<int>(vec_->length()); }
59
60 void applyUnary( const UnaryFunction &f ) override {
61 for( Ordinal i=0; i<vec_->length(); ++i ) {
62 (*vec_)(i) = f.apply(((*vec_)(i)));
63 }
64 }
65
66 void applyBinary( const BinaryFunction &f, const Vector<Real> &x ) override {
67 const TeuchosVector &ex = dynamic_cast<const TeuchosVector&>(x);
68 for( Ordinal i=0; i<vec_->length(); ++i ) {
69 (*vec_)(i) = f.apply((*vec_)(i),ex(i));
70 }
71 }
72
73 Real reduce( const ReductionOp &r ) const override {
74 Real result = r.initialValue();
75 for( Ordinal i=0; i<vec_->length(); ++i ) {
76 r.reduce((*vec_)(i),result);
77 }
78 return result;
79 }
80
81 void setScalar(const Real C) override {
82 for( Ordinal i=0; i<vec_->length(); ++i ) {
83 (*vec_)(i) = C;
84 }
85 }
86
87 void randomize(const Real l=0.0, const Real u=1.0) override {
88 Real a = (u-l);
89 Real b = l;
90 Real x(0);
91 for( Ordinal i=0; i<vec_->length(); ++i ) {
92 x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
93 (*vec_)(i) = a*x + b;
94 }
95 }
96
97 Teuchos::RCP<Vector<Real>> clone() const override {
98 return Teuchos::rcp( new TeuchosVector(vec_->length()) );
99 }
100
101 Teuchos::RCP<Vector<Real>> basis( int i ) const override {
102 //auto b = Teuchos::rcp( new SerialDenseVector(vec_->length(),true) );
103 auto b = Teuchos::rcp( new TeuchosVector(vec_->length(),true) );
104 (*b)[static_cast<Ordinal>(i)] = Real{1.0};
105 return b;
106 }
107
108 Teuchos::RCP<SerialDenseVector> getVector() {
109 return vec_;
110 }
111
112 Teuchos::RCP<const SerialDenseVector> getVector() const {
113 return vec_;
114 }
115
116 void print( std::ostream &outStream ) const override {
117 vec_->print(outStream);
118 }
119
120 // Add element access operators to circumvent needing to create and get SerialDenseVectors
121 Real& operator() ( Ordinal i ) {
122 return (*vec_)(i);
123 }
124
125 const Real& operator() ( Ordinal i ) const {
126 return (*vec_)(i);
127 }
128
129 Real& operator[] ( Ordinal i ) {
130 return (*vec_)[i];
131 }
132
133 const Real& operator[] ( Ordinal i ) const {
134 return (*vec_)[i];
135 }
136
137 }; // class TeuchosVector
138
139
140 } // namespace ROL
141
142
143 #endif