Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxx_main_band.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #include "Teuchos_Version.hpp"
15 
16 #define OTYPE int
17 #define STYPE std::complex<double>
18 
19 template<typename TYPE>
20 int PrintTestResults(std::string, TYPE, TYPE, bool);
21 
22 int ReturnCodeCheck(std::string, int, int, bool);
23 
27 
28 int main(int argc, char* argv[])
29 {
30 
31  int i;
32  bool verbose = 0;
33  if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
34 
35  if (verbose)
36  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
37 
38  int numberFailedTests = 0;
39  int returnCode = 0;
40  std::string testName = "";
41 
42  if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL BANDED DENSE MATRIX **********"<<std::endl<<std::endl;
43 
44  // default constructor test
45  BDMatrix DefConTest;
46  if (verbose) std::cout <<"default constructor -- construct empty matrix ";
47  if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) {
48  if (verbose) std::cout << "unsuccessful."<<std::endl;
49  numberFailedTests++;
50  } else {
51  if (verbose) std::cout << "successful."<<std::endl;
52  }
53 
54  // constructor 1 (matrix w/ dimension but empty)
55 
56  BDMatrix Con1Test( 4, 4, 1, 1 );
57  if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions ";
58  if ( Con1Test.numRows()!=4 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) {
59  if (verbose) std::cout << "unsuccessful."<<std::endl;
60  numberFailedTests++;
61  } else {
62  if (verbose) std::cout << "successful."<<std::endl;
63  }
64 
65  // constructor 2 (from array) tests
66 
67  STYPE a[25];
68  a[4] = 5; a[8] = 11; a[12] = 17; a[16] = 23;
69  a[1] = 0; a[5] = 6; a[9] = 12; a[13] = 18; a[17] = 24;
70  a[2] = 1; a[6] = 7; a[10] = 13; a[14] = 19;
71  a[3] = 2; a[7] = 8; a[11] = 14;
72 
73  BDMatrix C2T1ER;
74  C2T1ER.shape(5, 5, 2, 1);
75  C2T1ER(0, 0) = 0; C2T1ER(0, 1) = 5;
76  C2T1ER(1, 0) = 1; C2T1ER(1, 1) = 6; C2T1ER(1, 2) = 11;
77  C2T1ER(2, 0) = 2; C2T1ER(2, 1) = 7; C2T1ER(2, 2) = 12; C2T1ER(2, 3) = 17;
78  C2T1ER(3, 1) = 8; C2T1ER(3, 2) = 13; C2T1ER(3, 3) = 18; C2T1ER(3, 4) = 23;
79  C2T1ER(4, 2) = 14; C2T1ER(4, 3) = 19; C2T1ER(4, 4) = 24;
80 
81  // Create another lower triangular matrix with a view of 'a'.
82  BDMatrix Con2Test1(Teuchos::Copy, a, 4, 5, 5, 2, 1);
83  numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, C2T1ER, verbose);
84 
85  // constructor 3 (copy constructor)
86 
87  BDMatrix Con3TestCopy( C2T1ER );
88  if(verbose) std::cout <<"constructor 3 -- copy constructor ";
89  if ( Con3TestCopy != C2T1ER ) {
90  if (verbose) std::cout << "unsuccessful."<<std::endl;
91  numberFailedTests++;
92  } else {
93  if (verbose) std::cout << "successful."<<std::endl;
94  }
95 
96  BDMatrix Con3TestCopyTrans( C2T1ER, Teuchos::TRANS );
97  if(verbose) std::cout <<"constructor 3 -- copy constructor (transposed) ";
98  if ( Con3TestCopyTrans(0, 2) != C2T1ER(2, 0) ) {
99  if (verbose) std::cout << "unsuccessful."<<std::endl;
100  numberFailedTests++;
101  } else {
102  if (verbose) std::cout << "successful."<<std::endl;
103  }
104 
105  // constructor 4 (submatrix)
106 
107  BDMatrix Con4TestOrig(Teuchos::Copy, a, 4, 5, 5, 2, 1);
108  BDMatrix C4TS;
109  C4TS.shape( 3, 3, 2, 1 );
110  C4TS(0, 0) = 12; C4TS(0, 1) = 17;
111  C4TS(1, 0) = 13; C4TS(1, 1) = 18; C4TS(1, 2) = 23;
112  C4TS(2, 0) = 14; C4TS(2, 1) = 19; C4TS(2, 2) = 24;
113 
114  BDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 3, 3, 2);
115  numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, C4TS, verbose);
116  BDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 5, 5, 0);
117  numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose);
118  BDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 5, 5, 0);
119  numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestOrig, verbose);
120  BDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 3, 2);
121  numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, C4TS, verbose);
122 
123  // Norm Tests
124 
125  BDMatrix AAA;
126  AAA.shape( 5, 5, 2, 1 );
127  AAA(0, 0) = 0; AAA(0, 1) = 5;
128  AAA(1, 0) = 1; AAA(1, 1) = 6; AAA(1, 2) = 11;
129  AAA(2, 0) = 2; AAA(2, 1) = 7; AAA(2, 2) = 12; AAA(2, 3) = 17;
130  AAA(3, 1) = 8; AAA(3, 2) = 13; AAA(3, 3) = 18; AAA(3, 4) = 23;
131  AAA(4, 2) = 14; AAA(4, 3) = 19; AAA(4, 4) = 24;
132 
133  BDMatrix BBB;
134  numberFailedTests += PrintTestResults("normOne of a 5x5", AAA.normOne(), 54.0, verbose);
135  numberFailedTests += PrintTestResults("normInf of a 5x5", AAA.normInf(), 62.0, verbose);
137  numberFailedTests += PrintTestResults("normFrobenius of a 5x5", AAA.normFrobenius(), 4.0, verbose);
138  numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose);
139  numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose);
140  numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose);
141 
142  // Set Method Tests.
143 
144  BDMatrix CCC( 5, 5, 2, 1 );
145  // Randomize the entries in CCC.
146  testName = "random() -- enter random entries into matrix";
147  returnCode = CCC.random();
148  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
149  // Set the entries of CCC to 1.0.
150  testName = "putScalar() -- set every entry of this matrix to 1.0";
151  returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one());
152  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
153  // Check assignment operator.
154  BDMatrix CCC2( 5, 5, 2, 1 );
155  CCC2.assign( CCC );
156  if (verbose) std::cout << "assign() -- copy the values of an input matrix ";
157  if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) {
158  if (verbose) std::cout<< "successful" <<std::endl;
159  } else {
160  if (verbose) std::cout<< "unsuccessful" <<std::endl;
161  numberFailedTests++;
162  }
163  // Create a view into a submatrix of CCC
164  BDMatrix CCCview( Teuchos::View, CCC, 3, 3 );
165  BDMatrix CCCtest1( 3, 3, 2, 1 );
166  CCCtest1 = CCCview;
167  if (verbose) std::cout << "operator= -- small(empty) = large(view) ";
168  if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) {
169  if (verbose) std::cout<< "successful" <<std::endl;
170  } else {
171  if (verbose) std::cout<< "unsuccessful" <<std::endl;
172  numberFailedTests++;
173  }
174  CCCtest1 = CCC;
175  if (verbose) std::cout << "operator= -- small(view) = large(copy) ";
176  if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) {
177  if (verbose) std::cout<< "successful"<<std::endl;
178  } else {
179  if (verbose) std::cout<< "unsuccessful"<<std::endl;
180  numberFailedTests++;
181  }
182  BDMatrix CCCtest2( 3, 3, 2, 1 );
184  CCCtest1 = CCCtest2;
185  if (verbose) std::cout << "operator= -- large(copy) = small(copy) ";
186  if (CCCtest1.numRows()==3 ) {
187  if (verbose) std::cout<< "successful"<<std::endl;
188  } else {
189  if (verbose) std::cout<< "unsuccessful"<<std::endl;
190  numberFailedTests++;
191  }
192  CCCtest1 = CCCview;
193  if (verbose) std::cout << "operator= -- large(copy) = small(view) ";
194  if (CCCtest1.numRows()==3 && CCCtest1.stride()==4) {
195  if(verbose) std::cout<<"successful" <<std::endl;
196  } else {
197  if (verbose) std::cout<<"unsuccessful"<<std::endl;
198  numberFailedTests++;
199  }
200  BDMatrix CCCtest3( CCCview );
201  CCCtest1 += CCCtest3;
202  if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension ";
203  if (CCCtest1(1,1)==2.0) {
204  if(verbose) std::cout<<"successful" <<std::endl;
205  } else {
206  if (verbose) std::cout<<"unsuccessful"<<std::endl;
207  numberFailedTests++;
208  }
209  if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) ";
210  CCCtest1 += CCC;
211  if (CCCtest1(1,1)==2.0) {
212  if(verbose) std::cout<<"successful" <<std::endl;
213  } else {
214  if (verbose) std::cout<<"unsuccessful"<<std::endl;
215  numberFailedTests++;
216  }
217 
218  // Scale Tests.
219 
220  BDMatrix ScalTest( 8, 8, 2, 3 );
222  // Scale the entries by 8, it should be 8.
223  // The matrix is lower triangular, by default, so check a lower triangular entry.
224  if (verbose) std::cout << "operator*= -- scale matrix by some number ";
225  ScalTest *= 8.0;
226  if (ScalTest(5, 7) == 8.0) {
227  if (verbose) std::cout<< "successful." <<std::endl;
228  } else {
229  if (verbose) std::cout<< "unsuccessful." <<std::endl;
230  numberFailedTests++;
231  }
232 
233 
234  //
235  // If a test failed output the number of failed tests.
236  //
237  if(numberFailedTests > 0)
238  {
239  if (verbose) {
240  std::cout << "Number of failed tests: " << numberFailedTests << std::endl;
241  std::cout << "End Result: TEST FAILED" << std::endl;
242  return -1;
243  }
244  }
245  if(numberFailedTests == 0)
246  std::cout << "End Result: TEST PASSED" << std::endl;
247 
248  return 0;
249 }
250 
251 template<typename TYPE>
252 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose)
253 {
254  int result;
255  if(calculatedResult == expectedResult)
256  {
257  if(verbose) std::cout << testName << " successful." << std::endl;
258  result = 0;
259  }
260  else
261  {
262  if(verbose) std::cout << testName << " unsuccessful." << std::endl;
263  result = 1;
264  }
265  return result;
266 }
267 
268 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose)
269 {
270  int result;
271  if(expectedResult == 0)
272  {
273  if(returnCode == 0)
274  {
275  if(verbose) std::cout << testName << " test successful." << std::endl;
276  result = 0;
277  }
278  else
279  {
280  if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl;
281  result = 1;
282  }
283  }
284  else
285  {
286  if(returnCode != 0)
287  {
288  if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl;
289  result = 0;
290  }
291  else
292  {
293  if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl;
294  result = 1;
295  }
296  }
297  return result;
298 }
bool empty() const
Returns whether this matrix is empty.
OrdinalType numRows() const
Returns the row dimension of this matrix.
int PrintTestResults(std::string, TYPE, TYPE, bool)
Non-member helper functions on the templated serial, dense matrix/vector classes. ...
Templated serial dense matrix class.
int shape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Shape method for changing the size of a SerialBandDenseMatrix, initializing entries to zero...
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
Teuchos::SerialDenseVector< int, std::complex< Real > > DVector
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
#define STYPE
SerialBandDenseMatrix< OrdinalType, ScalarType > & assign(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
This class creates and provides basic support for dense vectors of templated type as a specialization...
This structure defines some basic traits for a scalar field type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
ScalarType * values() const
Data array access method.
Templated serial dense matrix class.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
This class creates and provides basic support for banded dense matrices of templated type...
Teuchos::SerialDenseMatrix< int, std::complex< Real > > DMatrix
std::string Teuchos_Version()
int ReturnCodeCheck(std::string, int, int, bool)
int main(int argc, char *argv[])
Teuchos::SerialBandDenseMatrix< OTYPE, STYPE > BDMatrix
Templated serial dense vector class.
OrdinalType numCols() const
Returns the column dimension of this matrix.
static T one()
Returns representation of one for this scalar type.
This class creates and provides basic support for dense rectangular matrix of templated type...