Teuchos Package Browser (Single Doxygen Collection) Version of the Day
cxx_main_band.cpp
Go to the documentation of this file.
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_SerialBandDenseMatrix.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::SerialBandDenseMatrix<OTYPE, STYPE> BDMatrix;
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 BANDED DENSE MATRIX **********"<<std::endl<<std::endl;
00075 
00076   // default constructor test
00077   BDMatrix 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   BDMatrix Con1Test( 4, 4, 1, 1 );
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[25];
00100         a[4] = 5;  a[8]  = 11;   a[12] = 17;  a[16] = 23;
00101   a[1] = 0;   a[5] = 6;  a[9]  = 12;   a[13] = 18;  a[17] = 24;
00102   a[2] = 1;   a[6] = 7;  a[10] = 13;   a[14] = 19;
00103   a[3] = 2;   a[7] = 8;  a[11] = 14;
00104 
00105   BDMatrix C2T1ER;
00106   C2T1ER.shape(5, 5, 2, 1);
00107   C2T1ER(0, 0) = 0;  C2T1ER(0, 1) = 5;
00108   C2T1ER(1, 0) = 1;  C2T1ER(1, 1) = 6;  C2T1ER(1, 2) = 11;
00109   C2T1ER(2, 0) = 2;  C2T1ER(2, 1) = 7;  C2T1ER(2, 2) = 12;  C2T1ER(2, 3) = 17;
00110          C2T1ER(3, 1) = 8;  C2T1ER(3, 2) = 13;  C2T1ER(3, 3) = 18;  C2T1ER(3, 4) = 23;
00111           C2T1ER(4, 2) = 14;  C2T1ER(4, 3) = 19;  C2T1ER(4, 4) = 24;
00112 
00113   // Create another lower triangular matrix with a view of 'a'.
00114   BDMatrix Con2Test1(Teuchos::Copy, a, 4, 5, 5, 2, 1);
00115   numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, C2T1ER, verbose);
00116 
00117   // constructor 3 (copy constructor)
00118 
00119   BDMatrix Con3TestCopy( C2T1ER );
00120   if(verbose) std::cout <<"constructor 3 -- copy constructor ";
00121   if ( Con3TestCopy != C2T1ER ) {
00122   if (verbose) std::cout << "unsuccessful."<<std::endl;
00123   numberFailedTests++;
00124   } else {
00125   if (verbose) std::cout << "successful."<<std::endl;
00126   }
00127 
00128   BDMatrix Con3TestCopyTrans( C2T1ER, Teuchos::TRANS );
00129   if(verbose) std::cout <<"constructor 3 -- copy constructor (transposed) ";
00130   if ( Con3TestCopyTrans(0, 2) != C2T1ER(2, 0) ) {
00131   if (verbose) std::cout << "unsuccessful."<<std::endl;
00132   numberFailedTests++;
00133   } else {
00134   if (verbose) std::cout << "successful."<<std::endl;
00135   }
00136 
00137   // constructor 4 (submatrix)
00138 
00139   BDMatrix Con4TestOrig(Teuchos::Copy, a, 4, 5, 5, 2, 1);
00140   BDMatrix C4TS;
00141   C4TS.shape( 3, 3, 2, 1 );
00142   C4TS(0, 0) = 12;  C4TS(0, 1) = 17;
00143   C4TS(1, 0) = 13;  C4TS(1, 1) = 18;  C4TS(1, 2) = 23;
00144   C4TS(2, 0) = 14;  C4TS(2, 1) = 19;  C4TS(2, 2) = 24;
00145 
00146   BDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 3, 3, 2);
00147   numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, C4TS, verbose);
00148   BDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 5, 5, 0);
00149   numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose);
00150   BDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 5, 5, 0);
00151   numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestOrig, verbose);
00152   BDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 3, 2);
00153   numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, C4TS, verbose);
00154 
00155   // Norm Tests
00156 
00157   BDMatrix AAA;
00158   AAA.shape( 5, 5, 2, 1 );
00159   AAA(0, 0) = 0;  AAA(0, 1) = 5;
00160   AAA(1, 0) = 1;  AAA(1, 1) = 6;  AAA(1, 2) = 11;
00161   AAA(2, 0) = 2;  AAA(2, 1) = 7;  AAA(2, 2) = 12;  AAA(2, 3) = 17;
00162       AAA(3, 1) = 8;  AAA(3, 2) = 13;  AAA(3, 3) = 18;  AAA(3, 4) = 23;
00163           AAA(4, 2) = 14;  AAA(4, 3) = 19;  AAA(4, 4) = 24;
00164 
00165   BDMatrix BBB;
00166   numberFailedTests += PrintTestResults("normOne of a 5x5", AAA.normOne(), 54.0, verbose);
00167   numberFailedTests += PrintTestResults("normInf of a 5x5", AAA.normInf(), 62.0, verbose);
00168   AAA = Teuchos::ScalarTraits<STYPE>::one();
00169   numberFailedTests += PrintTestResults("normFrobenius of a 5x5", AAA.normFrobenius(), 4.0, verbose);
00170   numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose);
00171   numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose);
00172   numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose);
00173 
00174   //  Set Method Tests.
00175 
00176   BDMatrix CCC( 5, 5, 2, 1 );
00177   //  Randomize the entries in CCC.
00178   testName = "random() -- enter random entries into matrix";
00179   returnCode = CCC.random();
00180   numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
00181   //  Set the entries of CCC to 1.0.
00182   testName = "putScalar() -- set every entry of this matrix to 1.0";
00183   returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one());
00184   numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose);
00185   //  Check assignment operator.
00186   BDMatrix CCC2( 5, 5, 2, 1 );
00187   CCC2.assign( CCC );
00188   if (verbose) std::cout <<  "assign() -- copy the values of an input matrix ";
00189   if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) {
00190     if (verbose) std::cout<< "successful" <<std::endl;
00191   } else {
00192     if (verbose) std::cout<< "unsuccessful" <<std::endl;
00193     numberFailedTests++;
00194   }
00195   //  Create a view into a submatrix of CCC
00196   BDMatrix CCCview( Teuchos::View, CCC, 3, 3 );
00197   BDMatrix CCCtest1( 3, 3, 2, 1 );
00198   CCCtest1 = CCCview;
00199   if (verbose) std::cout << "operator= -- small(empty) = large(view) ";
00200   if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) {
00201     if (verbose) std::cout<< "successful" <<std::endl;
00202   } else {
00203     if (verbose) std::cout<< "unsuccessful" <<std::endl;
00204     numberFailedTests++;
00205   }
00206   CCCtest1 = CCC;
00207   if (verbose) std::cout << "operator= -- small(view) = large(copy) ";
00208   if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) {
00209     if (verbose) std::cout<< "successful"<<std::endl;
00210   } else {
00211     if (verbose) std::cout<< "unsuccessful"<<std::endl;
00212     numberFailedTests++;
00213   }
00214   BDMatrix CCCtest2( 3, 3, 2, 1 );
00215   CCCtest2 = Teuchos::ScalarTraits<STYPE>::one();
00216   CCCtest1 = CCCtest2;
00217   if (verbose) std::cout << "operator= -- large(copy) = small(copy) ";
00218   if (CCCtest1.numRows()==3 ) {
00219     if (verbose) std::cout<< "successful"<<std::endl;
00220   } else {
00221     if (verbose) std::cout<< "unsuccessful"<<std::endl;
00222     numberFailedTests++;
00223   }
00224   CCCtest1 = CCCview;
00225   if (verbose) std::cout << "operator= -- large(copy) = small(view) ";
00226   if (CCCtest1.numRows()==3 && CCCtest1.stride()==4) {
00227     if(verbose) std::cout<<"successful" <<std::endl;
00228   } else {
00229     if (verbose) std::cout<<"unsuccessful"<<std::endl;
00230     numberFailedTests++;
00231   }
00232   BDMatrix 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   BDMatrix ScalTest( 8, 8, 2, 3 );
00253   ScalTest = Teuchos::ScalarTraits<STYPE>::one();
00254   //  Scale the entries by 8, it should be 8.
00255   //  The matrix is lower triangular, by default, so check a lower triangular entry.
00256   if (verbose) std::cout << "operator*= -- scale matrix by some number ";
00257   ScalTest *= 8.0;
00258   if (ScalTest(5, 7) == 8.0) {
00259   if (verbose) std::cout<< "successful." <<std::endl;
00260   } else {
00261   if (verbose) std::cout<< "unsuccessful." <<std::endl;
00262   numberFailedTests++;
00263   }
00264 
00265 
00266   //
00267   // If a test failed output the number of failed tests.
00268   //
00269   if(numberFailedTests > 0)
00270   {
00271       if (verbose) {
00272     std::cout << "Number of failed tests: " << numberFailedTests << std::endl;
00273     std::cout << "End Result: TEST FAILED" << std::endl;
00274     return -1;
00275       }
00276   }
00277   if(numberFailedTests == 0)
00278     std::cout << "End Result: TEST PASSED" << std::endl;
00279 
00280   return 0;
00281 }
00282 
00283 template<typename TYPE>
00284 int PrintTestResults(std::string testName, TYPE calculatedResult, TYPE expectedResult, bool verbose)
00285 {
00286   int result;
00287   if(calculatedResult == expectedResult)
00288     {
00289       if(verbose) std::cout << testName << " successful." << std::endl;
00290       result = 0;
00291     }
00292   else
00293     {
00294       if(verbose) std::cout << testName << " unsuccessful." << std::endl;
00295       result = 1;
00296     }
00297   return result;
00298 }
00299 
00300 int ReturnCodeCheck(std::string testName, int returnCode, int expectedResult, bool verbose)
00301 {
00302   int result;
00303   if(expectedResult == 0)
00304     {
00305       if(returnCode == 0)
00306   {
00307     if(verbose) std::cout << testName << " test successful." << std::endl;
00308     result = 0;
00309   }
00310       else
00311   {
00312     if(verbose) std::cout << testName << " test unsuccessful. Return code was " << returnCode << "." << std::endl;
00313     result = 1;
00314   }
00315     }
00316   else
00317     {
00318       if(returnCode != 0)
00319   {
00320     if(verbose) std::cout << testName << " test successful -- failed as expected." << std::endl;
00321     result = 0;
00322   }
00323       else
00324   {
00325     if(verbose) std::cout << testName << " test unsuccessful -- did not fail as expected. Return code was " << returnCode << "." << std::endl;
00326     result = 1;
00327   }
00328     }
00329   return result;
00330 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines