ROL
ROL_CArrayVector.hpp
Go to the documentation of this file.
1 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
2 // license for use of this work by or on behalf of the U.S. Government.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Corporation nor the names of the
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 // Questions? Contact lead developers:
32 // Drew Kouri (dpkouri@sandia.gov) and
33 // Denis Ridzal (dridzal@sandia.gov)
34 //
35 // ************************************************************************
36 // @HEADER
37 
38 #ifndef ROL_CARRAY_VECTOR_H
39 #define ROL_CARRAY_VECTOR_H
40 
41 #include "ROL_Vector.hpp"
42 #include "Teuchos_ArrayRCP.hpp"
43 
53 namespace ROL {
54 
55 template <class Real, class Element=Real>
56 class CArrayVector : public Vector<Real> {
57 
58  private:
59  unsigned int dim_;
60  Teuchos::ArrayRCP<Element> array_;
61  public:
62  // Create from C array with raw ptr
63  CArrayVector(Element* array, unsigned int dim) :
64  dim_(dim),array_(array,0,dim,false) {}
65 
66  // Create from Teuchos ArrayRCP
67  CArrayVector(const Teuchos::ArrayRCP<Element> array) :
68  dim_(array.size()),array_(array) {}
69 
70  // Create an array of all zeros
71  CArrayVector(unsigned int dim) :
72  dim_(dim),array_(dim) {}
73 
74  Teuchos::ArrayRCP<Element> getVector() const {
75  return array_;
76  }
77 
78  Teuchos::ArrayRCP<Element> getVector() {
79  return array_;
80  }
81 
82  void plus( const Vector<Real> &x ) {
83  // Need to make sure object has a getVector method
84  const CArrayVector &ex = Teuchos::dyn_cast<const CArrayVector>(x);
85 
86  Teuchos::ArrayRCP<Element> xp(ex.getVector());
87  for(unsigned int i=0; i<dim_; ++i) {
88  (array_)[i] += xp[i];
89  }
90  }
91 
92  Real dot( const Vector<Real> &x ) const {
93  const CArrayVector &ex = Teuchos::dyn_cast<const CArrayVector>(x);
94 
95  Teuchos::ArrayRCP<Element> xp(ex.getVector());
96  Real val = 0;
97  for(unsigned int i=0; i<dim_; ++i){
98  val += (array_)[i]*(xp)[i];
99  }
100  return val;
101  }
102 
103  Real norm() const {
104  Real val = 0;
105  val = std::sqrt( dot(*this) );
106  return val;
107  }
108 
109  void scale( const Real alpha ) {
110  for(unsigned int i=0; i<dim_; ++i) {
111  (array_)[i] *= alpha;
112  }
113  }
114 
115  int dimension() const {
116  return dim_;
117  }
118 
119  Teuchos::RCP<Vector<Real> > clone() const {
120  return Teuchos::rcp( new CArrayVector( Teuchos::ArrayRCP<Element>(dim_) ) );
121  }
122 
123  Teuchos::RCP<Vector<Real> > basis (const int i ) const {
124  Teuchos::RCP<CArrayVector> e =
125  Teuchos::rcp( new CArrayVector(dim_) );
126  (e->getVector())[i] = 1.0;
127  return e;
128  }
129 
130 };
131 
132 
133 }
134 
135 #endif
136 
Teuchos::ArrayRCP< Element > getVector() const
Real dot(const Vector< Real > &x) const
Compute where .
CArrayVector(unsigned int dim)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
CArrayVector(Element *array, unsigned int dim)
int dimension() const
Return dimension of the vector space.
Teuchos::ArrayRCP< Element > getVector()
Teuchos::ArrayRCP< Element > array_
void scale(const Real alpha)
Compute where .
Provides the C array implementation of the ROL::Vector interface for use with NumPy->C Array passing ...
CArrayVector(const Teuchos::ArrayRCP< Element > array)
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Real norm() const
Returns where .
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void plus(const Vector< Real > &x)
Compute , where .