|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #include "Teuchos_SerialSymDenseMatrix.hpp" 00030 #include "Teuchos_SerialDenseMatrix.hpp" 00031 #include "Teuchos_SerialDenseVector.hpp" 00032 #include "Teuchos_SerialDenseHelpers.hpp" 00033 #include "Teuchos_Version.hpp" 00034 00035 #define OTYPE int 00036 #define STYPE std::complex<double> 00037 00038 template<typename TYPE> 00039 int PrintTestResults(std::string, TYPE, TYPE, bool); 00040 00041 int ReturnCodeCheck(std::string, int, int, bool); 00042 00043 typedef Teuchos::SerialSymDenseMatrix<OTYPE, STYPE> SDMatrix; 00044 typedef Teuchos::SerialDenseMatrix<OTYPE, STYPE> DMatrix; 00045 typedef Teuchos::SerialDenseVector<OTYPE, STYPE> DVector; 00046 00047 int main(int argc, char* argv[]) 00048 { 00049 00050 int i; 00051 bool verbose = 0; 00052 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; 00053 00054 if (verbose) 00055 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00056 00057 int numberFailedTests = 0; 00058 int returnCode = 0; 00059 std::string testName = ""; 00060 00061 if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL SYMMETRIC DENSE MATRIX **********"<<std::endl<<std::endl; 00062 00063 // default constructor test 00064 SDMatrix DefConTest; 00065 if (verbose) std::cout <<"default constructor -- construct empty matrix "; 00066 if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) { 00067 if (verbose) std::cout << "unsuccessful."<<std::endl; 00068 numberFailedTests++; 00069 } else { 00070 if (verbose) std::cout << "successful."<<std::endl; 00071 } 00072 00073 // constructor 1 (matrix w/ dimension but empty) 00074 00075 SDMatrix Con1Test( 4 ); 00076 if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions "; 00077 if ( Con1Test.numRows()!=4 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) { 00078 if (verbose) std::cout << "unsuccessful."<<std::endl; 00079 numberFailedTests++; 00080 } else { 00081 if (verbose) std::cout << "successful."<<std::endl; 00082 } 00083 00084 // constructor 2 (from array) tests 00085 00086 STYPE a[9]; 00087 for(i = 0; i < 9; i++) 00088 { 00089 a[i] = i; 00090 } 00091 SDMatrix Con2Test1ExpRes; 00092 Con2Test1ExpRes.shape(3); 00093 Con2Test1ExpRes(0, 0) = 0; 00094 Con2Test1ExpRes(1, 0) = 1; Con2Test1ExpRes(1, 1) = 4; 00095 Con2Test1ExpRes(2, 0) = 2; Con2Test1ExpRes(2, 1) = 5; Con2Test1ExpRes(2, 2) = 8; 00096 00097 // Create another lower triangular matrix with a view of 'a'. 00098 SDMatrix Con2Test1(Teuchos::View, false, a, 3, 3); 00099 numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, Con2Test1ExpRes, verbose); 00100 00101 00102 // constructor 3 (copy constructor) 00103 00104 SDMatrix Con3TestCopy( Con2Test1ExpRes ); 00105 if(verbose) std::cout <<"constructor 3 -- copy constructor "; 00106 if ( Con3TestCopy != Con2Test1ExpRes ) { 00107 if (verbose) std::cout << "unsuccessful."<<std::endl; 00108 numberFailedTests++; 00109 } else { 00110 if (verbose) std::cout << "successful."<<std::endl; 00111 } 00112 00113 SDMatrix Con3TestCopyTrans( Con2Test1ExpRes ); 00114 Con3TestCopyTrans.setUpper(); 00115 if(verbose) std::cout <<"constructor 3 -- copy constructor (upper active storage) "; 00116 if ( Con3TestCopyTrans(2, 0) != Con2Test1ExpRes(2, 0) ) { 00117 if (verbose) std::cout << "unsuccessful."<<std::endl; 00118 numberFailedTests++; 00119 } else { 00120 if (verbose) std::cout << "successful."<<std::endl; 00121 } 00122 00123 // constructor 4 (submatrix) 00124 00125 SDMatrix Con4TestOrig(Teuchos::Copy, false, a, 3, 3); 00126 SDMatrix Con4TestSubmatrix; 00127 Con4TestSubmatrix.shape( 2 ); 00128 Con4TestSubmatrix(0, 0) = 4; 00129 Con4TestSubmatrix(1, 0) = 5; Con4TestSubmatrix(1, 1) = 8; 00130 SDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 2, 1); 00131 numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, Con4TestSubmatrix, verbose); 00132 SDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 3, 0); 00133 numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose); 00134 SDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 2, 1); 00135 numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestSubmatrix, verbose); 00136 SDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 0); 00137 numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, Con4TestOrig, verbose); 00138 00139 // Norm Tests 00140 00141 SDMatrix AAA; 00142 AAA.shape( 3 ); 00143 AAA(0, 0) = 8; 00144 AAA(1, 0) = 1; AAA(1, 1) = 8; 00145 AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; 00146 SDMatrix BBB; 00147 numberFailedTests += PrintTestResults("normOne of a 3x3", AAA.normOne(), 13.0, verbose); 00148 numberFailedTests += PrintTestResults("normInf of a 3x3", AAA.normInf(), 13.0, verbose); 00149 AAA = Teuchos::ScalarTraits<STYPE>::one(); 00150 numberFailedTests += PrintTestResults("normFrobenius of a 3x3", AAA.normFrobenius(), 3.0, verbose); 00151 numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose); 00152 numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose); 00153 numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose); 00154 00155 // Multiplication Tests 00156 00157 // Reset values of AAA. 00158 AAA(0, 0) = 8; 00159 AAA(1, 0) = 1; AAA(1, 1) = 8; 00160 AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; 00161 00162 DMatrix My_Prod( 4, 3 ), My_GenMatrix( 4, 3 ); 00163 My_GenMatrix = Teuchos::ScalarTraits<STYPE>::one(); 00164 00165 // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix ) 00166 My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); 00167 numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = lower tri)", My_Prod.normOne(), 52.0, verbose); 00168 AAA.setUpper(); 00169 AAA(2, 1) = 1.0; 00170 My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); 00171 numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = upper tri)", My_Prod.normOne(), 44.0, verbose); 00172 00173 // Set Method Tests. 00174 00175 SDMatrix CCC( 5 ); 00176 // Randomize the entries in CCC. 00177 testName = "random() -- enter random entries into matrix"; 00178 returnCode = CCC.random(); 00179 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00180 // Set the entries of CCC to 1.0. 00181 testName = "putScalar() -- set every entry of this matrix to 1.0"; 00182 returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one()); 00183 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00184 // Check assignment operator. 00185 SDMatrix CCC2( 5 ); 00186 CCC2.assign( CCC ); 00187 if (verbose) std::cout << "assign() -- copy the values of an input matrix "; 00188 if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) { 00189 if (verbose) std::cout<< "successful" <<std::endl; 00190 } else { 00191 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00192 numberFailedTests++; 00193 } 00194 // Create a view into a submatrix of CCC 00195 SDMatrix CCCview( Teuchos::View, CCC, 3 ); 00196 SDMatrix CCCtest1( 2 ); 00197 CCCtest1 = CCCview; 00198 if (verbose) std::cout << "operator= -- small(empty) = large(view) "; 00199 if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) { 00200 if (verbose) std::cout<< "successful" <<std::endl; 00201 } else { 00202 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00203 numberFailedTests++; 00204 } 00205 CCCtest1 = CCC; 00206 if (verbose) std::cout << "operator= -- small(view) = large(copy) "; 00207 if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) { 00208 if (verbose) std::cout<< "successful"<<std::endl; 00209 } else { 00210 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00211 numberFailedTests++; 00212 } 00213 SDMatrix CCCtest2( 2 ); 00214 CCCtest2 = Teuchos::ScalarTraits<STYPE>::one(); 00215 CCCtest1 = CCCtest2; 00216 if (verbose) std::cout << "operator= -- large(copy) = small(copy) "; 00217 if (CCCtest1.numRows()==2 ) { 00218 if (verbose) std::cout<< "successful"<<std::endl; 00219 } else { 00220 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00221 numberFailedTests++; 00222 } 00223 CCCtest1 = CCCview; 00224 if (verbose) std::cout << "operator= -- large(copy) = small(view) "; 00225 if (CCCtest1.numRows()==3 && CCCtest1.stride()==5) { 00226 if(verbose) std::cout<<"successful" <<std::endl; 00227 } else { 00228 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00229 numberFailedTests++; 00230 } 00231 00232 SDMatrix CCCtest3( CCCview ); 00233 CCCtest1 += CCCtest3; 00234 if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension "; 00235 if (CCCtest1(1,1)==2.0) { 00236 if(verbose) std::cout<<"successful" <<std::endl; 00237 } else { 00238 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00239 numberFailedTests++; 00240 } 00241 if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) "; 00242 CCCtest1 += CCC; 00243 if (CCCtest1(1,1)==2.0) { 00244 if(verbose) std::cout<<"successful" <<std::endl; 00245 } else { 00246 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00247 numberFailedTests++; 00248 } 00249 00250 // Scale Tests. 00251 00252 SDMatrix ScalTest( 8 ); 00253 ScalTest = Teuchos::ScalarTraits<STYPE>::one(); 00254 // Scale the entries by 8, it should be 8. 00255 if (verbose) std::cout << "operator*= -- scale matrix by some number "; 00256 ScalTest *= 8.0; 00257 if (ScalTest(2, 3) == 8.0) { 00258 if (verbose) std::cout<< "successful." <<std::endl; 00259 } else { 00260 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00261 numberFailedTests++; 00262 } 00263 00264 00265 // Matrix Triple-Product Test 00266 STYPE alpha=0.5*Teuchos::ScalarTraits<STYPE>::one(); 00267 DMatrix W(3,2); 00268 SDMatrix A1(2), A2(3); 00269 A1(0,0) = 1.0, A1(1,1) = 2.0; 00270 A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00; 00271 W = Teuchos::ScalarTraits<STYPE>::one(); 00272 00273 SDMatrix C1upper(3), C1lower(3), C2upper(2), C2lower(2); 00274 C1upper.setUpper(); C2upper.setUpper(); 00275 C1lower.setLower(); C2lower.setLower(); 00276 00277 // Test all combinations of triple products. 00278 00279 // These should return a matrix with 1.5 in all entries 00280 STYPE C1result = 1.5*Teuchos::ScalarTraits<STYPE>::one(); 00281 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1upper ); 00282 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1lower ); 00283 00284 // These should return a matrix with 3 in all entries 00285 STYPE C2result = 3.0*Teuchos::ScalarTraits<STYPE>::one(); 00286 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2upper ); 00287 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2lower ); 00288 00289 if (verbose) std::cout << "triple product -- compute C = W'*A*W or C = W*A*W' "; 00290 if (C1upper(2,1)==C1result && C1lower(1,2)==C1result && C2upper(1,0)==C2result && C2lower(0,1)==C2result) { 00291 if (verbose) std::cout<< "successful." <<std::endl; 00292 } else { 00293 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00294 numberFailedTests++; 00295 } 00296 00297 00298 00299 // 00300 // If a test failed output the number of failed tests. 00301 // 00302 if(numberFailedTests > 0) 00303 { 00304 if (verbose) { 00305 std::cout << "Number of failed tests: " << numberFailedTests << std::endl; 00306 std::cout << "End Result: TEST FAILED" << std::endl; 00307 return -1; 00308 } 00309 } 00310 if(numberFailedTests == 0) 00311 std::cout << "End Result: TEST PASSED" << std::endl; 00312 00313 return 0; 00314 } 00315 00316 template<typename TYPE> 00317 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose) 00318 { 00319 int result; 00320 if(calculatedResult == expectedResult) 00321 { 00322 if(verbose) std::cout << testName << " successful." << std::endl; 00323 result = 0; 00324 } 00325 else 00326 { 00327 if(verbose) std::cout << testName << " unsuccessful." << std::endl; 00328 result = 1; 00329 } 00330 return result; 00331 } 00332 00333 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose) 00334 { 00335 int result; 00336 if(expectedResult == 0) 00337 { 00338 if(returnCode == 0) 00339 { 00340 if(verbose) std::cout << testName << " test successful." << std::endl; 00341 result = 0; 00342 } 00343 else 00344 { 00345 if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl; 00346 result = 1; 00347 } 00348 } 00349 else 00350 { 00351 if(returnCode != 0) 00352 { 00353 if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl; 00354 result = 0; 00355 } 00356 else 00357 { 00358 if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl; 00359 result = 1; 00360 } 00361 } 00362 return result; 00363 }
1.7.4