Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
numerics/test/DenseMatrix/cxx_main.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 
13 #include "Teuchos_Version.hpp"
14 
15 #define OTYPE int
16 #define STYPE std::complex<double>
17 
18 template<typename TYPE>
19 int PrintTestResults(std::string, TYPE, TYPE, bool);
20 
21 int ReturnCodeCheck(std::string, int, int, bool);
22 
23 typedef double Real;
26 //typedef Teuchos::SerialDenseVector<OTYPE, STYPE> DVector;
27 
28 int main(int argc, char* argv[])
29 {
30 
31  int i, j;
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 
43 
44  if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL DENSE MATRIX **********"<<std::endl<<std::endl;
45 
46  // default constructor test
47  DMatrix DefConTest;
48  if (verbose) std::cout <<"default constructor -- construct empty matrix ";
49  if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) {
50  if (verbose) std::cout << "unsuccessful."<<std::endl;
51  numberFailedTests++;
52  } else {
53  if (verbose) std::cout << "successful."<<std::endl;
54  }
55 
56  // constructor 1 (matrix w/ dimension but empty)
57 
58  DMatrix Con1Test( 3, 4 );
59  if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions ";
60  if ( Con1Test.numRows()!=3 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) {
61  if (verbose) std::cout << "unsuccessful."<<std::endl;
62  numberFailedTests++;
63  } else {
64  if (verbose) std::cout << "successful."<<std::endl;
65  }
66 
67  // constructor 2 (from array) tests
68 
69  STYPE a[9];
70  for(i = 0; i < 9; i++)
71  {
72  a[i] = i;
73  }
74  DMatrix Con2Test1ExpRes;
75  Con2Test1ExpRes.shape(2, 3);
76  Con2Test1ExpRes(0, 0) = 0; Con2Test1ExpRes(0, 1) = 2; Con2Test1ExpRes(0, 2) = 4;
77  Con2Test1ExpRes(1, 0) = 1; Con2Test1ExpRes(1, 1) = 3; Con2Test1ExpRes(1, 2) = 5;
78 
79  DMatrix Con2Test1(Teuchos::Copy, a, 2, 2, 3);
80  numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, Con2Test1ExpRes, verbose);
81 
82 
83  // constructor 3 (copy constructor)
84 
85  DMatrix Con3TestCopy( Con2Test1ExpRes );
86  if(verbose) std::cout <<"constructor 3 -- copy constructor ";
87  if ( Con3TestCopy != Con2Test1ExpRes ) {
88  if (verbose) std::cout << "unsuccessful."<<std::endl;
89  numberFailedTests++;
90  } else {
91  if (verbose) std::cout << "successful."<<std::endl;
92  }
93 
94  DMatrix Con3TestCopyTrans( Con2Test1ExpRes, Teuchos::TRANS );
95  if(verbose) std::cout <<"constructor 3 -- copy constructor (transposed) ";
96  if ( Con3TestCopyTrans(2, 0) != Con2Test1ExpRes(0, 2) ) {
97  if (verbose) std::cout << "unsuccessful."<<std::endl;
98  numberFailedTests++;
99  } else {
100  if (verbose) std::cout << "successful."<<std::endl;
101  }
102 
103  // constructor 4 (submatrix)
104 
105  DMatrix Con4TestOrig(Teuchos::Copy, a, 3, 3, 3);
106  DMatrix Con4TestSubmatrix;
107  Con4TestSubmatrix.shape(2, 2);
108  Con4TestSubmatrix(0, 0) = 4; Con4TestSubmatrix(0, 1) = 7;
109  Con4TestSubmatrix(1, 0) = 5; Con4TestSubmatrix(1, 1) = 8;
110  DMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 2, 2, 1, 1);
111  numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, Con4TestSubmatrix, verbose);
112  DMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 3, 3, 0, 0);
113  numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose);
114  DMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 2, 2, 1, 1);
115  numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestSubmatrix, verbose);
116  DMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 3, 0, 0);
117  numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, Con4TestOrig, verbose);
118 
119  // Norm Tests
120 
121  DMatrix AAA;
122  AAA.shape(3, 3);
123  AAA(0, 0) = 1; AAA(0, 1) = 2; AAA(0, 2) = 3;
124  AAA(1, 0) = 4; AAA(1, 1) = 5; AAA(1, 2) = 6;
125  AAA(2, 0) = 7; AAA(2, 1) = 8; AAA(2, 2) = 9;
126  DMatrix BBB;
127  numberFailedTests += PrintTestResults("normOne of a 3x3", AAA.normOne(), 18.0, verbose);
128  numberFailedTests += PrintTestResults("normInf of a 3x3", AAA.normInf(), 24.0, verbose);
130  numberFailedTests += PrintTestResults("normFrobenius of a 3x3", AAA.normFrobenius(), 3.0, verbose);
131  numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose);
132  numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose);
133  numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose);
134 
135  // multiply() -- dimensions tests
136 
137  DMatrix DimTest0x0A, DimTest0x0B, DimTest2x0, DimTest1x2, DimTest2x1, DimTest2x2A, DimTest2x2B,
138  DimTest3x3, DimTest0x2, DimTest0x0Result, DimTest1x1Result, DimTest2x0Result, DimTest1x2Result, DimTest2x1Result, DimTest2x2Result,
139  DimTest2x3Result, DimTest0x2Result, DimTest3x3Result;
140 
141  DimTest0x2.shape(0, 2);
142  DimTest2x0.shape(2, 0);
143  DimTest1x2.shape(1, 2);
144  DimTest2x1.shape(2, 1);
145  DimTest2x2A.shape(2, 2);
146  DimTest2x2B.shape(2, 2);
147  DimTest3x3.shape(3, 3);
148  DimTest0x2Result.shape(0, 2);
149  DimTest1x1Result.shape(1, 1);
150  DimTest2x0Result.shape(2, 0);
151  DimTest1x2Result.shape(1, 2);
152  DimTest2x1Result.shape(2, 1);
153  DimTest2x2Result.shape(2, 2);
154  DimTest2x3Result.shape(2, 3);
155  DimTest3x3Result.shape(3, 3);
156 
157  returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest2x2B, 1);
158  testName = "multiply() -- dimensions -- compatible square matrices";
159  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
160  returnCode = DimTest2x3Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest3x3, 1);
161  testName = "multiply() -- dimensions -- incompatible square matrices";
162  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
163  returnCode = DimTest1x1Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest1x2, DimTest2x1, 1);
164  testName = "multiply() -- dimensions -- compatible nonsquare matrices";
165  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
166  returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x1, DimTest1x2, 1);
167  testName = "multiply() -- dimensions -- compatible nonsquare matrices";
168  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
169  returnCode = DimTest2x1Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x1, DimTest2x1, 1);
170  testName = "multiply() -- dimensions -- incompatible nonsquare matrices";
171  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
172  returnCode = DimTest1x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest1x2, DimTest1x2, 1);
173  testName = "multiply() -- dimensions -- incompatible nonsquare matrices";
174  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
175  returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x0, DimTest2x2A, 1);
176  testName = "multiply() -- dimensions -- first operand bad numCols";
177  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
178  returnCode = DimTest0x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest0x2, 1);
179  testName = "multiply() -- dimensions -- second operand bad numRows";
180  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
181  returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest2x0, 1);
182  testName = "multiply() -- dimensions -- second operand bad numCols";
183  numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose);
184 
185  // multiply() -- multiplication results tests
186 
187  DMatrix MultTest2x2A, MultTest2x2B, MultTest3x3A, MultTest3x3B, MultTest2x2ATimes2x2B,
188  MultTest3x3ATimes3x3B, MultTest2x2BTimes2x2A, MultTest3x3BTimes3x3A, MultTest2x2ATimes2x2BExpResult, MultTest2x2BTimes2x2AExpResult,
189  MultTest3x3ATimes3x3BExpResult, MultTest3x3BTimes3x3AExpResult, MultTest2x3A, MultTest2x3B, MultTest3x2A, MultTest3x2B,
190  MultTest2x3ATimes3x2B, MultTest3x2ATimes2x3B, MultTest2x3BTimes3x2A, MultTest3x2BTimes2x3A, MultTest2x3ATimes3x2BExpResult,
191  MultTest3x2ATimes2x3BExpResult, MultTest2x3BTimes3x2AExpResult, MultTest3x2BTimes2x3AExpResult;
192 
193  MultTest2x2A.shape(2, 2);
194  MultTest2x2B.shape(2, 2);
195  MultTest3x3A.shape(3, 3);
196  MultTest3x3B.shape(3, 3);
197  MultTest2x2ATimes2x2B.shape(2, 2);
198  MultTest2x2BTimes2x2A.shape(2, 2);
199  MultTest3x3ATimes3x3B.shape(3, 3);
200  MultTest3x3BTimes3x3A.shape(3, 3);
201  MultTest2x2ATimes2x2BExpResult.shape(2, 2);
202  MultTest2x2BTimes2x2AExpResult.shape(2, 2);
203  MultTest3x3ATimes3x3BExpResult.shape(3, 3);
204  MultTest3x3BTimes3x3AExpResult.shape(3, 3);
205  MultTest2x3A.shape(2, 3);
206  MultTest2x3B.shape(2, 3);
207  MultTest3x2A.shape(3, 2);
208  MultTest3x2B.shape(3, 2);
209  MultTest2x3ATimes3x2B.shape(2, 2);
210  MultTest3x2ATimes2x3B.shape(3, 3);
211  MultTest2x3BTimes3x2A.shape(2, 2);
212  MultTest3x2BTimes2x3A.shape(3, 3);
213  MultTest2x3ATimes3x2BExpResult.shape(2, 2);
214  MultTest3x2ATimes2x3BExpResult.shape(3, 3);
215  MultTest2x3BTimes3x2AExpResult.shape(2, 2);
216  MultTest3x2BTimes2x3AExpResult.shape(3, 3);
217 
218  for(i = 0; i < 2; i++)
219  {
220  for(j = 0; j < 2; j++)
221  {
222  MultTest2x2A(i, j) = i + j;
223  MultTest2x2B(i, j) = (i * j) + 1;
224  }
225  }
226  for(i = 0; i < 3; i++)
227  {
228  for(j = 0; j < 3; j++)
229  {
230  MultTest3x3A(i, j) = i + j;
231  MultTest3x3B(i, j) = (i * j) + 1;
232  }
233  }
234 
235  MultTest2x2ATimes2x2BExpResult(0, 0) = 1; MultTest2x2ATimes2x2BExpResult(0, 1) = 2;
236  MultTest2x2ATimes2x2BExpResult(1, 0) = 3; MultTest2x2ATimes2x2BExpResult(1, 1) = 5;
237  MultTest2x2BTimes2x2AExpResult(0, 0) = 1; MultTest2x2BTimes2x2AExpResult(0, 1) = 3;
238  MultTest2x2BTimes2x2AExpResult(1, 0) = 2; MultTest2x2BTimes2x2AExpResult(1, 1) = 5;
239  MultTest3x3ATimes3x3BExpResult(0, 0) = 3; MultTest3x3ATimes3x3BExpResult(0, 1) = 8; MultTest3x3ATimes3x3BExpResult(0, 2) = 13;
240  MultTest3x3ATimes3x3BExpResult(1, 0) = 6; MultTest3x3ATimes3x3BExpResult(1, 1) = 14; MultTest3x3ATimes3x3BExpResult(1, 2) = 22;
241  MultTest3x3ATimes3x3BExpResult(2, 0) = 9; MultTest3x3ATimes3x3BExpResult(2, 1) = 20; MultTest3x3ATimes3x3BExpResult(2, 2) = 31;
242  MultTest3x3BTimes3x3AExpResult(0, 0) = 3; MultTest3x3BTimes3x3AExpResult(0, 1) = 6; MultTest3x3BTimes3x3AExpResult(0, 2) = 9;
243  MultTest3x3BTimes3x3AExpResult(1, 0) = 8; MultTest3x3BTimes3x3AExpResult(1, 1) = 14; MultTest3x3BTimes3x3AExpResult(1, 2) = 20;
244  MultTest3x3BTimes3x3AExpResult(2, 0) = 13; MultTest3x3BTimes3x3AExpResult(2, 1) = 22; MultTest3x3BTimes3x3AExpResult(2, 2) = 31;
245  MultTest2x3A(0, 0) = 1; MultTest2x3A(0, 1) = 2; MultTest2x3A(0, 2) = 3;
246  MultTest2x3A(1, 0) = 4; MultTest2x3A(1, 1) = 5; MultTest2x3A(1, 2) = 6;
247  MultTest3x2A(0, 0) = 1; MultTest3x2A(0, 1) = 2;
248  MultTest3x2A(1, 0) = 3; MultTest3x2A(1, 1) = 4;
249  MultTest3x2A(2, 0) = 5; MultTest3x2A(2, 1) = 6;
250  MultTest2x3B(0, 0) = 0; MultTest2x3B(0, 1) = 2; MultTest2x3B(0, 2) = 4;
251  MultTest2x3B(1, 0) = 6; MultTest2x3B(1, 1) = 8; MultTest2x3B(1, 2) = 10;
252  MultTest3x2B(0, 0) = 0; MultTest3x2B(0, 1) = 2;
253  MultTest3x2B(1, 0) = 4; MultTest3x2B(1, 1) = 6;
254  MultTest3x2B(2, 0) = 8; MultTest3x2B(2, 1) = 10;
255  MultTest2x3ATimes3x2BExpResult(0, 0) = 32; MultTest2x3ATimes3x2BExpResult(0, 1) = 44;
256  MultTest2x3ATimes3x2BExpResult(1, 0) = 68; MultTest2x3ATimes3x2BExpResult(1, 1) = 98;
257  MultTest3x2ATimes2x3BExpResult(0, 0) = 12; MultTest3x2ATimes2x3BExpResult(0, 1) = 18; MultTest3x2ATimes2x3BExpResult(0, 2) = 24;
258  MultTest3x2ATimes2x3BExpResult(1, 0) = 24; MultTest3x2ATimes2x3BExpResult(1, 1) = 38; MultTest3x2ATimes2x3BExpResult(1, 2) = 52;
259  MultTest3x2ATimes2x3BExpResult(2, 0) = 36; MultTest3x2ATimes2x3BExpResult(2, 1) = 58; MultTest3x2ATimes2x3BExpResult(2, 2) = 80;
260  MultTest2x3BTimes3x2AExpResult(0, 0) = 26; MultTest2x3BTimes3x2AExpResult(0, 1) = 32;
261  MultTest2x3BTimes3x2AExpResult(1, 0) = 80; MultTest2x3BTimes3x2AExpResult(1, 1) = 104;
262  MultTest3x2BTimes2x3AExpResult(0, 0) = 8; MultTest3x2BTimes2x3AExpResult(0, 1) = 10; MultTest3x2BTimes2x3AExpResult(0, 2) = 12;
263  MultTest3x2BTimes2x3AExpResult(1, 0) = 28; MultTest3x2BTimes2x3AExpResult(1, 1) = 38; MultTest3x2BTimes2x3AExpResult(1, 2) = 48;
264  MultTest3x2BTimes2x3AExpResult(2, 0) = 48; MultTest3x2BTimes2x3AExpResult(2, 1) = 66; MultTest3x2BTimes2x3AExpResult(2, 2) = 84;
265 
266  MultTest2x2ATimes2x2B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x2A, MultTest2x2B, 1);
267  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x2 * 2x2", MultTest2x2ATimes2x2B, MultTest2x2ATimes2x2BExpResult, verbose);
268  MultTest2x2BTimes2x2A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x2B, MultTest2x2A, 1);
269  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x2 * 2x2", MultTest2x2BTimes2x2A, MultTest2x2BTimes2x2AExpResult, verbose);
270  MultTest3x3ATimes3x3B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x3A, MultTest3x3B, 1);
271  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x3 * 3x3", MultTest3x3ATimes3x3B, MultTest3x3ATimes3x3BExpResult, verbose);
272  MultTest3x3BTimes3x3A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x3B, MultTest3x3A, 1);
273  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x3 * 3x3", MultTest3x3BTimes3x3A, MultTest3x3BTimes3x3AExpResult, verbose);
274  MultTest2x3ATimes3x2B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x3A, MultTest3x2B, 1);
275  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x3 * 3x2", MultTest2x3ATimes3x2B, MultTest2x3ATimes3x2BExpResult, verbose);
276  MultTest2x3BTimes3x2A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x3B, MultTest3x2A, 1);
277  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x3 * 3x2", MultTest2x3BTimes3x2A, MultTest2x3BTimes3x2AExpResult, verbose);
278  MultTest3x2ATimes2x3B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x2A, MultTest2x3B, 1);
279  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x2 * 2x3", MultTest3x2ATimes2x3B, MultTest3x2ATimes2x3BExpResult, verbose);
280  MultTest3x2BTimes2x3A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x2B, MultTest2x3A, 1);
281  numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x2 * 2x3", MultTest3x2BTimes2x3A, MultTest3x2BTimes2x3AExpResult, verbose);
282 
283  DMatrix MultTestHugeA, MultTestHugeB, MultTestHugeATimesHugeBExpResult,
284  MultTestHugeATimesHugeB;
285 
286  const int hugeSize = 100;
287  MultTestHugeA.shape(hugeSize, hugeSize);
288  MultTestHugeB.shape(hugeSize, hugeSize);
289  MultTestHugeATimesHugeBExpResult.shape(hugeSize, hugeSize);
290  MultTestHugeATimesHugeB.shape(hugeSize, hugeSize);
291 
292  for(i = 0; i < hugeSize; i++)
293  {
294  for(j = 0; j < hugeSize; j++)
295  {
296  MultTestHugeA(i, j) = j;
297  MultTestHugeB(i, j) = i;
298  MultTestHugeATimesHugeBExpResult(i, j) = 328350;
299  }
300  }
301 
302  MultTestHugeATimesHugeB.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0,
303  MultTestHugeA, MultTestHugeB, 1.0);
304  numberFailedTests += PrintTestResults(
305  "multiply() -- mult. results -- huge * huge",
306  MultTestHugeATimesHugeB, MultTestHugeATimesHugeBExpResult, verbose);
307 
308  //
309  // Check scale methods.
310  //
311  DMatrix ScalTest( 8, 8 );
313  // Scale the entries by 8, it should be 8.
314  if (verbose) std::cout << "scale() -- scale matrix by some number ";
315  returnCode = ScalTest.scale( 8.0 );
316  if (ScalTest(2, 3) == 8.0) {
317  if (verbose) std::cout<< "successful." <<std::endl;
318  } else {
319  if (verbose) std::cout<< "unsuccessful." <<std::endl;
320  numberFailedTests++;
321  }
322  // Pointwise scale the entries by zero, they all should be zero.
323  DMatrix ScalTest2( 8, 8 );
324  if (verbose) std::cout << "scale() -- point-wise scale matrix ";
325  ScalTest.scale( ScalTest2 );
326  if (ScalTest.normOne() == 0.0) {
327  if (verbose) std::cout<< "successful." <<std::endl;
328  } else {
329  if (verbose) std::cout<< "unsuccessful." <<std::endl;
330  numberFailedTests++;
331  }
332  //
333  // Check set methods.
334  //
335  DMatrix CCC( 5, 5 );
336  // Randomize the entries in CCC.
337  testName = "random() -- enter random entries into matrix";
338  returnCode = CCC.random();
339  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
340  // Set the entries of CCC to 1.0.
341  testName = "putScalar() -- set every entry of this matrix to 1.0";
342  returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one());
343  numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
344  // Check assignment operator.
345  DMatrix CCC2( 5, 5 );
346  CCC2.assign( CCC );
347  if (verbose) std::cout << "assign() -- copy the values of an input matrix ";
348  if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) {
349  if (verbose) std::cout<< "successful" <<std::endl;
350  } else {
351  if (verbose) std::cout<< "unsuccessful" <<std::endl;
352  numberFailedTests++;
353  }
354  // Create a swap testing matrix
355  DMatrix CCCswap( 2, 3 );
356  CCCswap.random();
357  DMatrix copyCCC(Teuchos::Copy, CCC);
358  DMatrix copyCCCswap(Teuchos::Copy, CCCswap);
359  if (verbose) std::cout << "swap() -- swap the values and attributes of two matrices -- ";
360  CCCswap.swap(CCC);
361  bool op_result = ( (CCCswap == copyCCC) && (CCC == copyCCCswap) );
362  if (verbose)
363  std::cout << (op_result ? "successful" : "failed" )<<std::endl;
364  if( !op_result )
365  numberFailedTests++;
366  // Swap back using other matrix and allow downstream testing to proceed as if without swapping
367  CCC.swap(CCCswap);
368 
369  // Create a view into a submatrix of CCC
370  DMatrix CCCview( Teuchos::View, CCC, 3, 3 );
371  DMatrix CCCtest1( 2, 3 );
372  CCCtest1 = CCCview;
373  if (verbose) std::cout << "operator= -- small(empty) = large(view) ";
374  if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) {
375  if (verbose) std::cout<< "successful" <<std::endl;
376  } else {
377  if (verbose) std::cout<< "unsuccessful" <<std::endl;
378  numberFailedTests++;
379  }
380  CCCtest1 = CCC;
381  if (verbose) std::cout << "operator= -- small(view) = large(copy) ";
382  if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) {
383  if (verbose) std::cout<< "successful"<<std::endl;
384  } else {
385  if (verbose) std::cout<< "unsuccessful"<<std::endl;
386  numberFailedTests++;
387  }
388  DMatrix CCCtest2( 2, 2 );
389  CCCtest2 = 3.0;
390  CCCtest1 = CCCtest2;
391  if (verbose) std::cout << "operator= -- large(copy) = small(copy) ";
392  if (CCCtest1.numRows()==2 ) {
393  if (verbose) std::cout<< "successful"<<std::endl;
394  } else {
395  if (verbose) std::cout<< "unsuccessful"<<std::endl;
396  numberFailedTests++;
397  }
398  CCCtest1 = CCCview;
399  if (verbose) std::cout << "operator= -- large(copy) = small(view) ";
400  if (CCCtest1.numRows()==3 && CCCtest1.stride()==5) {
401  if(verbose) std::cout<<"successful" <<std::endl;
402  } else {
403  if (verbose) std::cout<<"unsuccessful"<<std::endl;
404  numberFailedTests++;
405  }
406 
407  DMatrix CCCtest3( CCCview );
408  CCCtest1 += CCCtest3;
409  if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension ";
410  if (CCCtest1(1,1)==2.0) {
411  if(verbose) std::cout<<"successful" <<std::endl;
412  } else {
413  if (verbose) std::cout<<"unsuccessful"<<std::endl;
414  numberFailedTests++;
415  }
416  if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) ";
417  CCCtest1 += CCC;
418  if (CCCtest1(1,1)==2.0) {
419  if(verbose) std::cout<<"successful" <<std::endl;
420  } else {
421  if (verbose) std::cout<<"unsuccessful"<<std::endl;
422  numberFailedTests++;
423  }
424  //
425  // Check overloaded operators.
426  //
427  op_result;
428  MultTestHugeATimesHugeB.reshape(10, 10);
429  op_result = (MultTestHugeATimesHugeB == MultTestHugeATimesHugeBExpResult);
430  if (verbose) {
431  std::cout << "operator== -- results -- small == huge "<< (op_result == false ? "successful" : "failed" )<<std::endl;
432  }
433  op_result = (MultTestHugeATimesHugeB != MultTestHugeATimesHugeBExpResult);
434  if (verbose) {
435  std::cout << "operator!= -- results -- small != huge "<< (op_result == true ? "successful" : "failed" )<<std::endl;
436  std::cout << std::endl<< MultTestHugeATimesHugeB << std::endl;
437  //These won't work unless boundschecking is enabled.
438  //std::cout << MultTestHugeATimesHugeB(100, 1) << std::endl;
439  //std::cout << MultTestHugeATimesHugeB(1, 100) << std::endl;
440  }
441 
442 
443  if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL DENSE VECTOR **********"<<std::endl<<std::endl;
444 
445  DVector DefConTestV;
446  if (verbose) std::cout <<"default constructor -- construct empty std::vector ";
447  if ( DefConTestV.values()!=NULL || DefConTestV.length()!=0 || DefConTestV.numRows()!=0 ||DefConTestV.stride()!=0 ) {
448  if (verbose) std::cout << "unsuccessful."<<std::endl;
449  numberFailedTests++;
450  } else {
451  if (verbose) std::cout << "successful."<<std::endl;
452  }
453 
454  // constructor 1 (matrix w/ dimension but empty)
455 
456  DVector Con1TestV( 3 );
457  if (verbose) std::cout <<"constructor 1 -- empty std::vector with given dimensions ";
458  if ( Con1TestV.length()!=3 || Con1TestV.numCols()!=1 || Con1TestV( 1 )!=0.0 ) {
459  if (verbose) std::cout << "unsuccessful."<<std::endl;
460  numberFailedTests++;
461  } else {
462  if (verbose) std::cout << "successful."<<std::endl;
463  }
464 
465  // constructor 2 (from array) tests
466 
467  DVector Con2Test1V(Teuchos::Copy, a, 4);
468  if (verbose) std::cout <<"constructor 2 -- construct std::vector from array subrange ";
469  if ( Con2Test1V.numRows()!=4 || Con2Test1V.numCols()!=1 || Con2Test1V[ 2 ]!=2.0 ) {
470  if (verbose) std::cout << "unsuccessful."<<std::endl;
471  numberFailedTests++;
472  } else {
473  if (verbose) std::cout << "successful."<<std::endl;
474  }
475 
476  // constructor 3 (copy constructor)
477 
478  DVector Con3TestCopyV( Con2Test1V );
479  if(verbose) std::cout <<"constructor 3 -- copy constructor ";
480  if ( Con3TestCopyV != Con2Test1V ) {
481  if (verbose) std::cout << "unsuccessful."<<std::endl;
482  numberFailedTests++;
483  } else {
484  if (verbose) std::cout << "successful."<<std::endl;
485  }
486 
487  // non-member helper function (construct vector view of matrix column)
488 
490  DVector ColViewTestV = Teuchos::getCol<OTYPE,STYPE>( Teuchos::View, AAA, col );
491  if (verbose) std::cout <<"non-method helper function -- construct vector view of second column of matrix ";
492  if ( ColViewTestV.normInf() != 1.0 || ColViewTestV.normOne() != 3.0 ) {
493  if (verbose) std::cout << "unsuccessful."<<std::endl;
494  numberFailedTests++;
495  } else {
496  if (verbose) std::cout << "successful."<<std::endl;
497  }
498 
499  // checking norms
500 
501  numberFailedTests += PrintTestResults("normOne of a 3x1 std::vector", Con2Test1V.normOne(), 6.0, verbose);
502  numberFailedTests += PrintTestResults("normInf of a 3x1 std::vector", Con2Test1V.normInf(), 3.0, verbose);
503  Con2Test1V = Teuchos::ScalarTraits<STYPE>::one();
504  numberFailedTests += PrintTestResults("normFrobenius of a 3x1 std::vector", Con2Test1V.normFrobenius(), 2.0, verbose);
505 
506  // check size/resize
507 
508  DVector SizeTestV1;
509  SizeTestV1.size( 5 );
510  if(verbose) std::cout <<"size() -- test ";
511  if (SizeTestV1( 4 )!= 0.0) {
512  if (verbose) std::cout << "unsuccessful."<<std::endl;
513  numberFailedTests++;
514  } else {
515  if (verbose) std::cout << "successful."<<std::endl;
516  }
517  SizeTestV1 = 2.0*Teuchos::ScalarTraits<STYPE>::one();
518  SizeTestV1.resize( 10 );
519  if(verbose) std::cout <<"resize() -- test small --> large ";
520  if (SizeTestV1[ 4 ]!= 2.0 || SizeTestV1[ 8 ]!=0.0 ) {
521  if (verbose) std::cout << "unsuccessful."<<std::endl;
522  numberFailedTests++;
523  } else {
524  if (verbose) std::cout << "successful."<<std::endl;
525  }
526  SizeTestV1.resize( 3 );
527  if(verbose) std::cout <<"resize() -- test large --> small ";
528  if (SizeTestV1( 2 )!= 2.0) {
529  if (verbose) std::cout << "unsuccessful."<<std::endl;
530  numberFailedTests++;
531  } else {
532  if (verbose) std::cout << "successful."<<std::endl;
533  }
534 
535  DVector OpEqTestV1( 10 ); OpEqTestV1 = 3.0*Teuchos::ScalarTraits<STYPE>::one();
536  DVector OpEqTestV2( Teuchos::View, OpEqTestV1.values(), 3 );
537  DVector OpEqTestV3( 2 );
538  OpEqTestV3 = OpEqTestV2;
539  if (verbose) std::cout << "operator= -- small(empty) = large(view) ";
540  if (OpEqTestV3.length()==3 && OpEqTestV3.values()==OpEqTestV2.values()) {
541  if (verbose) std::cout<< "successful"<<std::endl;
542  } else {
543  if (verbose) std::cout<< "unsuccessful"<<std::endl;
544  numberFailedTests++;
545  }
546  OpEqTestV3 = OpEqTestV1;
547  if (verbose) std::cout << "operator= -- small(view) = large(copy) ";
548  if (OpEqTestV3.length()==10 && OpEqTestV3.values()!=OpEqTestV1.values()) {
549  if (verbose) std::cout<< "successful"<<std::endl;
550  } else {
551  if (verbose) std::cout<< "unsuccessful"<<std::endl;
552  numberFailedTests++;
553  }
554  OpEqTestV3.size(5);
555  OpEqTestV3 = OpEqTestV1;
556  if (verbose) std::cout << "operator= -- small(copy) = large(copy) ";
557  if (OpEqTestV3.length()==10 && OpEqTestV3.values()!=OpEqTestV1.values() && OpEqTestV3[ 9 ]==3.0) {
558  if (verbose) std::cout<< "successful"<<std::endl;
559  } else {
560  if (verbose) std::cout<< "unsuccessful"<<std::endl;
561  numberFailedTests++;
562  }
563 
564  DVector OpSumTestV1( OpEqTestV2 );
565  OpSumTestV1 += OpEqTestV2;
566  if (verbose) std::cout << "operator+= -- add two vectors of the same size, but different leading dimension ";
567  if (OpSumTestV1( 1 )==6.0) {
568  if (verbose) std::cout<<"successful" <<std::endl;
569  } else {
570  if (verbose) std::cout<<"unsuccessful"<<std::endl;
571  numberFailedTests++;
572  }
573  if (verbose) std::cout << "operator+= -- add two vectors of different size (nothing should change) ";
574  OpSumTestV1 += OpEqTestV1;
575  if (OpSumTestV1( 1 )==6.0) {
576  if (verbose) std::cout<<"successful" <<std::endl;
577  } else {
578  if (verbose) std::cout<<"unsuccessful"<<std::endl;
579  numberFailedTests++;
580  }
581 
582  DVector OpCompTestV1( 5 );
583  OpCompTestV1 = 2.0*Teuchos::ScalarTraits<STYPE>::one();
584  if(verbose) std::cout <<"operator== -- test large == small ";
585  if (OpCompTestV1 == SizeTestV1) {
586  if (verbose) std::cout << "unsuccessful."<<std::endl;
587  numberFailedTests++;
588  } else {
589  if (verbose) std::cout << "successful."<<std::endl;
590  }
591  if(verbose) std::cout <<"operator!= -- test large != small ";
592  if (OpCompTestV1 != SizeTestV1) {
593  if (verbose) std::cout << "successful."<<std::endl;
594  } else {
595  if (verbose) std::cout << "successful."<<std::endl;
596  numberFailedTests++;
597  }
598 
599  DVector ColSetTestV( AAA.numRows() );
600  ColSetTestV.putScalar( 2.0 );
601  bool ret = Teuchos::setCol<OTYPE,STYPE>( ColSetTestV, col, AAA );
602  if (verbose) std::cout <<"non-method helper function -- set second column of matrix with vector ";
603  if ( ColViewTestV.normInf() != 2.0 || ColViewTestV.normOne() != 6.0 || ret == false ) {
604  if (verbose) std::cout << "unsuccessful."<<std::endl;
605  numberFailedTests++;
606  } else {
607  if (verbose) std::cout << "successful."<<std::endl;
608  }
609  //
610  // If a test failed output the number of failed tests.
611  //
612  if(numberFailedTests > 0)
613  {
614  if (verbose) {
615  std::cout << "Number of failed tests: " << numberFailedTests << std::endl;
616  std::cout << "End Result: TEST FAILED" << std::endl;
617  return -1;
618  }
619  }
620  if(numberFailedTests == 0)
621  std::cout << "End Result: TEST PASSED" << std::endl;
622 
623  return 0;
624 }
625 
626 template<typename TYPE>
627 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose)
628 {
629  int result;
630  if(calculatedResult == expectedResult)
631  {
632  if(verbose) std::cout << testName << " successful." << std::endl;
633  result = 0;
634  }
635  else
636  {
637  if(verbose) std::cout << testName << " unsuccessful." << std::endl;
638  result = 1;
639  }
640  return result;
641 }
642 
643 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose)
644 {
645  int result;
646  if(expectedResult == 0)
647  {
648  if(returnCode == 0)
649  {
650  if(verbose) std::cout << testName << " test successful." << std::endl;
651  result = 0;
652  }
653  else
654  {
655  if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl;
656  result = 1;
657  }
658  }
659  else
660  {
661  if(returnCode != 0)
662  {
663  if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl;
664  result = 0;
665  }
666  else
667  {
668  if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl;
669  result = 1;
670  }
671  }
672  return result;
673 }
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
ScalarType * values() const
Data array access method.
static T one()
Returns representation of one for this ordinal type.
int PrintTestResults(std::string, TYPE, TYPE, bool)
Non-member helper functions on the templated serial, dense matrix/vector classes. ...
Templated serial dense matrix class.
Teuchos::SerialDenseVector< int, std::complex< Real > > DVector
void swap(SerialDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
Multiply A * B and add them to this; this = beta * this + alpha*A*B.
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
This class creates and provides basic support for dense vectors of templated type as a specialization...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
This structure defines some basic traits for a scalar field type.
bool empty() const
Returns whether this matrix is empty.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
OrdinalType length() const
Returns the length of this vector.
Teuchos::SerialDenseMatrix< int, std::complex< Real > > DMatrix
std::string Teuchos_Version()
int ReturnCodeCheck(std::string, int, int, bool)
int main(int argc, char *argv[])
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
int size(OrdinalType length_in)
Size method for changing the size of a SerialDenseVector, initializing entries to zero...
int random()
Set all values in the matrix to be random numbers.
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
Templated serial dense vector class.
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero...
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
static T one()
Returns representation of one for this scalar type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numRows() const
Returns the row dimension of this matrix.
This class creates and provides basic support for dense rectangular matrix of templated type...