|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #include "Teuchos_SerialSymDenseMatrix.hpp" 00043 #include "Teuchos_SerialDenseMatrix.hpp" 00044 #include "Teuchos_SerialDenseVector.hpp" 00045 #include "Teuchos_SerialDenseHelpers.hpp" 00046 #include "Teuchos_Version.hpp" 00047 00048 #define OTYPE int 00049 #define STYPE std::complex<double> 00050 00051 template<typename TYPE> 00052 int PrintTestResults(std::string, TYPE, TYPE, bool); 00053 00054 int ReturnCodeCheck(std::string, int, int, bool); 00055 00056 typedef Teuchos::SerialSymDenseMatrix<OTYPE, STYPE> SDMatrix; 00057 typedef Teuchos::SerialDenseMatrix<OTYPE, STYPE> DMatrix; 00058 typedef Teuchos::SerialDenseVector<OTYPE, STYPE> DVector; 00059 00060 int main(int argc, char* argv[]) 00061 { 00062 00063 int i; 00064 bool verbose = 0; 00065 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; 00066 00067 if (verbose) 00068 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00069 00070 int numberFailedTests = 0; 00071 int returnCode = 0; 00072 std::string testName = ""; 00073 00074 if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL SYMMETRIC DENSE MATRIX **********"<<std::endl<<std::endl; 00075 00076 // default constructor test 00077 SDMatrix DefConTest; 00078 if (verbose) std::cout <<"default constructor -- construct empty matrix "; 00079 if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) { 00080 if (verbose) std::cout << "unsuccessful."<<std::endl; 00081 numberFailedTests++; 00082 } else { 00083 if (verbose) std::cout << "successful."<<std::endl; 00084 } 00085 00086 // constructor 1 (matrix w/ dimension but empty) 00087 00088 SDMatrix Con1Test( 4 ); 00089 if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions "; 00090 if ( Con1Test.numRows()!=4 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) { 00091 if (verbose) std::cout << "unsuccessful."<<std::endl; 00092 numberFailedTests++; 00093 } else { 00094 if (verbose) std::cout << "successful."<<std::endl; 00095 } 00096 00097 // constructor 2 (from array) tests 00098 00099 STYPE a[9]; 00100 for(i = 0; i < 9; i++) 00101 { 00102 a[i] = i; 00103 } 00104 SDMatrix Con2Test1ExpRes; 00105 Con2Test1ExpRes.shape(3); 00106 Con2Test1ExpRes(0, 0) = 0; 00107 Con2Test1ExpRes(1, 0) = 1; Con2Test1ExpRes(1, 1) = 4; 00108 Con2Test1ExpRes(2, 0) = 2; Con2Test1ExpRes(2, 1) = 5; Con2Test1ExpRes(2, 2) = 8; 00109 00110 // Create another lower triangular matrix with a view of 'a'. 00111 SDMatrix Con2Test1(Teuchos::View, false, a, 3, 3); 00112 numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, Con2Test1ExpRes, verbose); 00113 00114 00115 // constructor 3 (copy constructor) 00116 00117 SDMatrix Con3TestCopy( Con2Test1ExpRes ); 00118 if(verbose) std::cout <<"constructor 3 -- copy constructor "; 00119 if ( Con3TestCopy != Con2Test1ExpRes ) { 00120 if (verbose) std::cout << "unsuccessful."<<std::endl; 00121 numberFailedTests++; 00122 } else { 00123 if (verbose) std::cout << "successful."<<std::endl; 00124 } 00125 00126 SDMatrix Con3TestCopyTrans( Con2Test1ExpRes ); 00127 Con3TestCopyTrans.setUpper(); 00128 if(verbose) std::cout <<"constructor 3 -- copy constructor (upper active storage) "; 00129 if ( Con3TestCopyTrans(2, 0) != Con2Test1ExpRes(2, 0) ) { 00130 if (verbose) std::cout << "unsuccessful."<<std::endl; 00131 numberFailedTests++; 00132 } else { 00133 if (verbose) std::cout << "successful."<<std::endl; 00134 } 00135 00136 // constructor 4 (submatrix) 00137 00138 SDMatrix Con4TestOrig(Teuchos::Copy, false, a, 3, 3); 00139 SDMatrix Con4TestSubmatrix; 00140 Con4TestSubmatrix.shape( 2 ); 00141 Con4TestSubmatrix(0, 0) = 4; 00142 Con4TestSubmatrix(1, 0) = 5; Con4TestSubmatrix(1, 1) = 8; 00143 SDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 2, 1); 00144 numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, Con4TestSubmatrix, verbose); 00145 SDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 3, 0); 00146 numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose); 00147 SDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 2, 1); 00148 numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestSubmatrix, verbose); 00149 SDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 0); 00150 numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, Con4TestOrig, verbose); 00151 00152 // Norm Tests 00153 00154 SDMatrix AAA; 00155 AAA.shape( 3 ); 00156 AAA(0, 0) = 8; 00157 AAA(1, 0) = 1; AAA(1, 1) = 8; 00158 AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; 00159 SDMatrix BBB; 00160 numberFailedTests += PrintTestResults("normOne of a 3x3", AAA.normOne(), 13.0, verbose); 00161 numberFailedTests += PrintTestResults("normInf of a 3x3", AAA.normInf(), 13.0, verbose); 00162 AAA = Teuchos::ScalarTraits<STYPE>::one(); 00163 numberFailedTests += PrintTestResults("normFrobenius of a 3x3", AAA.normFrobenius(), 3.0, verbose); 00164 numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose); 00165 numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose); 00166 numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose); 00167 00168 // Multiplication Tests 00169 00170 // Reset values of AAA. 00171 AAA(0, 0) = 8; 00172 AAA(1, 0) = 1; AAA(1, 1) = 8; 00173 AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; 00174 00175 DMatrix My_Prod( 4, 3 ), My_GenMatrix( 4, 3 ); 00176 My_GenMatrix = Teuchos::ScalarTraits<STYPE>::one(); 00177 00178 // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix ) 00179 My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); 00180 numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = lower tri)", My_Prod.normOne(), 52.0, verbose); 00181 AAA.setUpper(); 00182 AAA(2, 1) = 1.0; 00183 My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); 00184 numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = upper tri)", My_Prod.normOne(), 44.0, verbose); 00185 00186 // Set Method Tests. 00187 00188 SDMatrix CCC( 5 ); 00189 // Randomize the entries in CCC. 00190 testName = "random() -- enter random entries into matrix"; 00191 returnCode = CCC.random(); 00192 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00193 // Set the entries of CCC to 1.0. 00194 testName = "putScalar() -- set every entry of this matrix to 1.0"; 00195 returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one()); 00196 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00197 // Check assignment operator. 00198 SDMatrix CCC2( 5 ); 00199 CCC2.assign( CCC ); 00200 if (verbose) std::cout << "assign() -- copy the values of an input matrix "; 00201 if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) { 00202 if (verbose) std::cout<< "successful" <<std::endl; 00203 } else { 00204 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00205 numberFailedTests++; 00206 } 00207 // Create a view into a submatrix of CCC 00208 SDMatrix CCCview( Teuchos::View, CCC, 3 ); 00209 SDMatrix CCCtest1( 2 ); 00210 CCCtest1 = CCCview; 00211 if (verbose) std::cout << "operator= -- small(empty) = large(view) "; 00212 if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) { 00213 if (verbose) std::cout<< "successful" <<std::endl; 00214 } else { 00215 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00216 numberFailedTests++; 00217 } 00218 CCCtest1 = CCC; 00219 if (verbose) std::cout << "operator= -- small(view) = large(copy) "; 00220 if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) { 00221 if (verbose) std::cout<< "successful"<<std::endl; 00222 } else { 00223 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00224 numberFailedTests++; 00225 } 00226 SDMatrix CCCtest2( 2 ); 00227 CCCtest2 = Teuchos::ScalarTraits<STYPE>::one(); 00228 CCCtest1 = CCCtest2; 00229 if (verbose) std::cout << "operator= -- large(copy) = small(copy) "; 00230 if (CCCtest1.numRows()==2 ) { 00231 if (verbose) std::cout<< "successful"<<std::endl; 00232 } else { 00233 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00234 numberFailedTests++; 00235 } 00236 CCCtest1 = CCCview; 00237 if (verbose) std::cout << "operator= -- large(copy) = small(view) "; 00238 if (CCCtest1.numRows()==3 && CCCtest1.stride()==5) { 00239 if(verbose) std::cout<<"successful" <<std::endl; 00240 } else { 00241 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00242 numberFailedTests++; 00243 } 00244 00245 SDMatrix CCCtest3( CCCview ); 00246 CCCtest1 += CCCtest3; 00247 if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension "; 00248 if (CCCtest1(1,1)==2.0) { 00249 if(verbose) std::cout<<"successful" <<std::endl; 00250 } else { 00251 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00252 numberFailedTests++; 00253 } 00254 if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) "; 00255 CCCtest1 += CCC; 00256 if (CCCtest1(1,1)==2.0) { 00257 if(verbose) std::cout<<"successful" <<std::endl; 00258 } else { 00259 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00260 numberFailedTests++; 00261 } 00262 00263 // Scale Tests. 00264 00265 SDMatrix ScalTest( 8 ); 00266 ScalTest = Teuchos::ScalarTraits<STYPE>::one(); 00267 // Scale the entries by 8, it should be 8. 00268 // The matrix is lower triangular, by default, so check a lower triangular entry. 00269 if (verbose) std::cout << "operator*= -- scale matrix by some number "; 00270 ScalTest *= 8.0; 00271 if (ScalTest(7, 1) == 8.0) { 00272 if (verbose) std::cout<< "successful." <<std::endl; 00273 } else { 00274 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00275 numberFailedTests++; 00276 } 00277 00278 00279 // Matrix Triple-Product Test 00280 STYPE alpha=0.5*Teuchos::ScalarTraits<STYPE>::one(); 00281 DMatrix W(3,2); 00282 SDMatrix A1(2), A2(3); 00283 A1(0,0) = 1.0, A1(1,1) = 2.0; 00284 A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00; 00285 W = Teuchos::ScalarTraits<STYPE>::one(); 00286 00287 SDMatrix C1upper(3), C1lower(3), C2upper(2), C2lower(2); 00288 C1upper.setUpper(); C2upper.setUpper(); 00289 C1lower.setLower(); C2lower.setLower(); 00290 00291 // Test all combinations of triple products. 00292 00293 // These should return a matrix with 1.5 in all entries 00294 STYPE C1result = 1.5*Teuchos::ScalarTraits<STYPE>::one(); 00295 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1upper ); 00296 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1lower ); 00297 00298 // These should return a matrix with 3 in all entries 00299 STYPE C2result = 3.0*Teuchos::ScalarTraits<STYPE>::one(); 00300 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2upper ); 00301 Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2lower ); 00302 00303 if (verbose) std::cout << "triple product -- compute C = W'*A*W or C = W*A*W' "; 00304 if (C1upper(2,1)==C1result && C1lower(1,2)==C1result && C2upper(1,0)==C2result && C2lower(0,1)==C2result) { 00305 if (verbose) std::cout<< "successful." <<std::endl; 00306 } else { 00307 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00308 numberFailedTests++; 00309 } 00310 00311 00312 00313 // 00314 // If a test failed output the number of failed tests. 00315 // 00316 if(numberFailedTests > 0) 00317 { 00318 if (verbose) { 00319 std::cout << "Number of failed tests: " << numberFailedTests << std::endl; 00320 std::cout << "End Result: TEST FAILED" << std::endl; 00321 return -1; 00322 } 00323 } 00324 if(numberFailedTests == 0) 00325 std::cout << "End Result: TEST PASSED" << std::endl; 00326 00327 return 0; 00328 } 00329 00330 template<typename TYPE> 00331 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose) 00332 { 00333 int result; 00334 if(calculatedResult == expectedResult) 00335 { 00336 if(verbose) std::cout << testName << " successful." << std::endl; 00337 result = 0; 00338 } 00339 else 00340 { 00341 if(verbose) std::cout << testName << " unsuccessful." << std::endl; 00342 result = 1; 00343 } 00344 return result; 00345 } 00346 00347 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose) 00348 { 00349 int result; 00350 if(expectedResult == 0) 00351 { 00352 if(returnCode == 0) 00353 { 00354 if(verbose) std::cout << testName << " test successful." << std::endl; 00355 result = 0; 00356 } 00357 else 00358 { 00359 if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl; 00360 result = 1; 00361 } 00362 } 00363 else 00364 { 00365 if(returnCode != 0) 00366 { 00367 if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl; 00368 result = 0; 00369 } 00370 else 00371 { 00372 if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl; 00373 result = 1; 00374 } 00375 } 00376 return result; 00377 }
1.7.6.1