1 #ifndef SELDON_FILE_MY_MULTIVEC_CPP
2 #define SELDON_FILE_MY_MULTIVEC_CPP
7 template<
class ScalarType>
8 inline int MyMultiVec<ScalarType>::GetVecLength ()
const
14 template<
class ScalarType>
15 inline int MyMultiVec<ScalarType>::GetNumberVecs ()
const
20 template<
class ScalarType>
21 inline ptrdiff_t MyMultiVec<ScalarType>::GetGlobalLength ()
const
26 template<
class ScalarType>
27 inline ScalarType& MyMultiVec<ScalarType>
28 ::operator()(
const int i,
const int j)
30 if (j < 0 || j >= NumberVecs_)
throw(-1);
31 if (i < 0 || i >= Length_)
throw(-2);
37 template<
class ScalarType>
38 inline const ScalarType& MyMultiVec<ScalarType>
39 ::operator()(
const int i,
const int j)
const
41 if (j < 0 || j >= NumberVecs_)
throw(-1);
42 if (i < 0 || i >= Length_)
throw(-2);
48 template<
class ScalarType>
49 inline ScalarType* MyMultiVec<ScalarType>::operator[](
int v)
51 if (v < 0 || v >= NumberVecs_)
throw(-1);
56 template<
class ScalarType>
57 inline ScalarType* MyMultiVec<ScalarType>::operator[](
int v)
const
64 template<
class ScalarType>
67 NumberVecs_(NumberVecs)
71 data_.resize(NumberVecs);
72 ownership_.resize(NumberVecs);
75 for (
int v = 0 ; v < NumberVecs_ ; ++v)
77 data_[v] =
new ScalarType[Length];
87 template<
class ScalarType>
89 ::MyMultiVec(
const int Length,
const std::vector<ScalarType*>& rhs) :
91 NumberVecs_(rhs.size())
95 data_.resize(NumberVecs_);
96 ownership_.resize(NumberVecs_);
100 for (
int v = 0 ; v < NumberVecs_ ; ++v)
103 ownership_[v] =
false;
109 template<
class ScalarType>
111 Length_(rhs.GetVecLength()),
112 NumberVecs_(rhs.NumberVecs_)
116 data_.resize(NumberVecs_);
117 ownership_.resize(NumberVecs_);
119 for (
int v = 0 ; v < NumberVecs_ ; ++v)
121 data_[v] =
new ScalarType[Length_];
122 ownership_[v] =
true;
125 for (
int v = 0 ; v < NumberVecs_ ; ++v)
127 for (
int i = 0 ; i < Length_ ; ++i)
128 (*
this)(i, v) = rhs(i, v);
134 template<
class ScalarType>
137 for (
int v = 0 ; v < NumberVecs_ ; ++v)
144 template<
class ScalarType>
159 template<
class ScalarType>
167 template<
class ScalarType>
170 int size = index.size();
173 for (
unsigned int v = 0 ; v < index.size() ; ++v)
174 for (
int i = 0 ; i < Length_ ; ++i)
175 (*tmp)(i, v) = (*
this)(i, index[v]);
182 template<
class ScalarType>
186 int size = index.size();
187 std::vector<ScalarType*> values(size);
189 for (
unsigned int v = 0 ; v < index.size() ; ++v)
190 values[v] = data_[index[v]];
197 template<
class ScalarType>
201 int size = index.size();
202 std::vector<ScalarType*> values(size);
204 for (
unsigned int v = 0 ; v < index.size() ; ++v)
205 values[v] = data_[index[v]];
212 template<
class ScalarType>
215 const Teuchos::SerialDenseMatrix<int, ScalarType> &B,
219 assert (Length_ == A.GetVecLength());
220 assert (B.numRows() == A.GetNumberVecs());
221 assert (B.numCols() <= NumberVecs_);
224 MyA =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(A));
227 if ((*
this)[0] == (*MyA)[0]) {
232 std::vector<ScalarType> tmp(NumberVecs_);
234 for (
int i = 0 ; i < Length_ ; ++i) {
235 for (
int v = 0; v < A.GetNumberVecs() ; ++v) {
236 tmp[v] = (*MyA)(i, v);
239 for (
int v = 0 ; v < B.numCols() ; ++v) {
240 (*this)(i, v) *= beta;
241 ScalarType res = Teuchos::ScalarTraits<ScalarType>::zero();
243 for (
int j = 0 ; j < A.GetNumberVecs() ; ++j) {
244 res += tmp[j] * B(j, v);
247 (*this)(i, v) += alpha * res;
252 for (
int i = 0 ; i < Length_ ; ++i) {
253 for (
int v = 0 ; v < B.numCols() ; ++v) {
254 (*this)(i, v) *= beta;
255 ScalarType res = 0.0;
256 for (
int j = 0 ; j < A.GetNumberVecs() ; ++j) {
257 res += (*MyA)(i, j) * B(j, v);
260 (*this)(i, v) += alpha * res;
268 template<
class ScalarType>
269 void MyMultiVec<ScalarType>
270 ::MvAddMv(ScalarType alpha,
const Anasazi::MultiVec<ScalarType>& A,
271 ScalarType beta,
const Anasazi::MultiVec<ScalarType>& B)
274 MyA =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(A));
278 MyB =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(B));
281 assert (NumberVecs_ == A.GetNumberVecs());
282 assert (NumberVecs_ == B.GetNumberVecs());
284 assert (Length_ == A.GetVecLength());
285 assert (Length_ == B.GetVecLength());
287 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
288 for (
int i = 0 ; i < Length_ ; ++i) {
289 (*this)(i, v) = alpha * (*MyA)(i, v) + beta * (*MyB)(i, v);
296 template<
class ScalarType>
297 void MyMultiVec<ScalarType>
298 ::MvTransMv(ScalarType alpha,
const Anasazi::MultiVec<ScalarType>& A,
299 Teuchos::SerialDenseMatrix< int, ScalarType >& B
300 #ifdef HAVE_ANASAZI_EXPERIMENTAL
301 , Anasazi::ConjType conj
306 MyA =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(A));
309 assert (A.GetVecLength() == Length_);
310 assert (NumberVecs_ <= B.numCols());
311 assert (A.GetNumberVecs() <= B.numRows());
313 #ifdef HAVE_ANASAZI_EXPERIMENTAL
314 if (conj == Anasazi::CONJ) {
316 for (
int v = 0 ; v < A.GetNumberVecs() ; ++v) {
317 for (
int w = 0 ; w < NumberVecs_ ; ++w) {
318 ScalarType value = 0.0;
319 for (
int i = 0 ; i < Length_ ; ++i) {
320 value += Teuchos::ScalarTraits<ScalarType>::conjugate((*MyA)(i, v)) * (*this)(i, w);
322 B(v, w) = alpha * value;
325 #ifdef HAVE_ANASAZI_EXPERIMENTAL
327 for (
int v = 0 ; v < A.GetNumberVecs() ; ++v) {
328 for (
int w = 0 ; w < NumberVecs_ ; ++w) {
329 ScalarType value = 0.0;
330 for (
int i = 0 ; i < Length_ ; ++i) {
331 value += (*MyA)(i, v) * (*
this)(i, w);
333 B(v, w) = alpha * value;
342 template<
class ScalarType>
343 void MyMultiVec<ScalarType>
344 ::MvDot(
const Anasazi::MultiVec<ScalarType>& A, std::vector<ScalarType> &b
345 #ifdef HAVE_ANASAZI_EXPERIMENTAL
346 , Anasazi::ConjType conj
351 MyA =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(A));
354 assert (NumberVecs_ <= (
int)b.size());
355 assert (NumberVecs_ == A.GetNumberVecs());
356 assert (Length_ == A.GetVecLength());
358 #ifdef HAVE_ANASAZI_EXPERIMENTAL
359 if (conj == Anasazi::CONJ) {
361 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
362 ScalarType value = 0.0;
363 for (
int i = 0 ; i < Length_ ; ++i) {
364 value += (*this)(i, v) * Teuchos::ScalarTraits<ScalarType>::conjugate((*MyA)(i, v));
368 #ifdef HAVE_ANASAZI_EXPERIMENTAL
370 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
371 ScalarType value = 0.0;
372 for (
int i = 0 ; i < Length_ ; ++i) {
373 value += (*this)(i, v) * (*MyA)(i, v);
382 template<
class ScalarType>
383 void MyMultiVec<ScalarType>
384 ::MvNorm( std::vector<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &normvec)
const
386 assert (NumberVecs_ <= (
int)normvec.size());
388 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
390 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
391 MagnitudeType value = Teuchos::ScalarTraits<MagnitudeType>::zero();
392 for (
int i = 0 ; i < Length_ ; ++i) {
393 MagnitudeType val = Teuchos::ScalarTraits<ScalarType>::magnitude((*
this)(i, v));
396 normvec[v] = Teuchos::ScalarTraits<MagnitudeType>::squareroot(value);
405 template<
class ScalarType>
406 void MyMultiVec<ScalarType>::SetBlock(
const Anasazi::MultiVec<ScalarType>& A,
407 const std::vector<int> &index)
410 MyA =
dynamic_cast<MyMultiVec*
>(&
const_cast<Anasazi::MultiVec<ScalarType> &
>(A));
413 assert (A.GetNumberVecs() >= (
int)index.size());
414 assert (A.GetVecLength() == Length_);
416 for (
unsigned int v = 0 ; v < index.size() ; ++v) {
417 for (
int i = 0 ; i < Length_ ; ++i) {
418 (*this)(i, index[v]) = (*MyA)(i, v);
425 template<
class ScalarType>
426 void MyMultiVec<ScalarType>::MvScale( ScalarType alpha )
428 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
429 for (
int i = 0 ; i < Length_ ; ++i) {
430 (*this)(i, v) *= alpha;
437 template<
class ScalarType>
438 void MyMultiVec<ScalarType>::MvScale(
const std::vector<ScalarType>& alpha )
440 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
441 for (
int i = 0 ; i < Length_ ; ++i) {
442 (*this)(i, v) *= alpha[v];
449 template<
class ScalarType>
450 void MyMultiVec<ScalarType>::MvRandom ()
452 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
453 for (
int i = 0 ; i < Length_ ; ++i) {
454 (*this)(i, v) = Teuchos::ScalarTraits<ScalarType>::random();
461 template<
class ScalarType>
462 void MyMultiVec<ScalarType>::MvInit(ScalarType alpha)
464 for (
int v = 0 ; v < NumberVecs_ ; ++v) {
465 for (
int i = 0 ; i < Length_ ; ++i) {
466 (*this)(i, v) = alpha;
472 template<
class ScalarType>
473 void MyMultiVec<ScalarType>::MvPrint(std::ostream &os)
const
475 os <<
"Object MyMultiVec" << std::endl;
476 os <<
"Number of rows = " << Length_ << std::endl;
477 os <<
"Number of vecs = " << NumberVecs_ << std::endl;
479 for (
int i = 0 ; i < Length_ ; ++i)
481 for (
int v = 0 ; v < NumberVecs_ ; ++v)
482 os << (*
this)(i, v) <<
" ";
488 template<
class ScalarType>
489 void MyMultiVec<ScalarType>::Check()
492 throw(
"Length must be positive");
494 if (NumberVecs_ <= 0)
495 throw(
"Number of vectors must be positive");
499 template <
typename ScalarType>
500 std::ostream&
operator<<(std::ostream& os,
const MyMultiVec<ScalarType>& Obj)
508 #endif // MY_MULTIVECTOR_CPP