Debian Codegraph @

packages/rol/adapters/teuchos/src/vector/ROL_MultiVectorDefault.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 /** @ingroup la_group
11 * \class ROL::MultiVectorDefault
12 * \brief Default implementation of the ROL::MultiVector container class
13 */
14
15
16 #ifndef ROL_MULTIVECTOR_DEFAULT_HPP
17 #define ROL_MULTIVECTOR_DEFAULT_HPP
18
19 #include "ROL_MultiVector.hpp"
20
21 namespace ROL {
22
23 template<class Real>
24 class MultiVectorDefault : public MultiVector<Real> {
25
26 typedef Vector<Real> V; // Single vector
27 typedef ROL::Ptr<V> PV; // Pointer to a vector
28 typedef Teuchos::ArrayRCP<PV> APV; // Array of pointers to vectors
29 typedef MultiVector<Real> MV; // Instance of this class
30 typedef ROL::Ptr<MV> PMV; // Pointer to an instance of this class
31
32 private:
33 APV mvec_; // Array of pointers to vectors
34 int numVectors_; // number of vectors (elements in the array)
35 int length_; // number of elements in a vector
36
37 virtual bool dimensionMismatch(const MV &A) const {
38
39 bool equalWidth = ( A.getNumberOfVectors() != numVectors_ );
40 bool equalLength = ( A.getLength() != length_ );
41 return ( equalWidth | equalLength );
42 }
43
44 public:
45
46 // Create a MultiVector from an array of pointers to vectors
47 MultiVectorDefault(APV mvec) : mvec_(mvec),
48 numVectors_(mvec.size()),
49 length_(mvec[0]->dimension()) {}
50
51 // Create a MultiVector from a pointer to a single vector
52 MultiVectorDefault(PV vec) : mvec_(APV(1,vec)),
53 numVectors_(1),
54 length_(vec->dimension()) {}
55
56 // Create a MultiVector from a pointer to a constant vector
57
58 ~MultiVectorDefault() {}
59
60 // Make a new MultiVector of the same dimensions
61 PMV clone() const {
62 APV x(numVectors_);
63 for(int i=0;i<numVectors_;++i) {
64 x[i] = mvec_[i]->clone();
65 }
66 return ROL::makePtr<MultiVectorDefault<Real>>(x);
67 }
68
69 // Make a new MultiVector of specified dimension
70 PMV clone( const int numvecs ) const {
71 APV x(numvecs);
72
73 for(int i=0;i<numvecs;++i) {
74 x[i] = mvec_[0]->clone();
75 }
76 return ROL::makePtr<MultiVectorDefault<Real>>(x);
77 }
78
79
80
81
82 // Make a deep copy of this MultiVector
83 PMV deepCopy() const {
84 APV x(numVectors_);
85 for(int i=0;i<numVectors_;++i) {
86 x[i] = mvec_[i]->clone();
87 x[i]->set(*mvec_[i]);
88 }
89 return ROL::makePtr<MultiVectorDefault<Real>>(x);
90 }
91
92 // Make a deep copy specified vectors in the MultiVector
93 PMV deepCopy(const std::vector<int> &index) const {
94 int n = index.size();
95 APV x(n);
96 for(int i=0;i<n;++i) {
97 int j = index[i];
98 x[i] = mvec_[j]->clone();
99 x[i]->set(*mvec_[j]);
100 }
101 return ROL::makePtr<MultiVectorDefault<Real>>(x);
102 }
103
104 // Make a shallow copy specified vectors in the MultiVector
105 PMV shallowCopy(const std::vector<int> &index) {
106 int n = index.size();
107 APV x(n);
108 for(int i=0;i<n;++i) {
109 int j = index[i];
110 x[i] = mvec_[j];
111 }
112 return ROL::makePtr<MultiVectorDefault<Real>>(x);
113 }
114
115 // Make a const shallow copy specified vectors in the MultiVector
116 const PMV shallowCopyConst(const std::vector<int> &index) const {
117 int n = index.size();
118 APV x(n);
119 for(int i=0;i<n;++i) {
120 int j = index[i];
121 x[i] = mvec_[j];
122 }
123 return ROL::makePtr<MultiVectorDefault<Real>>(x);
124 }
125
126 // Get the number of elements of a vector in the MultiVector
127 ptrdiff_t getLength() const {
128 return length_;
129 }
130
131 // Get the number of vectors in the MultiVector
132 int getNumberOfVectors() const {
133 return numVectors_;
134 }
135
136 void axpy(const Real alpha, const MV& x) {
137 for(int i=0;i<numVectors_;++i) {
138 mvec_[i]->axpy(alpha,*(x.getVector(i)));
139 }
140 }
141
142 // Generic BLAS level 3 matrix multiplication
143 // \f$\text{this}\leftarrow \alpha A B+\beta\text{this}\f$
144 void gemm(const Real alpha,
145 const MV& A,
146 const Teuchos::SerialDenseMatrix<int,Real> &B,
147 const Real beta) {
148
149 // Scale this by beta
150 this->scale(beta);
151
152 for(int i=0;i<B.numRows();++i) {
153 for(int j=0;j<B.numCols();++j) {
154 mvec_[j]->axpy(alpha*B(i,j),*A.getVector(i));
155 }
156 }
157 }
158
159 // Scale the MultiVector by a single scalar alpha
160 // \f$\text{this}\leftarrow\alpha\text{this}\f$
161 void scale(const Real alpha) {
162 for(int i=0;i<numVectors_;++i) {
163 mvec_[i]->scale(alpha);
164 }
165 }
166
167 // Scale each vector in the MultiVector by a different alpha
168 // \f$\text{this}[i]\leftarrow\alpha[i]\text{this}[i]\f$
169 void scale(const std::vector<Real> &alpha) {
170
171 ROL_TEST_FOR_EXCEPTION( static_cast<int>(alpha.size()) != numVectors_,
172 std::invalid_argument,
173 "Error: alpha must have the same length as the number of vectors.");
174
175 for(int i=0;i<numVectors_;++i) {
176 mvec_[i]->scale(alpha[i]);
177 }
178 }
179
180 // Set the MultiVector equal to another MultiVector
181 void set(const MV &A) {
182
183 // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A),
184 // std::invalid_argument,
185 // "Error: MultiVectors must have the same dimensions.");
186
187 for(int i=0;i<numVectors_;++i) {
188 mvec_[i]->set(*(A.getVector(i)));
189 }
190 }
191
192
193 // Set some of the vectors in this MultiVector equal to corresponding
194 // vectors in another MultiVector
195 void set(const MV &A, const std::vector<int> &index) {
196
197 // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A),
198 // std::invalid_argument,
199 // "Error: MultiVectors must have the same dimensions.");
200
201 int n = index.size();
202
203 for(int i=0;i<n;++i) {
204 int k = index[i];
205 if(k<numVectors_ && i<A.getNumberOfVectors()) {
206 mvec_[k]->set(*A.getVector(i));
207 }
208
209 }
210 }
211
212 // Compute \f$\alpha A^\top \text{this}\f$
213 void innerProducts(const Real alpha,
214 const MV &A,
215 Teuchos::SerialDenseMatrix<int,Real> &B) const {
216
217 // ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A),
218 // std::invalid_argument,
219 // "Error: MultiVectors must have the same dimensions.");
220
221 for(int i=0;i<A.getNumberOfVectors();++i) {
222 for(int j=0;j<numVectors_;++j) {
223 B(i,j) = alpha*mvec_[j]->dot(*A.getVector(i));
224 }
225 }
226 }
227
228 // Compute dot products of pairs of vectors
229 void dots(const MV &A,
230 std::vector<Real> &b) const {
231
232 ROL_TEST_FOR_EXCEPTION( this->dimensionMismatch(A),
233 std::invalid_argument,
234 "Error: MultiVectors must have the same dimensions.");
235
236 for(int i=0;i<numVectors_;++i) {
237 b[i] = mvec_[i]->dot(*A.getVector(i));
238 }
239 }
240
241 // Compute the norm of each vector in the MultiVector
242 void norms(std::vector<Real> &normvec) const {
243
244 int min = numVectors_ < static_cast<int>(normvec.size()) ? numVectors_ : normvec.size();
245
246 for(int i=0;i<min;++i) {
247 normvec[i] = mvec_[i]->norm();
248 }
249 }
250
251 // Zero each of the vectors in the MultiVector
252 void zero() {
253 for(int i=0;i<numVectors_;++i) {
254 mvec_[i]->zero();
255 }
256 }
257
258 // Return a pointer to the ith vector
259 PV getVector(int i) const {
260
261 ROL_TEST_FOR_EXCEPTION( i>=numVectors_,
262 std::invalid_argument,
263 "Error: index out of bounds");
264
265 return mvec_[i];
266 }
267
268 };
269 }
270
271
272 #endif