Anasazi  Version of the Day
AnasaziMVOPTester.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 //
29 #ifndef ANASAZI_MVOPTESTER_HPP
30 #define ANASAZI_MVOPTESTER_HPP
31 
32 // Assumptions that I have made:
33 // * I assume/verify that a multivector must have at least one vector. This seems
34 // to be consistent with Epetra_MultiVec.
35 // * I do not assume that an operator is deterministic; I do assume that the
36 // operator, applied to 0, will return 0.
37 
46 #include "AnasaziConfigDefs.hpp"
47 #include "AnasaziTypes.hpp"
48 
51 #include "AnasaziOutputManager.hpp"
52 
53 #include "Teuchos_RCP.hpp"
54 #include "Teuchos_as.hpp"
55 
56 namespace Anasazi {
57 
63  template< class ScalarType, class MV >
66  const Teuchos::RCP<const MV> &A ) {
67 
68  using std::endl;
69 
70  /* MVT Contract:
71 
72  Clone(MV,int)
73  CloneCopy(MV)
74  CloneCopy(MV,vector<int>)
75  USER: will request positive number of vectors
76  MV: will return a multivector with exactly the number of
77  requested vectors.
78  vectors are the same dimension as the cloned MV
79 
80 
81  CloneView(MV,vector<int>) [const and non-const]
82  USER: There is no assumed communication between creation and
83  destruction of a view. I.e., after a view is created, changes to the
84  source multivector are not reflected in the view. Likewise, until
85  destruction of the view, changes in the view are not reflected in the
86  source multivector.
87 
88  GetVecLength
89  MV: will always be positive (MV cannot have zero vectors)
90 
91  GetGlobalLength (MultiVecTraitsExt)
92  MV: will always be positive (MV cannot have zero vectors)
93 
94  GetNumberVecs
95  MV: will always be positive (MV cannot have zero vectors)
96 
97  MvAddMv
98  USER: multivecs will be of the same dimension and same number of vecs
99  MV: input vectors will not be modified
100  performing C=0*A+1*B will assign B to C exactly
101 
102  MvTimesMatAddMv
103  USER: multivecs and serialdensematrix will be of the proper shape
104  MV: input arguments will not be modified
105  following arithmetic relations hold exactly:
106  A*I = A
107  0*B = B
108  1*B = B
109 
110  MvTransMv
111  USER: SerialDenseMatrix will be large enough to hold results.
112  MV: SerialDenseMatrix will not be resized.
113  Inner products will satisfy |a'*b| <= |a|*|b|
114  alpha == 0 => SerialDenseMatrix == 0
115 
116  MvDot
117  USER: Results vector will be large enough for results.
118  Both multivectors will have the same number of vectors.
119  (Epetra crashes, otherwise.)
120  MV: Inner products will satisfy |a'*b| <= |a|*|b|
121  Results vector will not be resized.
122 
123  MvNorm
124  MV: vector norm is always non-negative, and zero
125  only for zero vectors.
126  results vector should not be resized
127 
128  SetBlock
129  USER: indices will be distinct
130  MV: assigns copies of the vectors to the specified
131  locations, leaving the other vectors untouched.
132 
133  MvRandom
134  MV: Generate zero vector with "zero" probability
135  Don't gen the same vectors twice.
136 
137  MvInit
138  MV: Init(alpha) sets all elements to alpha
139 
140  MvScale (two versions)
141  MV: scales multivector values
142 
143  MvPrint
144  MV: routine does not modify vectors (not tested here)
145  *********************************************************************/
146 
147  typedef MultiVecTraits<ScalarType, MV> MVT;
148  typedef MultiVecTraitsExt<ScalarType, MV> MVText;
150  typedef typename SCT::magnitudeType MagType;
151 
152  const ScalarType one = SCT::one();
153  const ScalarType zero = SCT::zero();
154  const MagType zero_mag = Teuchos::ScalarTraits<MagType>::zero();
155 
156  // Don't change these two without checking the initialization of ind below
157  const int numvecs = 10;
158  const int numvecs_2 = 5;
159 
160  std::vector<int> ind(numvecs_2);
161 
162  /* Initialize indices for selected copies/views
163  The MVT specialization should not assume that
164  these are ordered or even distinct.
165  Also retrieve the edges.
166 
167  However, to spice things up, grab the first vector,
168  last vector, and choose the others randomly.
169  */
170  TEUCHOS_TEST_FOR_EXCEPT(numvecs_2 != 5);
171  ind[0] = 0;
172  ind[1] = 5;
173  ind[2] = 2;
174  ind[3] = 2;
175  ind[4] = 9;
176 
177  /*********** GetNumberVecs() *****************************************
178  Verify:
179  1) This number should be strictly positive
180  *********************************************************************/
181  if ( MVT::GetNumberVecs(*A) <= 0 ) {
182  om->stream(Warnings)
183  << "*** ERROR *** MultiVectorTraits::GetNumberVecs()." << endl
184  << "Returned <= 0." << endl;
185  return false;
186  }
187 
188 
189  /*********** GetVecLength() ******************************************
190  Verify:
191  1) This number should be strictly positive
192  *********************************************************************/
193  if ( MVT::GetVecLength(*A) <= 0 ) {
194  om->stream(Warnings)
195  << "*** ERROR *** MultiVectorTraits::GetVecLength()" << endl
196  << "Returned <= 0." << endl;
197  return false;
198  }
199 
200 
201  /*********** GetGlobalLength() ***************************************
202  Verify:
203  1) This number should be strictly positive
204  *********************************************************************/
205  if ( MVText::GetGlobalLength(*A) <= 0 ) {
206  om->stream(Warnings)
207  << "*** ERROR *** MultiVectorTraitsExt::GetGlobalLength()" << endl
208  << "Returned <= 0." << endl;
209  return false;
210  }
211 
212  /*********** Clone() and MvNorm() ************************************
213  Verify:
214  1) Clone() allows us to specify the number of vectors
215  2) Clone() returns a multivector of the same dimension
216  3) Vector norms shouldn't be negative
217  *********************************************************************/
218  {
219  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
220  std::vector<MagType> norms(numvecs);
221  if ( MVT::GetNumberVecs(*B) != numvecs ) {
222  om->stream(Warnings)
223  << "*** ERROR *** MultiVecTraits::Clone()." << endl
224  << "Did not allocate requested number of vectors." << endl;
225  return false;
226  }
227  if ( MVText::GetGlobalLength(*B) != MVText::GetGlobalLength(*A) ) {
228  om->stream(Warnings)
229  << "*** ERROR *** MultiVecTraits::Clone()." << endl
230  << "Did not allocate requested number of vectors." << endl;
231  return false;
232  }
233  MVT::MvNorm(*B, norms);
234  if ( (int)norms.size() != numvecs ) {
235  om->stream(Warnings)
236  << "*** ERROR *** MultiVecTraits::MvNorm()." << endl
237  << "Method resized the output vector." << endl;
238  }
239  for (int i=0; i<numvecs; i++) {
240  if ( norms[i] < zero_mag ) {
241  om->stream(Warnings)
242  << "*** ERROR *** MultiVecTraits::Clone()." << endl
243  << "Vector had negative norm." << endl;
244  return false;
245  }
246  }
247  }
248 
249 
250  /*********** MvRandom() and MvNorm() and MvInit() ********************
251  Verify:
252  1) Set vectors to zero
253  2) Check that norm is zero
254  3) Perform MvRandom.
255  4) Verify that vectors aren't zero anymore
256  5) Perform MvRandom again.
257  6) Verify that vector norms are different than before
258 
259  Without knowing something about the random distribution,
260  this is about the best that we can do, to make sure that MvRandom
261  did at least *something*.
262 
263  Also, make sure vector norms aren't negative.
264  *********************************************************************/
265  {
266  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
267  std::vector<MagType> norms(numvecs), norms2(numvecs);
268 
269  MVT::MvInit(*B);
270  MVT::MvNorm(*B, norms);
271  for (int i=0; i<numvecs; i++) {
272  if ( norms[i] != zero_mag ) {
273  om->stream(Warnings)
274  << "*** ERROR *** MultiVecTraits::MvInit() "
275  << "and MultiVecTraits::MvNorm()" << endl
276  << "Supposedly zero vector has non-zero norm." << endl;
277  return false;
278  }
279  }
280  MVT::MvRandom(*B);
281  MVT::MvNorm(*B, norms);
282  MVT::MvRandom(*B);
283  MVT::MvNorm(*B, norms2);
284  for (int i=0; i<numvecs; i++) {
285  if ( norms[i] == zero_mag || norms2[i] == zero_mag ) {
286  om->stream(Warnings)
287  << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
288  << "Random vector was empty (very unlikely)." << endl;
289  return false;
290  }
291  else if ( norms[i] < zero_mag || norms2[i] < zero_mag ) {
292  om->stream(Warnings)
293  << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
294  << "Vector had negative norm." << endl;
295  return false;
296  }
297  else if ( norms[i] == norms2[i] ) {
298  om->stream(Warnings)
299  << "*** ERROR *** MutliVecTraits::MvRandom()." << endl
300  << "Vectors not random enough." << endl;
301  return false;
302  }
303  }
304  }
305 
306 
307  /*********** MvRandom() and MvNorm() and MvScale() *******************
308  Verify:
309  1) Perform MvRandom.
310  2) Verify that vectors aren't zero
311  3) Set vectors to zero via MvScale
312  4) Check that norm is zero
313  *********************************************************************/
314  {
315  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
316  std::vector<MagType> norms(numvecs);
317 
318  MVT::MvRandom(*B);
319  MVT::MvScale(*B,SCT::zero());
320  MVT::MvNorm(*B, norms);
321  for (int i=0; i<numvecs; i++) {
322  if ( norms[i] != zero_mag ) {
323  om->stream(Warnings)
324  << "*** ERROR *** MultiVecTraits::MvScale(alpha) "
325  << "Supposedly zero vector has non-zero norm." << endl;
326  return false;
327  }
328  }
329 
330  MVT::MvRandom(*B);
331  std::vector<ScalarType> zeros(numvecs,SCT::zero());
332  MVT::MvScale(*B,zeros);
333  MVT::MvNorm(*B, norms);
334  for (int i=0; i<numvecs; i++) {
335  if ( norms[i] != zero_mag ) {
336  om->stream(Warnings)
337  << "*** ERROR *** MultiVecTraits::MvScale(alphas) "
338  << "Supposedly zero vector has non-zero norm." << endl;
339  return false;
340  }
341  }
342  }
343 
344 
345  /*********** MvInit() and MvNorm() ***********************************
346  A vector of ones of dimension n should have norm sqrt(n)
347  1) Init vectors to all ones
348  2) Verify that norm is sqrt(n)
349  3) Verify that norms aren't negative
350 
351  Note: I'm not sure that we can expect this to hold in practice.
352  Maybe something like abs(norm-sqrt(n)) < SCT::eps() ???
353  The sum of 1^2==1 should be n, but what about sqrt(n)?
354  They may be using a different square root than ScalartTraits
355  On my iBook G4 and on jeter, this test works.
356  Right now, this has been demoted to a warning.
357  *********************************************************************/
358  {
359  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
360  std::vector<MagType> norms(numvecs);
361 
362  MVT::MvInit(*B,one);
363  MVT::MvNorm(*B, norms);
364  bool BadNormWarning = false;
365  for (int i=0; i<numvecs; i++) {
366  if ( norms[i] < zero_mag ) {
367  om->stream(Warnings)
368  << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
369  << "Vector had negative norm." << endl;
370  return false;
371  }
372  else if ( norms[i] != SCT::squareroot(MVT::GetVecLength(*B)) && !BadNormWarning ) {
373  om->stream(Warnings)
374  << endl
375  << "Warning testing MultiVecTraits::MvInit()." << endl
376  << "Ones vector should have norm sqrt(dim)." << endl
377  << "norms[i]: " << norms[i] << "\tdim: " << MVText::GetGlobalLength(*B) << endl << endl;
378  BadNormWarning = true;
379  }
380  }
381  }
382 
383 
384  /*********** MvInit() and MvNorm() ***********************************
385  A vector of zeros of dimension n should have norm 0
386  1) Verify that norms aren't negative
387  2) Verify that norms are zero
388 
389  We must know this works before the next tests.
390  *********************************************************************/
391  {
392  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
393  std::vector<MagType> norms(numvecs);
394  MVT::MvInit(*B, zero_mag);
395  MVT::MvNorm(*B, norms);
396  for (int i=0; i<numvecs; i++) {
397  if ( norms[i] < zero_mag ) {
398  om->stream(Warnings)
399  << "*** ERROR *** MultiVecTraits::MvInit()." << endl
400  << "Vector had negative norm." << endl;
401  return false;
402  }
403  else if ( norms[i] != zero_mag ) {
404  om->stream(Warnings)
405  << "*** ERROR *** MultiVecTraits::MvInit()." << endl
406  << "Zero vector should have norm zero." << endl;
407  return false;
408  }
409  }
410  }
411 
412 
413  /*********** CloneCopy(MV,vector<int>) and MvNorm ********************
414  1) Check quantity/length of vectors
415  2) Check vector norms for agreement
416  3) Zero out B and make sure that C norms are not affected
417  *********************************************************************/
418  {
419  Teuchos::RCP<MV> B, C;
420  std::vector<MagType> norms(numvecs), norms2(ind.size());
421 
422  B = MVT::Clone(*A,numvecs);
423  MVT::MvRandom(*B);
424  MVT::MvNorm(*B, norms);
425  C = MVT::CloneCopy(*B,ind);
426  MVT::MvNorm(*C, norms2);
427  if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
428  om->stream(Warnings)
429  << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
430  << "Wrong number of vectors." << endl;
431  return false;
432  }
433  if ( MVText::GetGlobalLength(*C) != MVText::GetGlobalLength(*B) ) {
434  om->stream(Warnings)
435  << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
436  << "Vector lengths don't match." << endl;
437  return false;
438  }
439  for (int i=0; i<numvecs_2; i++) {
440  if ( norms2[i] != norms[ind[i]] ) {
441  om->stream(Warnings)
442  << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
443  << "Copied vectors do not agree:"
444  << norms2[i] << " != " << norms[ind[i]] << endl;
445  return false;
446  }
447  }
448  MVT::MvInit(*B,zero);
449  MVT::MvNorm(*C, norms2);
450  for (int i=0; i<numvecs_2; i++) {
451  if ( norms2[i] != norms2[i] ) {
452  om->stream(Warnings)
453  << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
454  << "Copied vectors were not independent." << endl;
455  return false;
456  }
457  }
458  }
459 
460 
461  /*********** CloneCopy(MV) and MvNorm ********************************
462  1) Check quantity
463  2) Check value of norms
464  3) Zero out B and make sure that C is still okay
465  *********************************************************************/
466  {
467  Teuchos::RCP<MV> B, C;
468  std::vector<MagType> norms(numvecs), norms2(numvecs);
469 
470  B = MVT::Clone(*A,numvecs);
471  MVT::MvRandom(*B);
472  MVT::MvNorm(*B, norms);
473  C = MVT::CloneCopy(*B);
474  MVT::MvNorm(*C, norms2);
475  if ( MVT::GetNumberVecs(*C) != numvecs ) {
476  om->stream(Warnings)
477  << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
478  << "Wrong number of vectors." << endl;
479  return false;
480  }
481  for (int i=0; i<numvecs; i++) {
482  if ( norms2[i] != norms[i] ) {
483  om->stream(Warnings)
484  << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
485  << "Copied vectors do not agree." << endl;
486  return false;
487  }
488  }
489  MVT::MvInit(*B,zero);
490  MVT::MvNorm(*C, norms);
491  for (int i=0; i<numvecs; i++) {
492  if ( norms2[i] != norms[i] ) {
493  om->stream(Warnings)
494  << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
495  << "Copied vectors were not independent." << endl;
496  return false;
497  }
498  }
499  }
500 
501 
502  /*********** CloneView(MV,vector<int>) and MvNorm ********************
503  Check that we have a view of the selected vectors
504  1) Check quantity
505  2) Check value of norms
506  3) Zero out B and make sure that C is zero as well
507  *********************************************************************/
508  {
509  Teuchos::RCP<MV> B, C;
510  std::vector<MagType> norms(numvecs), norms2(ind.size());
511 
512  B = MVT::Clone(*A,numvecs);
513  MVT::MvRandom(*B);
514  MVT::MvNorm(*B, norms);
515  C = MVT::CloneViewNonConst(*B,ind);
516  MVT::MvNorm(*C, norms2);
517  if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
518  om->stream(Warnings)
519  << "*** ERROR *** MultiVecTraits::CloneView(ind)." << endl
520  << "Wrong number of vectors." << endl;
521  return false;
522  }
523  for (int i=0; i<numvecs_2; i++) {
524  if ( norms2[i] != norms[ind[i]] ) {
525  om->stream(Warnings)
526  << "*** ERROR *** MultiVecTraits::CloneView(ind)." << endl
527  << "Viewed vectors do not agree." << endl;
528  return false;
529  }
530  }
531  }
532 
533 
534  /*********** const CloneView(MV,vector<int>) and MvNorm() ************
535  Check that we have a view of the selected vectors.
536  1) Check quantity
537  2) Check value of norms for agreement
538  3) Zero out B and make sure that C is zerod as well
539  *********************************************************************/
540  {
542  Teuchos::RCP<const MV> constB, C;
543  std::vector<MagType> normsB(numvecs), normsC(ind.size());
544  std::vector<int> allind(numvecs);
545  for (int i=0; i<numvecs; i++) {
546  allind[i] = i;
547  }
548 
549  B = MVT::Clone(*A,numvecs);
550  MVT::MvRandom( *B );
551  MVT::MvNorm(*B, normsB);
552  C = MVT::CloneView(*B,ind);
553  MVT::MvNorm(*C, normsC);
554  if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
555  om->stream(Warnings)
556  << "*** ERROR *** const MultiVecTraits::CloneView(ind)." << endl
557  << "Wrong number of vectors." << endl;
558  return false;
559  }
560  for (int i=0; i<numvecs_2; i++) {
561  if ( normsC[i] != normsB[ind[i]] ) {
562  om->stream(Warnings)
563  << "*** ERROR *** const MultiVecTraits::CloneView(ind)." << endl
564  << "Viewed vectors do not agree." << endl;
565  return false;
566  }
567  }
568  }
569 
570 
571  /*********** SetBlock() and MvNorm() *********************************
572  SetBlock() will copy the vectors from C into B
573  1) Verify that the specified vectors were copied
574  2) Verify that the other vectors were not modified
575  3) Verify that C was not modified
576  4) Change C and then check B to make sure it was not modified
577 
578  Use a different index set than has been used so far (distinct entries).
579  This is because duplicate entries will cause the vector to be
580  overwritten, making it more difficult to test.
581  *********************************************************************/
582  {
583  Teuchos::RCP<MV> B, C;
584  std::vector<MagType> normsB1(numvecs), normsB2(numvecs),
585  normsC1(numvecs_2), normsC2(numvecs_2);
586 
587  B = MVT::Clone(*A,numvecs);
588  C = MVT::Clone(*A,numvecs_2);
589  // Just do every other one, interleaving the vectors of C into B
590  ind.resize(numvecs_2);
591  for (int i=0; i<numvecs_2; i++) {
592  ind[i] = 2*i;
593  }
594  MVT::MvRandom(*B);
595  MVT::MvRandom(*C);
596 
597  MVT::MvNorm(*B,normsB1);
598  MVT::MvNorm(*C,normsC1);
599  MVT::SetBlock(*C,ind,*B);
600  MVT::MvNorm(*B,normsB2);
601  MVT::MvNorm(*C,normsC2);
602 
603  // check that C was not changed by SetBlock
604  for (int i=0; i<numvecs_2; i++) {
605  if ( normsC1[i] != normsC2[i] ) {
606  om->stream(Warnings)
607  << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
608  << "Operation modified source vectors." << endl;
609  return false;
610  }
611  }
612  // check that the correct vectors of B were modified
613  // and the others were not
614  for (int i=0; i<numvecs; i++) {
615  if (i % 2 == 0) {
616  // should be a vector from C
617  if ( normsB2[i] != normsC1[i/2] ) {
618  om->stream(Warnings)
619  << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
620  << "Copied vectors do not agree." << endl;
621  return false;
622  }
623  }
624  else {
625  // should be an original vector
626  if ( normsB1[i] != normsB2[i] ) {
627  om->stream(Warnings)
628  << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
629  << "Incorrect vectors were modified." << endl;
630  return false;
631  }
632  }
633  }
634  MVT::MvInit(*C,zero);
635  MVT::MvNorm(*B,normsB1);
636  // verify that we copied and didn't reference
637  for (int i=0; i<numvecs; i++) {
638  if ( normsB1[i] != normsB2[i] ) {
639  om->stream(Warnings)
640  << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
641  << "Copied vectors were not independent." << endl;
642  return false;
643  }
644  }
645  }
646 
647 
648  /*********** MvTransMv() *********************************************
649  Performs C = alpha * A^H * B, where
650  alpha is type ScalarType
651  A,B are type MV with p and q vectors, respectively
652  C is a SerialDenseMatrix<int,ScalarType> ALREADY sized to p by q
653 
654  Verify:
655  1) C is not resized by the routine
656  3) Check that zero*(A^H B) == zero
657  3) Check inner product inequality:
658  [ |a1|*|b1| ... |ap|*|b1| ]
659  [a1 ... ap]^H [b1 ... bq] <= [ ... |ai|*|bj| ... ]
660  [ |ap|*|b1| ... |ap|*|bq| ]
661  4) Zero B and check that B^H * C is zero
662  5) Zero C and check that B^H * C is zero
663 
664  Note 1: Test 4 is performed with a p x q Teuchos::SDM view of
665  a (p+1) x (q+1) Teuchos::SDM that is initialized to ones.
666  This ensures the user is correctly accessing and filling the SDM.
667 
668  Note 2: Should we really require that C is correctly sized already?
669  Epetra does (and crashes if it isn't.)
670  *********************************************************************/
671  {
672  const int p = 7;
673  const int q = 9;
674  Teuchos::RCP<MV> B, C;
675  std::vector<MagType> normsB(p), normsC(q);
677 
678  B = MVT::Clone(*A,p);
679  C = MVT::Clone(*A,q);
680 
681  // randomize the multivectors
682  MVT::MvRandom(*B);
683  MVT::MvNorm(*B,normsB);
684  MVT::MvRandom(*C);
685  MVT::MvNorm(*C,normsC);
686 
687  // perform SDM = zero() * B^H * C
688  MVT::MvTransMv( zero, *B, *C, SDM );
689 
690  // check the sizes: not allowed to have shrunk
691  if ( SDM.numRows() != p || SDM.numCols() != q ) {
692  om->stream(Warnings)
693  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
694  << "Routine resized SerialDenseMatrix." << endl;
695  return false;
696  }
697 
698  // check that zero**A^H*B == zero
699  if ( SDM.normOne() != zero ) {
700  om->stream(Warnings)
701  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
702  << "Scalar argument processed incorrectly." << endl;
703  return false;
704  }
705 
706  // perform SDM = one * B^H * C
707  MVT::MvTransMv( one, *B, *C, SDM );
708 
709  // check the norms: a^H b = |a| |b| cos(theta) <= |a| |b|
710  // with equality only when a and b are colinear
711  for (int i=0; i<p; i++) {
712  for (int j=0; j<q; j++) {
713  if ( SCT::magnitude(SDM(i,j))
714  > SCT::magnitude(normsB[i]*normsC[j]) ) {
715  om->stream(Warnings)
716  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
717  << "Triangle inequality did not hold: "
718  << SCT::magnitude(SDM(i,j))
719  << " > "
720  << SCT::magnitude(normsB[i]*normsC[j])
721  << endl;
722  return false;
723  }
724  }
725  }
726  MVT::MvInit(*C);
727  MVT::MvRandom(*B);
728  MVT::MvTransMv( one, *B, *C, SDM );
729  for (int i=0; i<p; i++) {
730  for (int j=0; j<q; j++) {
731  if ( SDM(i,j) != zero ) {
732  om->stream(Warnings)
733  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
734  << "Inner products not zero for C==0." << endl;
735  return false;
736  }
737  }
738  }
739  MVT::MvInit(*B);
740  MVT::MvRandom(*C);
741  MVT::MvTransMv( one, *B, *C, SDM );
742  for (int i=0; i<p; i++) {
743  for (int j=0; j<q; j++) {
744  if ( SDM(i,j) != zero ) {
745  om->stream(Warnings)
746  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
747  << "Inner products not zero for B==0." << endl;
748  return false;
749  }
750  }
751  }
752 
753  // A larger SDM is filled with ones, initially, and a smaller
754  // view is used for the MvTransMv method. If the smaller SDM
755  // is not all zeroes, then the interface is improperly writing
756  // to the matrix object.
757  // Note: Since we didn't fail above, we know that the general
758  // inner product works, but we are checking to see if it
759  // works for a view too. This is common usage in Anasazi.
762  largeSDM.putScalar( one );
763  MVT::MvInit(*C);
764  MVT::MvRandom(*B);
765  MVT::MvTransMv( one, *B, *C, SDM2 );
766  for (int i=0; i<p; i++) {
767  for (int j=0; j<q; j++) {
768  if ( SDM2(i,j) != zero ) {
769  om->stream(Warnings)
770  << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
771  << "Inner products not zero for C==0 when using a view into Teuchos::SerialDenseMatrix<>." << endl;
772  return false;
773  }
774  }
775  }
776  }
777 
778 
779  /*********** MvDot() *************************************************
780  Verify:
781  1) Results vector not resized
782  2) Inner product inequalities are satisfied
783  3) Zero vectors give zero inner products
784  *********************************************************************/
785  {
786  const int p = 7;
787  Teuchos::RCP<MV> B, C;
788  std::vector<ScalarType> iprods(p);
789  std::vector<MagType> normsB(p), normsC(p);
790 
791  B = MVT::Clone(*A,p);
792  C = MVT::Clone(*A,p);
793 
794  MVT::MvRandom(*B);
795  MVT::MvRandom(*C);
796  MVT::MvNorm(*B,normsB);
797  MVT::MvNorm(*C,normsC);
798  MVT::MvDot( *B, *C, iprods );
799  if ( (int)iprods.size() != p ) {
800  om->stream(Warnings)
801  << "*** ERROR *** MultiVecTraits::MvDot." << endl
802  << "Routine resized results vector." << endl;
803  return false;
804  }
805  for (int i=0; i<p; i++) {
806  if ( SCT::magnitude(iprods[i])
807  > SCT::magnitude(normsB[i]*normsC[i]) ) {
808  om->stream(Warnings)
809  << "*** ERROR *** MultiVecTraits::MvDot()." << endl
810  << "Inner products not valid." << endl;
811  return false;
812  }
813  }
814  MVT::MvInit(*B);
815  MVT::MvRandom(*C);
816  MVT::MvDot( *B, *C, iprods );
817  for (int i=0; i<p; i++) {
818  if ( iprods[i] != zero ) {
819  om->stream(Warnings)
820  << "*** ERROR *** MultiVecTraits::MvDot()." << endl
821  << "Inner products not zero for B==0." << endl;
822  return false;
823  }
824  }
825  MVT::MvInit(*C);
826  MVT::MvRandom(*B);
827  MVT::MvDot( *B, *C, iprods );
828  for (int i=0; i<p; i++) {
829  if ( iprods[i] != zero ) {
830  om->stream(Warnings)
831  << "*** ERROR *** MultiVecTraits::MvDot()." << endl
832  << "Inner products not zero for C==0." << endl;
833  return false;
834  }
835  }
836  }
837 
838 
839  /*********** MvAddMv() ***********************************************
840  D = alpha*B + beta*C
841  1) Use alpha==0,beta==1 and check that D == C
842  2) Use alpha==1,beta==0 and check that D == B
843  3) Use D==0 and D!=0 and check that result is the same
844  4) Check that input arguments are not modified
845  *********************************************************************/
846  {
847  const int p = 7;
848  Teuchos::RCP<MV> B, C, D;
849  std::vector<MagType> normsB1(p), normsB2(p),
850  normsC1(p), normsC2(p),
851  normsD1(p), normsD2(p);
852  ScalarType alpha = SCT::random(),
853  beta = SCT::random();
854 
855  B = MVT::Clone(*A,p);
856  C = MVT::Clone(*A,p);
857  D = MVT::Clone(*A,p);
858 
859  MVT::MvRandom(*B);
860  MVT::MvRandom(*C);
861  MVT::MvNorm(*B,normsB1);
862  MVT::MvNorm(*C,normsC1);
863 
864  // check that 0*B+1*C == C
865  MVT::MvAddMv(zero,*B,one,*C,*D);
866  MVT::MvNorm(*B,normsB2);
867  MVT::MvNorm(*C,normsC2);
868  MVT::MvNorm(*D,normsD1);
869  for (int i=0; i<p; i++) {
870  if ( normsB1[i] != normsB2[i] ) {
871  om->stream(Warnings)
872  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
873  << "Input arguments were modified." << endl;
874  return false;
875  }
876  else if ( normsC1[i] != normsC2[i] ) {
877  om->stream(Warnings)
878  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
879  << "Input arguments were modified." << endl;
880  return false;
881  }
882  else if ( normsC1[i] != normsD1[i] ) {
883  om->stream(Warnings)
884  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
885  << "Assignment did not work." << endl;
886  return false;
887  }
888  }
889 
890  // check that 1*B+0*C == B
891  MVT::MvAddMv(one,*B,zero,*C,*D);
892  MVT::MvNorm(*B,normsB2);
893  MVT::MvNorm(*C,normsC2);
894  MVT::MvNorm(*D,normsD1);
895  for (int i=0; i<p; i++) {
896  if ( normsB1[i] != normsB2[i] ) {
897  om->stream(Warnings)
898  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
899  << "Input arguments were modified." << endl;
900  return false;
901  }
902  else if ( normsC1[i] != normsC2[i] ) {
903  om->stream(Warnings)
904  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
905  << "Input arguments were modified." << endl;
906  return false;
907  }
908  else if ( normsB1[i] != normsD1[i] ) {
909  om->stream(Warnings)
910  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
911  << "Assignment did not work." << endl;
912  return false;
913  }
914  }
915 
916  // check that alpha*B+beta*C -> D is invariant under initial D
917  // first, try random D
918  MVT::MvRandom(*D);
919  MVT::MvAddMv(alpha,*B,beta,*C,*D);
920  MVT::MvNorm(*B,normsB2);
921  MVT::MvNorm(*C,normsC2);
922  MVT::MvNorm(*D,normsD1);
923  // check that input args are not modified
924  for (int i=0; i<p; i++) {
925  if ( normsB1[i] != normsB2[i] ) {
926  om->stream(Warnings)
927  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
928  << "Input arguments were modified." << endl;
929  return false;
930  }
931  else if ( normsC1[i] != normsC2[i] ) {
932  om->stream(Warnings)
933  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
934  << "Input arguments were modified." << endl;
935  return false;
936  }
937  }
938  // next, try zero D
939  MVT::MvInit(*D);
940  MVT::MvAddMv(alpha,*B,beta,*C,*D);
941  MVT::MvNorm(*B,normsB2);
942  MVT::MvNorm(*C,normsC2);
943  MVT::MvNorm(*D,normsD2);
944  // check that input args are not modified and that D is the same
945  // as the above test
946  for (int i=0; i<p; i++) {
947  if ( normsB1[i] != normsB2[i] ) {
948  om->stream(Warnings)
949  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
950  << "Input arguments were modified." << endl;
951  return false;
952  }
953  else if ( normsC1[i] != normsC2[i] ) {
954  om->stream(Warnings)
955  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
956  << "Input arguments were modified." << endl;
957  return false;
958  }
959  else if ( normsD1[i] != normsD2[i] ) {
960  om->stream(Warnings)
961  << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
962  << "Results varies depending on initial state of dest vectors." << endl;
963  return false;
964  }
965  }
966  }
967 
968  /*********** MvAddMv() ***********************************************
969  Similar to above, but where B or C are potentially the same
970  object as D. This case is commonly used, for example, to affect
971  A <- alpha*A
972  via
973  MvAddMv(alpha,A,zero,A,A)
974  ** OR **
975  MvAddMv(zero,A,alpha,A,A)
976 
977  The result is that the operation has to be "atomic". That is,
978  B and C are no longer reliable after D is modified, so that
979  the assignment to D must be the last thing to occur.
980 
981  D = alpha*B + beta*C
982 
983  1) Use alpha==0,beta==1 and check that D == C
984  2) Use alpha==1,beta==0 and check that D == B
985  *********************************************************************/
986  {
987  const int p = 7;
988  Teuchos::RCP<MV> B, D;
990  std::vector<MagType> normsB(p),
991  normsD(p);
992  std::vector<int> lclindex(p);
993  for (int i=0; i<p; i++) lclindex[i] = i;
994 
995  B = MVT::Clone(*A,p);
996  C = MVT::CloneView(*B,lclindex);
997  D = MVT::CloneViewNonConst(*B,lclindex);
998 
999  MVT::MvRandom(*B);
1000  MVT::MvNorm(*B,normsB);
1001 
1002  // check that 0*B+1*C == C
1003  MVT::MvAddMv(zero,*B,one,*C,*D);
1004  MVT::MvNorm(*D,normsD);
1005  for (int i=0; i<p; i++) {
1006  if ( normsB[i] != normsD[i] ) {
1007  om->stream(Warnings)
1008  << "*** ERROR *** MultiVecTraits::MvAddMv() #2" << endl
1009  << "Assignment did not work." << endl;
1010  return false;
1011  }
1012  }
1013 
1014  // check that 1*B+0*C == B
1015  MVT::MvAddMv(one,*B,zero,*C,*D);
1016  MVT::MvNorm(*D,normsD);
1017  for (int i=0; i<p; i++) {
1018  if ( normsB[i] != normsD[i] ) {
1019  om->stream(Warnings)
1020  << "*** ERROR *** MultiVecTraits::MvAddMv() #2" << endl
1021  << "Assignment did not work." << endl;
1022  return false;
1023  }
1024  }
1025 
1026  }
1027 
1028 
1029  /*********** MvTimesMatAddMv() 7 by 5 ********************************
1030  C = alpha*B*SDM + beta*C
1031  1) Use alpha==0, SDM!=0, beta==1 and check that C is unchanged
1032  2) Use alpha==0, SDM!=0, beta==0 and check that C is set to zero
1033  3) Use alpha==1, SDM==I, beta==0 and check that C is set to B
1034  4) Use alpha==1, SDM==0, beta==1 and check that C is unchanged
1035  5) Test with non-square matrices
1036  6) Always check that input arguments are not modified
1037  *********************************************************************/
1038  {
1039  const int p = 7, q = 5;
1040  Teuchos::RCP<MV> B, C;
1042  std::vector<MagType> normsC1(q), normsC2(q),
1043  normsB1(p), normsB2(p);
1044 
1045  B = MVT::Clone(*A,p);
1046  C = MVT::Clone(*A,q);
1047 
1048  // Test 1: alpha==0, SDM!=0, beta==1 and check that C is unchanged
1049  MVT::MvRandom(*B);
1050  MVT::MvRandom(*C);
1051  MVT::MvNorm(*B,normsB1);
1052  MVT::MvNorm(*C,normsC1);
1053  SDM.random();
1054  MVT::MvTimesMatAddMv(zero,*B,SDM,one,*C);
1055  MVT::MvNorm(*B,normsB2);
1056  MVT::MvNorm(*C,normsC2);
1057  for (int i=0; i<p; i++) {
1058  if ( normsB1[i] != normsB2[i] ) {
1059  om->stream(Warnings)
1060  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1061  << "Input vectors were modified." << endl;
1062  return false;
1063  }
1064  }
1065  for (int i=0; i<q; i++) {
1066  if ( normsC1[i] != normsC2[i] ) {
1067  om->stream(Warnings)
1068  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1069  << "Arithmetic test 1 failed." << endl;
1070  return false;
1071  }
1072  }
1073 
1074  // Test 2: alpha==0, SDM!=0, beta==0 and check that C is set to zero
1075  MVT::MvRandom(*B);
1076  MVT::MvRandom(*C);
1077  MVT::MvNorm(*B,normsB1);
1078  MVT::MvNorm(*C,normsC1);
1079  SDM.random();
1080  MVT::MvTimesMatAddMv(zero,*B,SDM,zero,*C);
1081  MVT::MvNorm(*B,normsB2);
1082  MVT::MvNorm(*C,normsC2);
1083  for (int i=0; i<p; i++) {
1084  if ( normsB1[i] != normsB2[i] ) {
1085  om->stream(Warnings)
1086  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1087  << "Input vectors were modified." << endl;
1088  return false;
1089  }
1090  }
1091  for (int i=0; i<q; i++) {
1092  if ( normsC2[i] != zero ) {
1093  om->stream(Warnings)
1094  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1095  << "Arithmetic test 2 failed: "
1096  << normsC2[i]
1097  << " != "
1098  << zero
1099  << endl;
1100  return false;
1101  }
1102  }
1103 
1104  // Test 3: alpha==1, SDM==|I|, beta==0 and check that C is set to B
1105  // |0|
1106  MVT::MvRandom(*B);
1107  MVT::MvRandom(*C);
1108  MVT::MvNorm(*B,normsB1);
1109  MVT::MvNorm(*C,normsC1);
1110  SDM.scale(zero);
1111  for (int i=0; i<q; i++) {
1112  SDM(i,i) = one;
1113  }
1114  MVT::MvTimesMatAddMv(one,*B,SDM,zero,*C);
1115  MVT::MvNorm(*B,normsB2);
1116  MVT::MvNorm(*C,normsC2);
1117  for (int i=0; i<p; i++) {
1118  if ( normsB1[i] != normsB2[i] ) {
1119  om->stream(Warnings)
1120  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1121  << "Input vectors were modified." << endl;
1122  return false;
1123  }
1124  }
1125  for (int i=0; i<q; i++) {
1126  if ( normsB1[i] != normsC2[i] ) {
1127  om->stream(Warnings)
1128  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1129  << "Arithmetic test 3 failed: "
1130  << normsB1[i]
1131  << " != "
1132  << normsC2[i]
1133  << endl;
1134  return false;
1135  }
1136  }
1137 
1138  // Test 4: alpha==1, SDM==0, beta==1 and check that C is unchanged
1139  MVT::MvRandom(*B);
1140  MVT::MvRandom(*C);
1141  MVT::MvNorm(*B,normsB1);
1142  MVT::MvNorm(*C,normsC1);
1143  SDM.scale(zero);
1144  MVT::MvTimesMatAddMv(one,*B,SDM,one,*C);
1145  MVT::MvNorm(*B,normsB2);
1146  MVT::MvNorm(*C,normsC2);
1147  for (int i=0; i<p; i++) {
1148  if ( normsB1[i] != normsB2[i] ) {
1149  om->stream(Warnings)
1150  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1151  << "Input vectors were modified." << endl;
1152  return false;
1153  }
1154  }
1155  for (int i=0; i<q; i++) {
1156  if ( normsC1[i] != normsC2[i] ) {
1157  om->stream(Warnings)
1158  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1159  << "Arithmetic test 4 failed." << endl;
1160  return false;
1161  }
1162  }
1163  }
1164 
1165  /*********** MvTimesMatAddMv() 5 by 7 ********************************
1166  C = alpha*B*SDM + beta*C
1167  1) Use alpha==0, SDM!=0, beta==1 and check that C is unchanged
1168  2) Use alpha==0, SDM!=0, beta==0 and check that C is set to zero
1169  3) Use alpha==1, SDM==I, beta==0 and check that C is set to B
1170  4) Use alpha==1, SDM==0, beta==1 and check that C is unchanged
1171  5) Test with non-square matrices
1172  6) Always check that input arguments are not modified
1173  *********************************************************************/
1174  {
1175  const int p = 5, q = 7;
1176  Teuchos::RCP<MV> B, C;
1178  std::vector<MagType> normsC1(q), normsC2(q),
1179  normsB1(p), normsB2(p);
1180 
1181  B = MVT::Clone(*A,p);
1182  C = MVT::Clone(*A,q);
1183 
1184  // Test 5: alpha==0, SDM!=0, beta==1 and check that C is unchanged
1185  MVT::MvRandom(*B);
1186  MVT::MvRandom(*C);
1187  MVT::MvNorm(*B,normsB1);
1188  MVT::MvNorm(*C,normsC1);
1189  SDM.random();
1190  MVT::MvTimesMatAddMv(zero,*B,SDM,one,*C);
1191  MVT::MvNorm(*B,normsB2);
1192  MVT::MvNorm(*C,normsC2);
1193  for (int i=0; i<p; i++) {
1194  if ( normsB1[i] != normsB2[i] ) {
1195  om->stream(Warnings)
1196  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1197  << "Input vectors were modified." << endl;
1198  return false;
1199  }
1200  }
1201  for (int i=0; i<q; i++) {
1202  if ( normsC1[i] != normsC2[i] ) {
1203  om->stream(Warnings)
1204  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1205  << "Arithmetic test 5 failed." << endl;
1206  return false;
1207  }
1208  }
1209 
1210  // Test 6: alpha==0, SDM!=0, beta==0 and check that C is set to zero
1211  MVT::MvRandom(*B);
1212  MVT::MvRandom(*C);
1213  MVT::MvNorm(*B,normsB1);
1214  MVT::MvNorm(*C,normsC1);
1215  SDM.random();
1216  MVT::MvTimesMatAddMv(zero,*B,SDM,zero,*C);
1217  MVT::MvNorm(*B,normsB2);
1218  MVT::MvNorm(*C,normsC2);
1219  for (int i=0; i<p; i++) {
1220  if ( normsB1[i] != normsB2[i] ) {
1221  om->stream(Warnings)
1222  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1223  << "Input vectors were modified." << endl;
1224  return false;
1225  }
1226  }
1227  for (int i=0; i<q; i++) {
1228  if ( normsC2[i] != zero ) {
1229  om->stream(Warnings)
1230  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1231  << "Arithmetic test 6 failed: "
1232  << normsC2[i]
1233  << " != "
1234  << zero
1235  << endl;
1236  return false;
1237  }
1238  }
1239 
1240  // Test 7: alpha==1, SDM==[I 0], beta==0 and check that C is set to B
1241  MVT::MvRandom(*B);
1242  MVT::MvRandom(*C);
1243  MVT::MvNorm(*B,normsB1);
1244  MVT::MvNorm(*C,normsC1);
1245  SDM.scale(zero);
1246  for (int i=0; i<p; i++) {
1247  SDM(i,i) = one;
1248  }
1249  MVT::MvTimesMatAddMv(one,*B,SDM,zero,*C);
1250  MVT::MvNorm(*B,normsB2);
1251  MVT::MvNorm(*C,normsC2);
1252  for (int i=0; i<p; i++) {
1253  if ( normsB1[i] != normsB2[i] ) {
1254  om->stream(Warnings)
1255  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1256  << "Input vectors were modified." << endl;
1257  return false;
1258  }
1259  }
1260  for (int i=0; i<p; i++) {
1261  if ( normsB1[i] != normsC2[i] ) {
1262  om->stream(Warnings)
1263  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1264  << "Arithmetic test 7 failed." << endl;
1265  return false;
1266  }
1267  }
1268  for (int i=p; i<q; i++) {
1269  if ( normsC2[i] != zero ) {
1270  om->stream(Warnings)
1271  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1272  << "Arithmetic test 7 failed." << endl;
1273  return false;
1274  }
1275  }
1276 
1277  // Test 8: alpha==1, SDM==0, beta==1 and check that C is unchanged
1278  MVT::MvRandom(*B);
1279  MVT::MvRandom(*C);
1280  MVT::MvNorm(*B,normsB1);
1281  MVT::MvNorm(*C,normsC1);
1282  SDM.scale(zero);
1283  MVT::MvTimesMatAddMv(one,*B,SDM,one,*C);
1284  MVT::MvNorm(*B,normsB2);
1285  MVT::MvNorm(*C,normsC2);
1286  for (int i=0; i<p; i++) {
1287  if ( normsB1[i] != normsB2[i] ) {
1288  om->stream(Warnings)
1289  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1290  << "Input vectors were modified." << endl;
1291  return false;
1292  }
1293  }
1294  for (int i=0; i<q; i++) {
1295  if ( normsC1[i] != normsC2[i] ) {
1296  om->stream(Warnings)
1297  << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1298  << "Arithmetic test 8 failed." << endl;
1299  return false;
1300  }
1301  }
1302  }
1303 
1304  return true;
1305 
1306  }
1307 
1308 
1309 
1315  template< class ScalarType, class MV, class OP>
1318  const Teuchos::RCP<const MV> &A,
1319  const Teuchos::RCP<const OP> &M) {
1320 
1321  using std::endl;
1322 
1323  /* OPT Contract:
1324  Apply()
1325  MV: OP*zero == zero
1326  Warn if OP is not deterministic (OP*A != OP*A)
1327  Does not modify input arguments
1328  *********************************************************************/
1329 
1330  typedef MultiVecTraits<ScalarType, MV> MVT;
1333  typedef typename SCT::magnitudeType MagType;
1334 
1335  const int numvecs = 10;
1336 
1337  Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs),
1338  C = MVT::Clone(*A,numvecs);
1339 
1340  std::vector<MagType> normsB1(numvecs), normsB2(numvecs),
1341  normsC1(numvecs), normsC2(numvecs);
1342  bool NonDeterministicWarning;
1343 
1344  /*********** Apply() *************************************************
1345  Verify:
1346  1) OP*B == OP*B; OP is deterministic (just warn on this)
1347  2) OP*zero == 0
1348  3) OP*B doesn't modify B
1349  4) OP*B is invariant under initial state of destination vectors
1350  *********************************************************************/
1351  MVT::MvInit(*B);
1352  MVT::MvRandom(*C);
1353  MVT::MvNorm(*B,normsB1);
1354  OPT::Apply(*M,*B,*C);
1355  MVT::MvNorm(*B,normsB2);
1356  MVT::MvNorm(*C,normsC2);
1357  for (int i=0; i<numvecs; i++) {
1358  if (normsB2[i] != normsB1[i]) {
1359  om->stream(Warnings)
1360  << "*** ERROR *** OperatorTraits::Apply() [1]" << endl
1361  << "Apply() modified the input vectors." << endl;
1362  return false;
1363  }
1364  if (normsC2[i] != SCT::zero()) {
1365  om->stream(Warnings)
1366  << "*** ERROR *** OperatorTraits::Apply() [1]" << endl
1367  << "Operator applied to zero did not return zero." << endl;
1368  return false;
1369  }
1370  }
1371 
1372  // If we send in a random matrix, we should not get a zero return
1373  MVT::MvRandom(*B);
1374  MVT::MvNorm(*B,normsB1);
1375  OPT::Apply(*M,*B,*C);
1376  MVT::MvNorm(*B,normsB2);
1377  MVT::MvNorm(*C,normsC2);
1378  bool ZeroWarning = false;
1379  for (int i=0; i<numvecs; i++) {
1380  if (normsB2[i] != normsB1[i]) {
1381  om->stream(Warnings)
1382  << "*** ERROR *** OperatorTraits::Apply() [2]" << endl
1383  << "Apply() modified the input vectors." << endl;
1384  return false;
1385  }
1386  if (normsC2[i] == SCT::zero() && ZeroWarning==false ) {
1387  om->stream(Warnings)
1388  << "*** ERROR *** OperatorTraits::Apply() [2]" << endl
1389  << "Operator applied to random vectors returned zero." << endl;
1390  ZeroWarning = true;
1391  }
1392  }
1393 
1394  // Apply operator with C init'd to zero
1395  MVT::MvRandom(*B);
1396  MVT::MvNorm(*B,normsB1);
1397  MVT::MvInit(*C);
1398  OPT::Apply(*M,*B,*C);
1399  MVT::MvNorm(*B,normsB2);
1400  MVT::MvNorm(*C,normsC1);
1401  for (int i=0; i<numvecs; i++) {
1402  if (normsB2[i] != normsB1[i]) {
1403  om->stream(Warnings)
1404  << "*** ERROR *** OperatorTraits::Apply() [3]" << endl
1405  << "Apply() modified the input vectors." << endl;
1406  return false;
1407  }
1408  }
1409 
1410  // Apply operator with C init'd to random
1411  // Check that result is the same as before; warn if not.
1412  // This could be a result of a bug, or a stochastic
1413  // operator. We do not want to prejudice against a
1414  // stochastic operator.
1415  MVT::MvRandom(*C);
1416  OPT::Apply(*M,*B,*C);
1417  MVT::MvNorm(*B,normsB2);
1418  MVT::MvNorm(*C,normsC2);
1419  NonDeterministicWarning = false;
1420  for (int i=0; i<numvecs; i++) {
1421  if (normsB2[i] != normsB1[i]) {
1422  om->stream(Warnings)
1423  << "*** ERROR *** OperatorTraits::Apply() [4]" << endl
1424  << "Apply() modified the input vectors." << endl;
1425  return false;
1426  }
1427  if (normsC1[i] != normsC2[i] && !NonDeterministicWarning) {
1428  om->stream(Warnings)
1429  << endl
1430  << "*** WARNING *** OperatorTraits::Apply() [4]" << endl
1431  << "Apply() returned two different results." << endl << endl;
1432  NonDeterministicWarning = true;
1433  }
1434  }
1435 
1436  return true;
1437 
1438  }
1439 
1440 }
1441 
1442 #endif
ScalarTraits< ScalarType >::magnitudeType normOne() const
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
bool TestOperatorTraits(const Teuchos::RCP< OutputManager< ScalarType > > &om, const Teuchos::RCP< const MV > &A, const Teuchos::RCP< const OP > &M)
This function tests the correctness of an operator implementation with respect to an OperatorTraits s...
Declaration of basic traits for the multivector type.
Virtual base class which defines basic traits for the operator type.
int scale(const ScalarType alpha)
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
Abstract class definition for Anasazi Output Managers.
Output managers remove the need for the eigensolver to know any information about the required output...
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
bool TestMultiVecTraits(const Teuchos::RCP< OutputManager< ScalarType > > &om, const Teuchos::RCP< const MV > &A)
This is a function to test the correctness of a MultiVecTraits specialization and multivector impleme...
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
OrdinalType numCols() const
Types and exceptions used within Anasazi solvers and interfaces.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
OrdinalType numRows() const