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