|
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_SerialDenseMatrix.hpp" 00030 #include "Teuchos_SerialDenseVector.hpp" 00031 #include "Teuchos_SerialDenseHelpers.hpp" 00032 #include "Teuchos_Version.hpp" 00033 00034 #define OTYPE int 00035 #define STYPE std::complex<double> 00036 00037 template<typename TYPE> 00038 int PrintTestResults(std::string, TYPE, TYPE, bool); 00039 00040 int ReturnCodeCheck(std::string, int, int, bool); 00041 00042 typedef double Real; 00043 typedef Teuchos::SerialDenseVector<int, std::complex<Real> > DVector; 00044 typedef Teuchos::SerialDenseMatrix<int, std::complex<Real> > DMatrix; 00045 //typedef Teuchos::SerialDenseVector<OTYPE, STYPE> DVector; 00046 00047 int main(int argc, char* argv[]) 00048 { 00049 00050 int i, j; 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 00062 00063 if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL DENSE MATRIX **********"<<std::endl<<std::endl; 00064 00065 // default constructor test 00066 DMatrix DefConTest; 00067 if (verbose) std::cout <<"default constructor -- construct empty matrix "; 00068 if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) { 00069 if (verbose) std::cout << "unsuccessful."<<std::endl; 00070 numberFailedTests++; 00071 } else { 00072 if (verbose) std::cout << "successful."<<std::endl; 00073 } 00074 00075 // constructor 1 (matrix w/ dimension but empty) 00076 00077 DMatrix Con1Test( 3, 4 ); 00078 if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions "; 00079 if ( Con1Test.numRows()!=3 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) { 00080 if (verbose) std::cout << "unsuccessful."<<std::endl; 00081 numberFailedTests++; 00082 } else { 00083 if (verbose) std::cout << "successful."<<std::endl; 00084 } 00085 00086 // constructor 2 (from array) tests 00087 00088 STYPE a[9]; 00089 for(i = 0; i < 9; i++) 00090 { 00091 a[i] = i; 00092 } 00093 DMatrix Con2Test1ExpRes; 00094 Con2Test1ExpRes.shape(2, 3); 00095 Con2Test1ExpRes(0, 0) = 0; Con2Test1ExpRes(0, 1) = 2; Con2Test1ExpRes(0, 2) = 4; 00096 Con2Test1ExpRes(1, 0) = 1; Con2Test1ExpRes(1, 1) = 3; Con2Test1ExpRes(1, 2) = 5; 00097 00098 DMatrix Con2Test1(Teuchos::Copy, a, 2, 2, 3); 00099 numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, Con2Test1ExpRes, verbose); 00100 00101 00102 // constructor 3 (copy constructor) 00103 00104 DMatrix 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 DMatrix Con3TestCopyTrans( Con2Test1ExpRes, Teuchos::TRANS ); 00114 if(verbose) std::cout <<"constructor 3 -- copy constructor (transposed) "; 00115 if ( Con3TestCopyTrans(2, 0) != Con2Test1ExpRes(0, 2) ) { 00116 if (verbose) std::cout << "unsuccessful."<<std::endl; 00117 numberFailedTests++; 00118 } else { 00119 if (verbose) std::cout << "successful."<<std::endl; 00120 } 00121 00122 // constructor 4 (submatrix) 00123 00124 DMatrix Con4TestOrig(Teuchos::Copy, a, 3, 3, 3); 00125 DMatrix Con4TestSubmatrix; 00126 Con4TestSubmatrix.shape(2, 2); 00127 Con4TestSubmatrix(0, 0) = 4; Con4TestSubmatrix(0, 1) = 7; 00128 Con4TestSubmatrix(1, 0) = 5; Con4TestSubmatrix(1, 1) = 8; 00129 DMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 2, 2, 1, 1); 00130 numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, Con4TestSubmatrix, verbose); 00131 DMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 3, 3, 0, 0); 00132 numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose); 00133 DMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 2, 2, 1, 1); 00134 numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestSubmatrix, verbose); 00135 DMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 3, 0, 0); 00136 numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, Con4TestOrig, verbose); 00137 00138 // Norm Tests 00139 00140 DMatrix AAA; 00141 AAA.shape(3, 3); 00142 AAA(0, 0) = 1; AAA(0, 1) = 2; AAA(0, 2) = 3; 00143 AAA(1, 0) = 4; AAA(1, 1) = 5; AAA(1, 2) = 6; 00144 AAA(2, 0) = 7; AAA(2, 1) = 8; AAA(2, 2) = 9; 00145 DMatrix BBB; 00146 numberFailedTests += PrintTestResults("normOne of a 3x3", AAA.normOne(), 18.0, verbose); 00147 numberFailedTests += PrintTestResults("normInf of a 3x3", AAA.normInf(), 24.0, verbose); 00148 AAA = Teuchos::ScalarTraits<STYPE>::one(); 00149 numberFailedTests += PrintTestResults("normFrobenius of a 3x3", AAA.normFrobenius(), 3.0, verbose); 00150 numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose); 00151 numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose); 00152 numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose); 00153 00154 // multiply() -- dimensions tests 00155 00156 DMatrix DimTest0x0A, DimTest0x0B, DimTest2x0, DimTest1x2, DimTest2x1, DimTest2x2A, DimTest2x2B, 00157 DimTest3x3, DimTest0x2, DimTest0x0Result, DimTest1x1Result, DimTest2x0Result, DimTest1x2Result, DimTest2x1Result, DimTest2x2Result, 00158 DimTest2x3Result, DimTest0x2Result, DimTest3x3Result; 00159 00160 DimTest0x2.shape(0, 2); 00161 DimTest2x0.shape(2, 0); 00162 DimTest1x2.shape(1, 2); 00163 DimTest2x1.shape(2, 1); 00164 DimTest2x2A.shape(2, 2); 00165 DimTest2x2B.shape(2, 2); 00166 DimTest3x3.shape(3, 3); 00167 DimTest0x2Result.shape(0, 2); 00168 DimTest1x1Result.shape(1, 1); 00169 DimTest2x0Result.shape(2, 0); 00170 DimTest1x2Result.shape(1, 2); 00171 DimTest2x1Result.shape(2, 1); 00172 DimTest2x2Result.shape(2, 2); 00173 DimTest2x3Result.shape(2, 3); 00174 DimTest3x3Result.shape(3, 3); 00175 00176 returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest2x2B, 1); 00177 testName = "multiply() -- dimensions -- compatible square matrices"; 00178 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00179 returnCode = DimTest2x3Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest3x3, 1); 00180 testName = "multiply() -- dimensions -- incompatible square matrices"; 00181 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00182 returnCode = DimTest1x1Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest1x2, DimTest2x1, 1); 00183 testName = "multiply() -- dimensions -- compatible nonsquare matrices"; 00184 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00185 returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x1, DimTest1x2, 1); 00186 testName = "multiply() -- dimensions -- compatible nonsquare matrices"; 00187 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00188 returnCode = DimTest2x1Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x1, DimTest2x1, 1); 00189 testName = "multiply() -- dimensions -- incompatible nonsquare matrices"; 00190 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00191 returnCode = DimTest1x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest1x2, DimTest1x2, 1); 00192 testName = "multiply() -- dimensions -- incompatible nonsquare matrices"; 00193 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00194 returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x0, DimTest2x2A, 1); 00195 testName = "multiply() -- dimensions -- first operand bad numCols"; 00196 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00197 returnCode = DimTest0x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest0x2, 1); 00198 testName = "multiply() -- dimensions -- second operand bad numRows"; 00199 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00200 returnCode = DimTest2x2Result.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, DimTest2x2A, DimTest2x0, 1); 00201 testName = "multiply() -- dimensions -- second operand bad numCols"; 00202 numberFailedTests += ReturnCodeCheck(testName, returnCode, 1, verbose); 00203 00204 // multiply() -- multiplication results tests 00205 00206 DMatrix MultTest2x2A, MultTest2x2B, MultTest3x3A, MultTest3x3B, MultTest2x2ATimes2x2B, 00207 MultTest3x3ATimes3x3B, MultTest2x2BTimes2x2A, MultTest3x3BTimes3x3A, MultTest2x2ATimes2x2BExpResult, MultTest2x2BTimes2x2AExpResult, 00208 MultTest3x3ATimes3x3BExpResult, MultTest3x3BTimes3x3AExpResult, MultTest2x3A, MultTest2x3B, MultTest3x2A, MultTest3x2B, 00209 MultTest2x3ATimes3x2B, MultTest3x2ATimes2x3B, MultTest2x3BTimes3x2A, MultTest3x2BTimes2x3A, MultTest2x3ATimes3x2BExpResult, 00210 MultTest3x2ATimes2x3BExpResult, MultTest2x3BTimes3x2AExpResult, MultTest3x2BTimes2x3AExpResult; 00211 00212 MultTest2x2A.shape(2, 2); 00213 MultTest2x2B.shape(2, 2); 00214 MultTest3x3A.shape(3, 3); 00215 MultTest3x3B.shape(3, 3); 00216 MultTest2x2ATimes2x2B.shape(2, 2); 00217 MultTest2x2BTimes2x2A.shape(2, 2); 00218 MultTest3x3ATimes3x3B.shape(3, 3); 00219 MultTest3x3BTimes3x3A.shape(3, 3); 00220 MultTest2x2ATimes2x2BExpResult.shape(2, 2); 00221 MultTest2x2BTimes2x2AExpResult.shape(2, 2); 00222 MultTest3x3ATimes3x3BExpResult.shape(3, 3); 00223 MultTest3x3BTimes3x3AExpResult.shape(3, 3); 00224 MultTest2x3A.shape(2, 3); 00225 MultTest2x3B.shape(2, 3); 00226 MultTest3x2A.shape(3, 2); 00227 MultTest3x2B.shape(3, 2); 00228 MultTest2x3ATimes3x2B.shape(2, 2); 00229 MultTest3x2ATimes2x3B.shape(3, 3); 00230 MultTest2x3BTimes3x2A.shape(2, 2); 00231 MultTest3x2BTimes2x3A.shape(3, 3); 00232 MultTest2x3ATimes3x2BExpResult.shape(2, 2); 00233 MultTest3x2ATimes2x3BExpResult.shape(3, 3); 00234 MultTest2x3BTimes3x2AExpResult.shape(2, 2); 00235 MultTest3x2BTimes2x3AExpResult.shape(3, 3); 00236 00237 for(i = 0; i < 2; i++) 00238 { 00239 for(j = 0; j < 2; j++) 00240 { 00241 MultTest2x2A(i, j) = i + j; 00242 MultTest2x2B(i, j) = (i * j) + 1; 00243 } 00244 } 00245 for(i = 0; i < 3; i++) 00246 { 00247 for(j = 0; j < 3; j++) 00248 { 00249 MultTest3x3A(i, j) = i + j; 00250 MultTest3x3B(i, j) = (i * j) + 1; 00251 } 00252 } 00253 00254 MultTest2x2ATimes2x2BExpResult(0, 0) = 1; MultTest2x2ATimes2x2BExpResult(0, 1) = 2; 00255 MultTest2x2ATimes2x2BExpResult(1, 0) = 3; MultTest2x2ATimes2x2BExpResult(1, 1) = 5; 00256 MultTest2x2BTimes2x2AExpResult(0, 0) = 1; MultTest2x2BTimes2x2AExpResult(0, 1) = 3; 00257 MultTest2x2BTimes2x2AExpResult(1, 0) = 2; MultTest2x2BTimes2x2AExpResult(1, 1) = 5; 00258 MultTest3x3ATimes3x3BExpResult(0, 0) = 3; MultTest3x3ATimes3x3BExpResult(0, 1) = 8; MultTest3x3ATimes3x3BExpResult(0, 2) = 13; 00259 MultTest3x3ATimes3x3BExpResult(1, 0) = 6; MultTest3x3ATimes3x3BExpResult(1, 1) = 14; MultTest3x3ATimes3x3BExpResult(1, 2) = 22; 00260 MultTest3x3ATimes3x3BExpResult(2, 0) = 9; MultTest3x3ATimes3x3BExpResult(2, 1) = 20; MultTest3x3ATimes3x3BExpResult(2, 2) = 31; 00261 MultTest3x3BTimes3x3AExpResult(0, 0) = 3; MultTest3x3BTimes3x3AExpResult(0, 1) = 6; MultTest3x3BTimes3x3AExpResult(0, 2) = 9; 00262 MultTest3x3BTimes3x3AExpResult(1, 0) = 8; MultTest3x3BTimes3x3AExpResult(1, 1) = 14; MultTest3x3BTimes3x3AExpResult(1, 2) = 20; 00263 MultTest3x3BTimes3x3AExpResult(2, 0) = 13; MultTest3x3BTimes3x3AExpResult(2, 1) = 22; MultTest3x3BTimes3x3AExpResult(2, 2) = 31; 00264 MultTest2x3A(0, 0) = 1; MultTest2x3A(0, 1) = 2; MultTest2x3A(0, 2) = 3; 00265 MultTest2x3A(1, 0) = 4; MultTest2x3A(1, 1) = 5; MultTest2x3A(1, 2) = 6; 00266 MultTest3x2A(0, 0) = 1; MultTest3x2A(0, 1) = 2; 00267 MultTest3x2A(1, 0) = 3; MultTest3x2A(1, 1) = 4; 00268 MultTest3x2A(2, 0) = 5; MultTest3x2A(2, 1) = 6; 00269 MultTest2x3B(0, 0) = 0; MultTest2x3B(0, 1) = 2; MultTest2x3B(0, 2) = 4; 00270 MultTest2x3B(1, 0) = 6; MultTest2x3B(1, 1) = 8; MultTest2x3B(1, 2) = 10; 00271 MultTest3x2B(0, 0) = 0; MultTest3x2B(0, 1) = 2; 00272 MultTest3x2B(1, 0) = 4; MultTest3x2B(1, 1) = 6; 00273 MultTest3x2B(2, 0) = 8; MultTest3x2B(2, 1) = 10; 00274 MultTest2x3ATimes3x2BExpResult(0, 0) = 32; MultTest2x3ATimes3x2BExpResult(0, 1) = 44; 00275 MultTest2x3ATimes3x2BExpResult(1, 0) = 68; MultTest2x3ATimes3x2BExpResult(1, 1) = 98; 00276 MultTest3x2ATimes2x3BExpResult(0, 0) = 12; MultTest3x2ATimes2x3BExpResult(0, 1) = 18; MultTest3x2ATimes2x3BExpResult(0, 2) = 24; 00277 MultTest3x2ATimes2x3BExpResult(1, 0) = 24; MultTest3x2ATimes2x3BExpResult(1, 1) = 38; MultTest3x2ATimes2x3BExpResult(1, 2) = 52; 00278 MultTest3x2ATimes2x3BExpResult(2, 0) = 36; MultTest3x2ATimes2x3BExpResult(2, 1) = 58; MultTest3x2ATimes2x3BExpResult(2, 2) = 80; 00279 MultTest2x3BTimes3x2AExpResult(0, 0) = 26; MultTest2x3BTimes3x2AExpResult(0, 1) = 32; 00280 MultTest2x3BTimes3x2AExpResult(1, 0) = 80; MultTest2x3BTimes3x2AExpResult(1, 1) = 104; 00281 MultTest3x2BTimes2x3AExpResult(0, 0) = 8; MultTest3x2BTimes2x3AExpResult(0, 1) = 10; MultTest3x2BTimes2x3AExpResult(0, 2) = 12; 00282 MultTest3x2BTimes2x3AExpResult(1, 0) = 28; MultTest3x2BTimes2x3AExpResult(1, 1) = 38; MultTest3x2BTimes2x3AExpResult(1, 2) = 48; 00283 MultTest3x2BTimes2x3AExpResult(2, 0) = 48; MultTest3x2BTimes2x3AExpResult(2, 1) = 66; MultTest3x2BTimes2x3AExpResult(2, 2) = 84; 00284 00285 MultTest2x2ATimes2x2B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x2A, MultTest2x2B, 1); 00286 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x2 * 2x2", MultTest2x2ATimes2x2B, MultTest2x2ATimes2x2BExpResult, verbose); 00287 MultTest2x2BTimes2x2A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x2B, MultTest2x2A, 1); 00288 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x2 * 2x2", MultTest2x2BTimes2x2A, MultTest2x2BTimes2x2AExpResult, verbose); 00289 MultTest3x3ATimes3x3B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x3A, MultTest3x3B, 1); 00290 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x3 * 3x3", MultTest3x3ATimes3x3B, MultTest3x3ATimes3x3BExpResult, verbose); 00291 MultTest3x3BTimes3x3A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x3B, MultTest3x3A, 1); 00292 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x3 * 3x3", MultTest3x3BTimes3x3A, MultTest3x3BTimes3x3AExpResult, verbose); 00293 MultTest2x3ATimes3x2B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x3A, MultTest3x2B, 1); 00294 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x3 * 3x2", MultTest2x3ATimes3x2B, MultTest2x3ATimes3x2BExpResult, verbose); 00295 MultTest2x3BTimes3x2A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest2x3B, MultTest3x2A, 1); 00296 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 2x3 * 3x2", MultTest2x3BTimes3x2A, MultTest2x3BTimes3x2AExpResult, verbose); 00297 MultTest3x2ATimes2x3B.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x2A, MultTest2x3B, 1); 00298 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x2 * 2x3", MultTest3x2ATimes2x3B, MultTest3x2ATimes2x3BExpResult, verbose); 00299 MultTest3x2BTimes2x3A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, MultTest3x2B, MultTest2x3A, 1); 00300 numberFailedTests += PrintTestResults("multiply() -- mult. results -- 3x2 * 2x3", MultTest3x2BTimes2x3A, MultTest3x2BTimes2x3AExpResult, verbose); 00301 00302 DMatrix MultTestHugeA, MultTestHugeB, MultTestHugeATimesHugeBExpResult, 00303 MultTestHugeATimesHugeB; 00304 00305 const int hugeSize = 100; 00306 MultTestHugeA.shape(hugeSize, hugeSize); 00307 MultTestHugeB.shape(hugeSize, hugeSize); 00308 MultTestHugeATimesHugeBExpResult.shape(hugeSize, hugeSize); 00309 MultTestHugeATimesHugeB.shape(hugeSize, hugeSize); 00310 00311 for(i = 0; i < hugeSize; i++) 00312 { 00313 for(j = 0; j < hugeSize; j++) 00314 { 00315 MultTestHugeA(i, j) = j; 00316 MultTestHugeB(i, j) = i; 00317 MultTestHugeATimesHugeBExpResult(i, j) = 328350; 00318 } 00319 } 00320 00321 MultTestHugeATimesHugeB.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, 00322 MultTestHugeA, MultTestHugeB, 1.0); 00323 numberFailedTests += PrintTestResults( 00324 "multiply() -- mult. results -- huge * huge", 00325 MultTestHugeATimesHugeB, MultTestHugeATimesHugeBExpResult, verbose); 00326 00327 // 00328 // Check scale methods. 00329 // 00330 DMatrix ScalTest( 8, 8 ); 00331 ScalTest = Teuchos::ScalarTraits<STYPE>::one(); 00332 // Scale the entries by 8, it should be 8. 00333 if (verbose) std::cout << "scale() -- scale matrix by some number "; 00334 returnCode = ScalTest.scale( 8.0 ); 00335 if (ScalTest(2, 3) == 8.0) { 00336 if (verbose) std::cout<< "successful." <<std::endl; 00337 } else { 00338 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00339 numberFailedTests++; 00340 } 00341 // Pointwise scale the entries by zero, they all should be zero. 00342 DMatrix ScalTest2( 8, 8 ); 00343 if (verbose) std::cout << "scale() -- point-wise scale matrix "; 00344 ScalTest.scale( ScalTest2 ); 00345 if (ScalTest.normOne() == 0.0) { 00346 if (verbose) std::cout<< "successful." <<std::endl; 00347 } else { 00348 if (verbose) std::cout<< "unsuccessful." <<std::endl; 00349 numberFailedTests++; 00350 } 00351 // 00352 // Check set methods. 00353 // 00354 DMatrix CCC( 5, 5 ); 00355 // Randomize the entries in CCC. 00356 testName = "random() -- enter random entries into matrix"; 00357 returnCode = CCC.random(); 00358 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00359 // Set the entries of CCC to 1.0. 00360 testName = "putScalar() -- set every entry of this matrix to 1.0"; 00361 returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one()); 00362 numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); 00363 // Check assignment operator. 00364 DMatrix CCC2( 5, 5 ); 00365 CCC2.assign( CCC ); 00366 if (verbose) std::cout << "assign() -- copy the values of an input matrix "; 00367 if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) { 00368 if (verbose) std::cout<< "successful" <<std::endl; 00369 } else { 00370 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00371 numberFailedTests++; 00372 } 00373 // Create a view into a submatrix of CCC 00374 DMatrix CCCview( Teuchos::View, CCC, 3, 3 ); 00375 DMatrix CCCtest1( 2, 3 ); 00376 CCCtest1 = CCCview; 00377 if (verbose) std::cout << "operator= -- small(empty) = large(view) "; 00378 if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) { 00379 if (verbose) std::cout<< "successful" <<std::endl; 00380 } else { 00381 if (verbose) std::cout<< "unsuccessful" <<std::endl; 00382 numberFailedTests++; 00383 } 00384 CCCtest1 = CCC; 00385 if (verbose) std::cout << "operator= -- small(view) = large(copy) "; 00386 if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) { 00387 if (verbose) std::cout<< "successful"<<std::endl; 00388 } else { 00389 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00390 numberFailedTests++; 00391 } 00392 DMatrix CCCtest2( 2, 2 ); 00393 CCCtest2 = 3.0; 00394 CCCtest1 = CCCtest2; 00395 if (verbose) std::cout << "operator= -- large(copy) = small(copy) "; 00396 if (CCCtest1.numRows()==2 ) { 00397 if (verbose) std::cout<< "successful"<<std::endl; 00398 } else { 00399 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00400 numberFailedTests++; 00401 } 00402 CCCtest1 = CCCview; 00403 if (verbose) std::cout << "operator= -- large(copy) = small(view) "; 00404 if (CCCtest1.numRows()==3 && CCCtest1.stride()==5) { 00405 if(verbose) std::cout<<"successful" <<std::endl; 00406 } else { 00407 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00408 numberFailedTests++; 00409 } 00410 00411 DMatrix CCCtest3( CCCview ); 00412 CCCtest1 += CCCtest3; 00413 if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension "; 00414 if (CCCtest1(1,1)==2.0) { 00415 if(verbose) std::cout<<"successful" <<std::endl; 00416 } else { 00417 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00418 numberFailedTests++; 00419 } 00420 if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) "; 00421 CCCtest1 += CCC; 00422 if (CCCtest1(1,1)==2.0) { 00423 if(verbose) std::cout<<"successful" <<std::endl; 00424 } else { 00425 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00426 numberFailedTests++; 00427 } 00428 // 00429 // Check overloaded operators. 00430 // 00431 bool op_result; 00432 MultTestHugeATimesHugeB.reshape(10, 10); 00433 op_result = (MultTestHugeATimesHugeB == MultTestHugeATimesHugeBExpResult); 00434 if (verbose) { 00435 std::cout << "operator== -- results -- small == huge "<< (op_result == false ? "successful" : "failed" )<<std::endl; 00436 } 00437 op_result = (MultTestHugeATimesHugeB != MultTestHugeATimesHugeBExpResult); 00438 if (verbose) { 00439 std::cout << "operator!= -- results -- small != huge "<< (op_result == true ? "successful" : "failed" )<<std::endl; 00440 std::cout << std::endl<< MultTestHugeATimesHugeB << std::endl; 00441 //These won't work unless boundschecking is enabled. 00442 //std::cout << MultTestHugeATimesHugeB(100, 1) << std::endl; 00443 //std::cout << MultTestHugeATimesHugeB(1, 100) << std::endl; 00444 } 00445 00446 00447 if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL DENSE VECTOR **********"<<std::endl<<std::endl; 00448 00449 DVector DefConTestV; 00450 if (verbose) std::cout <<"default constructor -- construct empty std::vector "; 00451 if ( DefConTestV.values()!=NULL || DefConTestV.length()!=0 || DefConTestV.numRows()!=0 ||DefConTestV.stride()!=0 ) { 00452 if (verbose) std::cout << "unsuccessful."<<std::endl; 00453 numberFailedTests++; 00454 } else { 00455 if (verbose) std::cout << "successful."<<std::endl; 00456 } 00457 00458 // constructor 1 (matrix w/ dimension but empty) 00459 00460 DVector Con1TestV( 3 ); 00461 if (verbose) std::cout <<"constructor 1 -- empty std::vector with given dimensions "; 00462 if ( Con1TestV.length()!=3 || Con1TestV.numCols()!=1 || Con1TestV( 1 )!=0.0 ) { 00463 if (verbose) std::cout << "unsuccessful."<<std::endl; 00464 numberFailedTests++; 00465 } else { 00466 if (verbose) std::cout << "successful."<<std::endl; 00467 } 00468 00469 // constructor 2 (from array) tests 00470 00471 DVector Con2Test1V(Teuchos::Copy, a, 4); 00472 if (verbose) std::cout <<"constructor 2 -- construct std::vector from array subrange "; 00473 if ( Con2Test1V.numRows()!=4 || Con2Test1V.numCols()!=1 || Con2Test1V[ 2 ]!=2.0 ) { 00474 if (verbose) std::cout << "unsuccessful."<<std::endl; 00475 numberFailedTests++; 00476 } else { 00477 if (verbose) std::cout << "successful."<<std::endl; 00478 } 00479 00480 // constructor 3 (copy constructor) 00481 00482 DVector Con3TestCopyV( Con2Test1V ); 00483 if(verbose) std::cout <<"constructor 3 -- copy constructor "; 00484 if ( Con3TestCopyV != Con2Test1V ) { 00485 if (verbose) std::cout << "unsuccessful."<<std::endl; 00486 numberFailedTests++; 00487 } else { 00488 if (verbose) std::cout << "successful."<<std::endl; 00489 } 00490 00491 // non-member helper function (construct vector view of matrix column) 00492 00493 OTYPE col = Teuchos::OrdinalTraits<OTYPE>::one(); 00494 DVector ColViewTestV = Teuchos::getCol<OTYPE,STYPE>( Teuchos::View, AAA, col ); 00495 if (verbose) std::cout <<"non-method helper function -- construct vector view of second column of matrix "; 00496 if ( ColViewTestV.normInf() != 1.0 || ColViewTestV.normOne() != 3.0 ) { 00497 if (verbose) std::cout << "unsuccessful."<<std::endl; 00498 numberFailedTests++; 00499 } else { 00500 if (verbose) std::cout << "successful."<<std::endl; 00501 } 00502 00503 // checking norms 00504 00505 numberFailedTests += PrintTestResults("normOne of a 3x1 std::vector", Con2Test1V.normOne(), 6.0, verbose); 00506 numberFailedTests += PrintTestResults("normInf of a 3x1 std::vector", Con2Test1V.normInf(), 3.0, verbose); 00507 Con2Test1V = Teuchos::ScalarTraits<STYPE>::one(); 00508 numberFailedTests += PrintTestResults("normFrobenius of a 3x1 std::vector", Con2Test1V.normFrobenius(), 2.0, verbose); 00509 00510 // check size/resize 00511 00512 DVector SizeTestV1; 00513 SizeTestV1.size( 5 ); 00514 if(verbose) std::cout <<"size() -- test "; 00515 if (SizeTestV1( 4 )!= 0.0) { 00516 if (verbose) std::cout << "unsuccessful."<<std::endl; 00517 numberFailedTests++; 00518 } else { 00519 if (verbose) std::cout << "successful."<<std::endl; 00520 } 00521 SizeTestV1 = 2.0*Teuchos::ScalarTraits<STYPE>::one(); 00522 SizeTestV1.resize( 10 ); 00523 if(verbose) std::cout <<"resize() -- test small --> large "; 00524 if (SizeTestV1[ 4 ]!= 2.0 || SizeTestV1[ 8 ]!=0.0 ) { 00525 if (verbose) std::cout << "unsuccessful."<<std::endl; 00526 numberFailedTests++; 00527 } else { 00528 if (verbose) std::cout << "successful."<<std::endl; 00529 } 00530 SizeTestV1.resize( 3 ); 00531 if(verbose) std::cout <<"resize() -- test large --> small "; 00532 if (SizeTestV1( 2 )!= 2.0) { 00533 if (verbose) std::cout << "unsuccessful."<<std::endl; 00534 numberFailedTests++; 00535 } else { 00536 if (verbose) std::cout << "successful."<<std::endl; 00537 } 00538 00539 DVector OpEqTestV1( 10 ); OpEqTestV1 = 3.0*Teuchos::ScalarTraits<STYPE>::one(); 00540 DVector OpEqTestV2( Teuchos::View, OpEqTestV1.values(), 3 ); 00541 DVector OpEqTestV3( 2 ); 00542 OpEqTestV3 = OpEqTestV2; 00543 if (verbose) std::cout << "operator= -- small(empty) = large(view) "; 00544 if (OpEqTestV3.length()==3 && OpEqTestV3.values()==OpEqTestV2.values()) { 00545 if (verbose) std::cout<< "successful"<<std::endl; 00546 } else { 00547 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00548 numberFailedTests++; 00549 } 00550 OpEqTestV3 = OpEqTestV1; 00551 if (verbose) std::cout << "operator= -- small(view) = large(copy) "; 00552 if (OpEqTestV3.length()==10 && OpEqTestV3.values()!=OpEqTestV1.values()) { 00553 if (verbose) std::cout<< "successful"<<std::endl; 00554 } else { 00555 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00556 numberFailedTests++; 00557 } 00558 OpEqTestV3.size(5); 00559 OpEqTestV3 = OpEqTestV1; 00560 if (verbose) std::cout << "operator= -- small(copy) = large(copy) "; 00561 if (OpEqTestV3.length()==10 && OpEqTestV3.values()!=OpEqTestV1.values() && OpEqTestV3[ 9 ]==3.0) { 00562 if (verbose) std::cout<< "successful"<<std::endl; 00563 } else { 00564 if (verbose) std::cout<< "unsuccessful"<<std::endl; 00565 numberFailedTests++; 00566 } 00567 00568 DVector OpSumTestV1( OpEqTestV2 ); 00569 OpSumTestV1 += OpEqTestV2; 00570 if (verbose) std::cout << "operator+= -- add two vectors of the same size, but different leading dimension "; 00571 if (OpSumTestV1( 1 )==6.0) { 00572 if (verbose) std::cout<<"successful" <<std::endl; 00573 } else { 00574 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00575 numberFailedTests++; 00576 } 00577 if (verbose) std::cout << "operator+= -- add two vectors of different size (nothing should change) "; 00578 OpSumTestV1 += OpEqTestV1; 00579 if (OpSumTestV1( 1 )==6.0) { 00580 if (verbose) std::cout<<"successful" <<std::endl; 00581 } else { 00582 if (verbose) std::cout<<"unsuccessful"<<std::endl; 00583 numberFailedTests++; 00584 } 00585 00586 DVector OpCompTestV1( 5 ); 00587 OpCompTestV1 = 2.0*Teuchos::ScalarTraits<STYPE>::one(); 00588 if(verbose) std::cout <<"operator== -- test large == small "; 00589 if (OpCompTestV1 == SizeTestV1) { 00590 if (verbose) std::cout << "unsuccessful."<<std::endl; 00591 numberFailedTests++; 00592 } else { 00593 if (verbose) std::cout << "successful."<<std::endl; 00594 } 00595 if(verbose) std::cout <<"operator!= -- test large != small "; 00596 if (OpCompTestV1 != SizeTestV1) { 00597 if (verbose) std::cout << "successful."<<std::endl; 00598 } else { 00599 if (verbose) std::cout << "successful."<<std::endl; 00600 numberFailedTests++; 00601 } 00602 00603 DVector ColSetTestV( AAA.numRows() ); 00604 ColSetTestV.putScalar( 2.0 ); 00605 bool ret = Teuchos::setCol<OTYPE,STYPE>( ColSetTestV, col, AAA ); 00606 if (verbose) std::cout <<"non-method helper function -- set second column of matrix with vector "; 00607 if ( ColViewTestV.normInf() != 2.0 || ColViewTestV.normOne() != 6.0 || ret == false ) { 00608 if (verbose) std::cout << "unsuccessful."<<std::endl; 00609 numberFailedTests++; 00610 } else { 00611 if (verbose) std::cout << "successful."<<std::endl; 00612 } 00613 // 00614 // If a test failed output the number of failed tests. 00615 // 00616 if(numberFailedTests > 0) 00617 { 00618 if (verbose) { 00619 std::cout << "Number of failed tests: " << numberFailedTests << std::endl; 00620 std::cout << "End Result: TEST FAILED" << std::endl; 00621 return -1; 00622 } 00623 } 00624 if(numberFailedTests == 0) 00625 std::cout << "End Result: TEST PASSED" << std::endl; 00626 00627 return 0; 00628 } 00629 00630 template<typename TYPE> 00631 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose) 00632 { 00633 int result; 00634 if(calculatedResult == expectedResult) 00635 { 00636 if(verbose) std::cout << testName << " successful." << std::endl; 00637 result = 0; 00638 } 00639 else 00640 { 00641 if(verbose) std::cout << testName << " unsuccessful." << std::endl; 00642 result = 1; 00643 } 00644 return result; 00645 } 00646 00647 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose) 00648 { 00649 int result; 00650 if(expectedResult == 0) 00651 { 00652 if(returnCode == 0) 00653 { 00654 if(verbose) std::cout << testName << " test successful." << std::endl; 00655 result = 0; 00656 } 00657 else 00658 { 00659 if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl; 00660 result = 1; 00661 } 00662 } 00663 else 00664 { 00665 if(returnCode != 0) 00666 { 00667 if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl; 00668 result = 0; 00669 } 00670 else 00671 { 00672 if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl; 00673 result = 1; 00674 } 00675 } 00676 return result; 00677 }
1.7.4