Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
cxx_main_band.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools 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// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
46#include "Teuchos_Version.hpp"
47
48#define OTYPE int
49#define STYPE std::complex<double>
50
51template<typename TYPE>
52int PrintTestResults(std::string, TYPE, TYPE, bool);
53
54int ReturnCodeCheck(std::string, int, int, bool);
55
59
60int main(int argc, char* argv[])
61{
62
63 int i;
64 bool verbose = 0;
65 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
66
67 if (verbose)
68 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
69
70 int numberFailedTests = 0;
71 int returnCode = 0;
72 std::string testName = "";
73
74 if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL BANDED DENSE MATRIX **********"<<std::endl<<std::endl;
75
76 // default constructor test
77 BDMatrix DefConTest;
78 if (verbose) std::cout <<"default constructor -- construct empty matrix ";
79 if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) {
80 if (verbose) std::cout << "unsuccessful."<<std::endl;
81 numberFailedTests++;
82 } else {
83 if (verbose) std::cout << "successful."<<std::endl;
84 }
85
86 // constructor 1 (matrix w/ dimension but empty)
87
88 BDMatrix Con1Test( 4, 4, 1, 1 );
89 if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions ";
90 if ( Con1Test.numRows()!=4 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) {
91 if (verbose) std::cout << "unsuccessful."<<std::endl;
92 numberFailedTests++;
93 } else {
94 if (verbose) std::cout << "successful."<<std::endl;
95 }
96
97 // constructor 2 (from array) tests
98
99 STYPE a[25];
100 a[4] = 5; a[8] = 11; a[12] = 17; a[16] = 23;
101 a[1] = 0; a[5] = 6; a[9] = 12; a[13] = 18; a[17] = 24;
102 a[2] = 1; a[6] = 7; a[10] = 13; a[14] = 19;
103 a[3] = 2; a[7] = 8; a[11] = 14;
104
105 BDMatrix C2T1ER;
106 C2T1ER.shape(5, 5, 2, 1);
107 C2T1ER(0, 0) = 0; C2T1ER(0, 1) = 5;
108 C2T1ER(1, 0) = 1; C2T1ER(1, 1) = 6; C2T1ER(1, 2) = 11;
109 C2T1ER(2, 0) = 2; C2T1ER(2, 1) = 7; C2T1ER(2, 2) = 12; C2T1ER(2, 3) = 17;
110 C2T1ER(3, 1) = 8; C2T1ER(3, 2) = 13; C2T1ER(3, 3) = 18; C2T1ER(3, 4) = 23;
111 C2T1ER(4, 2) = 14; C2T1ER(4, 3) = 19; C2T1ER(4, 4) = 24;
112
113 // Create another lower triangular matrix with a view of 'a'.
114 BDMatrix Con2Test1(Teuchos::Copy, a, 4, 5, 5, 2, 1);
115 numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, C2T1ER, verbose);
116
117 // constructor 3 (copy constructor)
118
119 BDMatrix Con3TestCopy( C2T1ER );
120 if(verbose) std::cout <<"constructor 3 -- copy constructor ";
121 if ( Con3TestCopy != C2T1ER ) {
122 if (verbose) std::cout << "unsuccessful."<<std::endl;
123 numberFailedTests++;
124 } else {
125 if (verbose) std::cout << "successful."<<std::endl;
126 }
127
128 BDMatrix Con3TestCopyTrans( C2T1ER, Teuchos::TRANS );
129 if(verbose) std::cout <<"constructor 3 -- copy constructor (transposed) ";
130 if ( Con3TestCopyTrans(0, 2) != C2T1ER(2, 0) ) {
131 if (verbose) std::cout << "unsuccessful."<<std::endl;
132 numberFailedTests++;
133 } else {
134 if (verbose) std::cout << "successful."<<std::endl;
135 }
136
137 // constructor 4 (submatrix)
138
139 BDMatrix Con4TestOrig(Teuchos::Copy, a, 4, 5, 5, 2, 1);
140 BDMatrix C4TS;
141 C4TS.shape( 3, 3, 2, 1 );
142 C4TS(0, 0) = 12; C4TS(0, 1) = 17;
143 C4TS(1, 0) = 13; C4TS(1, 1) = 18; C4TS(1, 2) = 23;
144 C4TS(2, 0) = 14; C4TS(2, 1) = 19; C4TS(2, 2) = 24;
145
146 BDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 3, 3, 2);
147 numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, C4TS, verbose);
148 BDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 5, 5, 0);
149 numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose);
150 BDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 5, 5, 0);
151 numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestOrig, verbose);
152 BDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 3, 2);
153 numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, C4TS, verbose);
154
155 // Norm Tests
156
157 BDMatrix AAA;
158 AAA.shape( 5, 5, 2, 1 );
159 AAA(0, 0) = 0; AAA(0, 1) = 5;
160 AAA(1, 0) = 1; AAA(1, 1) = 6; AAA(1, 2) = 11;
161 AAA(2, 0) = 2; AAA(2, 1) = 7; AAA(2, 2) = 12; AAA(2, 3) = 17;
162 AAA(3, 1) = 8; AAA(3, 2) = 13; AAA(3, 3) = 18; AAA(3, 4) = 23;
163 AAA(4, 2) = 14; AAA(4, 3) = 19; AAA(4, 4) = 24;
164
165 BDMatrix BBB;
166 numberFailedTests += PrintTestResults("normOne of a 5x5", AAA.normOne(), 54.0, verbose);
167 numberFailedTests += PrintTestResults("normInf of a 5x5", AAA.normInf(), 62.0, verbose);
169 numberFailedTests += PrintTestResults("normFrobenius of a 5x5", AAA.normFrobenius(), 4.0, verbose);
170 numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose);
171 numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose);
172 numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose);
173
174 // Set Method Tests.
175
176 BDMatrix CCC( 5, 5, 2, 1 );
177 // Randomize the entries in CCC.
178 testName = "random() -- enter random entries into matrix";
179 returnCode = CCC.random();
180 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
181 // Set the entries of CCC to 1.0.
182 testName = "putScalar() -- set every entry of this matrix to 1.0";
184 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
185 // Check assignment operator.
186 BDMatrix CCC2( 5, 5, 2, 1 );
187 CCC2.assign( CCC );
188 if (verbose) std::cout << "assign() -- copy the values of an input matrix ";
189 if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) {
190 if (verbose) std::cout<< "successful" <<std::endl;
191 } else {
192 if (verbose) std::cout<< "unsuccessful" <<std::endl;
193 numberFailedTests++;
194 }
195 // Create a view into a submatrix of CCC
196 BDMatrix CCCview( Teuchos::View, CCC, 3, 3 );
197 BDMatrix CCCtest1( 3, 3, 2, 1 );
198 CCCtest1 = CCCview;
199 if (verbose) std::cout << "operator= -- small(empty) = large(view) ";
200 if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) {
201 if (verbose) std::cout<< "successful" <<std::endl;
202 } else {
203 if (verbose) std::cout<< "unsuccessful" <<std::endl;
204 numberFailedTests++;
205 }
206 CCCtest1 = CCC;
207 if (verbose) std::cout << "operator= -- small(view) = large(copy) ";
208 if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) {
209 if (verbose) std::cout<< "successful"<<std::endl;
210 } else {
211 if (verbose) std::cout<< "unsuccessful"<<std::endl;
212 numberFailedTests++;
213 }
214 BDMatrix CCCtest2( 3, 3, 2, 1 );
216 CCCtest1 = CCCtest2;
217 if (verbose) std::cout << "operator= -- large(copy) = small(copy) ";
218 if (CCCtest1.numRows()==3 ) {
219 if (verbose) std::cout<< "successful"<<std::endl;
220 } else {
221 if (verbose) std::cout<< "unsuccessful"<<std::endl;
222 numberFailedTests++;
223 }
224 CCCtest1 = CCCview;
225 if (verbose) std::cout << "operator= -- large(copy) = small(view) ";
226 if (CCCtest1.numRows()==3 && CCCtest1.stride()==4) {
227 if(verbose) std::cout<<"successful" <<std::endl;
228 } else {
229 if (verbose) std::cout<<"unsuccessful"<<std::endl;
230 numberFailedTests++;
231 }
232 BDMatrix CCCtest3( CCCview );
233 CCCtest1 += CCCtest3;
234 if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension ";
235 if (CCCtest1(1,1)==2.0) {
236 if(verbose) std::cout<<"successful" <<std::endl;
237 } else {
238 if (verbose) std::cout<<"unsuccessful"<<std::endl;
239 numberFailedTests++;
240 }
241 if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) ";
242 CCCtest1 += CCC;
243 if (CCCtest1(1,1)==2.0) {
244 if(verbose) std::cout<<"successful" <<std::endl;
245 } else {
246 if (verbose) std::cout<<"unsuccessful"<<std::endl;
247 numberFailedTests++;
248 }
249
250 // Scale Tests.
251
252 BDMatrix ScalTest( 8, 8, 2, 3 );
254 // Scale the entries by 8, it should be 8.
255 // The matrix is lower triangular, by default, so check a lower triangular entry.
256 if (verbose) std::cout << "operator*= -- scale matrix by some number ";
257 ScalTest *= 8.0;
258 if (ScalTest(5, 7) == 8.0) {
259 if (verbose) std::cout<< "successful." <<std::endl;
260 } else {
261 if (verbose) std::cout<< "unsuccessful." <<std::endl;
262 numberFailedTests++;
263 }
264
265
266 //
267 // If a test failed output the number of failed tests.
268 //
269 if(numberFailedTests > 0)
270 {
271 if (verbose) {
272 std::cout << "Number of failed tests: " << numberFailedTests << std::endl;
273 std::cout << "End Result: TEST FAILED" << std::endl;
274 return -1;
275 }
276 }
277 if(numberFailedTests == 0)
278 std::cout << "End Result: TEST PASSED" << std::endl;
279
280 return 0;
281}
282
283template<typename TYPE>
284int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose)
285{
286 int result;
287 if(calculatedResult == expectedResult)
288 {
289 if(verbose) std::cout << testName << " successful." << std::endl;
290 result = 0;
291 }
292 else
293 {
294 if(verbose) std::cout << testName << " unsuccessful." << std::endl;
295 result = 1;
296 }
297 return result;
298}
299
300int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose)
301{
302 int result;
303 if(expectedResult == 0)
304 {
305 if(returnCode == 0)
306 {
307 if(verbose) std::cout << testName << " test successful." << std::endl;
308 result = 0;
309 }
310 else
311 {
312 if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl;
313 result = 1;
314 }
315 }
316 else
317 {
318 if(returnCode != 0)
319 {
320 if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl;
321 result = 0;
322 }
323 else
324 {
325 if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl;
326 result = 1;
327 }
328 }
329 return result;
330}
Templated serial dense matrix class.
Non-member helper functions on the templated serial, dense matrix/vector classes.
Templated serial dense matrix class.
Templated serial dense vector class.
This class creates and provides basic support for banded dense matrices of templated type.
int random()
Set all values in the matrix to be random numbers.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
bool empty() const
Returns whether this matrix is empty.
ScalarType * values() const
Data array access method.
OrdinalType numRows() const
Returns the row dimension of this matrix.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
SerialBandDenseMatrix< OrdinalType, ScalarType > & assign(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
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.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
This class creates and provides basic support for dense rectangular matrix of templated type.
This class creates and provides basic support for dense vectors of templated type as a specialization...
Teuchos::SerialBandDenseMatrix< OTYPE, STYPE > BDMatrix
Teuchos::SerialDenseVector< OTYPE, STYPE > DVector
#define STYPE
int PrintTestResults(std::string, TYPE, TYPE, bool)
Teuchos::SerialDenseMatrix< OTYPE, STYPE > DMatrix
int ReturnCodeCheck(std::string, int, int, bool)
int main()
Definition: evilMain.cpp:75
std::string Teuchos_Version()
This structure defines some basic traits for a scalar field type.
static T one()
Returns representation of one for this scalar type.