Debian Codegraph @

packages/rol/adapters/teuchos/src/vector/ROL_CArrayVector.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_CARRAY_VECTOR_H
11 #define ROL_CARRAY_VECTOR_H
12
13 #include "ROL_Vector.hpp"
14 #include "Teuchos_ArrayRCP.hpp"
15
16 /** \class ROL::CArrayVector
17 \brief Provides the C array implementation of the ROL::Vector interface
18 for use with NumPy->C Array passing by pointer. This class in intended
19 to be used with the Python ROL interface.
20
21 \details See https://github.com/cython/cython/wiki/tutorials-NumpyPointerToC
22 \author Greg von Winckel ([email protected])
23 */
24
25 namespace ROL {
26
27 template <class Real, class Element=Real>
28 class CArrayVector : public Vector<Real> {
29
30 private:
31 unsigned int dim_;
32 Teuchos::ArrayRCP<Element> array_;
33 public:
34 // Create from C array with raw ptr
35 CArrayVector(Element* array, unsigned int dim) :
36 dim_(dim),array_(array,0,dim,false) {}
37
38 // Create from Teuchos ArrayRCP
39 CArrayVector(const Teuchos::ArrayRCP<Element> array) :
40 dim_(array.size()),array_(array) {}
41
42 // Create an array of all zeros
43 CArrayVector(unsigned int dim) :
44 dim_(dim),array_(dim) {}
45
46 Teuchos::ArrayRCP<Element> getVector() const {
47 return array_;
48 }
49
50 Teuchos::ArrayRCP<Element> getVector() {
51 return array_;
52 }
53
54 void plus( const Vector<Real> &x ) {
55 // Need to make sure object has a getVector method
56 const CArrayVector &ex = dynamic_cast<const CArrayVector&>(x);
57
58 Teuchos::ArrayRCP<Element> xp(ex.getVector());
59 for(unsigned int i=0; i<dim_; ++i) {
60 (array_)[i] += xp[i];
61 }
62 }
63
64 Real dot( const Vector<Real> &x ) const {
65 const CArrayVector &ex = dynamic_cast<const CArrayVector&>(x);
66
67 Teuchos::ArrayRCP<Element> xp(ex.getVector());
68 Real val = 0;
69 for(unsigned int i=0; i<dim_; ++i){
70 val += (array_)[i]*(xp)[i];
71 }
72 return val;
73 }
74
75 Real norm() const {
76 Real val = 0;
77 val = std::sqrt( dot(*this) );
78 return val;
79 }
80
81 void scale( const Real alpha ) {
82 for(unsigned int i=0; i<dim_; ++i) {
83 (array_)[i] *= alpha;
84 }
85 }
86
87 int dimension() const {
88 return dim_;
89 }
90
91 ROL::Ptr<Vector<Real> > clone() const {
92 return ROL::makePtr<CArrayVector>( Teuchos::ArrayRCP<Element>(dim_) );
93 }
94
95 ROL::Ptr<Vector<Real> > basis (const int i ) const {
96 ROL::Ptr<CArrayVector> e =
97 ROL::makePtr<CArrayVector>(dim_);
98 (e->getVector())[i] = 1.0;
99 return e;
100 }
101
102 void setScalar( const Real C ) {
103 for(unsigned int i=0; i<dim_; ++i) {
104 (array_)[i] = C;
105 }
106 }
107
108 void randomize( const Real l=0.0, const Real u=1.0 ) {
109 Real a = (u-l);
110 Real b = l;
111 Real x(0);
112 for(unsigned int i=0; i<dim_; ++i) {
113 x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
114 (array_)[i] = a*x + b;
115 }
116 }
117
118 };
119
120
121 }
122
123 #endif
124