int main(int argc, char* argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif // Creating a double-precision matrix can be done in several ways: // Create an empty matrix with no dimension Teuchos::SerialDenseMatrix<int,double> Empty_Matrix; // Create an empty 3x4 matrix Teuchos::SerialDenseMatrix<int,double> My_Matrix( 3, 4 ); // Basic copy of My_Matrix Teuchos::SerialDenseMatrix<int,double> My_Copy1( My_Matrix ), // (Deep) Copy of principle 3x3 submatrix of My_Matrix My_Copy2( Teuchos::Copy, My_Matrix, 3, 3 ), // (Shallow) Copy of 2x3 submatrix of My_Matrix My_Copy3( Teuchos::View, My_Matrix, 2, 3, 1, 1 ); // The matrix dimensions and strided storage information can be obtained: int rows, cols, stride; rows = My_Copy3.numRows(); // number of rows cols = My_Copy3.numCols(); // number of columns stride = My_Copy3.stride(); // storage stride // Matrices can change dimension: Empty_Matrix.shape( 3, 3 ); // size non-dimensional matrices My_Matrix.reshape( 3, 3 ); // resize matrices and save values // Filling matrices with numbers can be done in several ways: My_Matrix.random(); // random numbers My_Copy1.putScalar( 1.0 ); // every entry is 1.0 My_Copy2(1,1) = 10.0; // individual element access Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix // Basic matrix arithmetic can be performed: Teuchos::SerialDenseMatrix<int,double> My_Prod( 3, 2 ); // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T ) My_Prod.multiply( Teuchos::NO_TRANS, Teuchos::TRANS, 1.0, My_Matrix, My_Copy3, 0.0 ); My_Copy2 += My_Matrix; // Matrix addition My_Copy2.scale( 0.5 ); // Matrix scaling // The pointer to the array of matrix values can be obtained: double *My_Array, *My_Column; My_Array = My_Matrix.values(); // pointer to matrix values My_Column = My_Matrix[2]; // pointer to third column values // The norm of a matrix can be computed: double norm_one, norm_inf, norm_fro; norm_one = My_Matrix.normOne(); // one norm norm_inf = My_Matrix.normInf(); // infinity norm norm_fro = My_Matrix.normFrobenius(); // frobenius norm // Matrices can be compared: // Check if the matrices are equal in dimension and values if (Empty_Matrix == My_Matrix) { cout<< "The matrices are the same!" <<endl; } // Check if the matrices are different in dimension or values if (My_Copy2 != My_Matrix) { cout<< "The matrices are different!" <<endl; } // A matrix can be sent to the output stream: cout<< My_Matrix << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif return 0; }
int main(int argc, char* argv[]) { std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; // Creating a double-precision matrix can be done in several ways: // Create an empty matrix with no dimension Teuchos::SerialSymDenseMatrix<int,double> Empty_Matrix; // Create an empty 4x4 matrix Teuchos::SerialSymDenseMatrix<int,double> My_Matrix( 4 ); // Basic copy of My_Matrix Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ), // (Deep) Copy of principle 3x3 submatrix of My_Matrix My_Copy2( Teuchos::Copy, My_Matrix, 3 ), // (Shallow) Copy of 3x3 submatrix of My_Matrix My_Copy3( Teuchos::View, My_Matrix, 3, 1 ); // The matrix dimensions and strided storage information can be obtained: int rows, cols, stride; rows = My_Copy3.numRows(); // number of rows cols = My_Copy3.numCols(); // number of columns stride = My_Copy3.stride(); // storage stride TEUCHOS_ASSERT_EQUALITY(rows, 3); TEUCHOS_ASSERT_EQUALITY(cols, 3); TEUCHOS_ASSERT_EQUALITY(stride, 4); // Matrices can change dimension: Empty_Matrix.shape( 3 ); // size non-dimensional matrices My_Matrix.reshape( 3 ); // resize matrices and save values // Filling matrices with numbers can be done in several ways: My_Matrix.random(); // random numbers My_Copy1.putScalar( 1.0 ); // every entry is 1.0 My_Copy1 = 1.0; // every entry is 1.0 (still) My_Copy2(1,1) = 10.0; // individual element access Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix // Basic matrix arithmetic can be performed: Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 ); My_GenMatrix = 1.0; // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix ) My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 ); My_Copy2 += My_Matrix; // Matrix addition My_Copy2 *= 0.5; // Matrix scaling // Matrices can be compared: // Check if the matrices are equal in dimension and values if (Empty_Matrix == My_Matrix) { std::cout<< "The matrices are the same!" <<std::endl; } // Check if the matrices are different in dimension or values if (My_Copy2 != My_Matrix) { std::cout<< "The matrices are different!" <<std::endl; } // The norm of a matrix can be computed: double norm_one, norm_inf, norm_fro; norm_one = My_Matrix.normOne(); // one norm norm_inf = My_Matrix.normInf(); // infinity norm norm_fro = My_Matrix.normFrobenius(); // frobenius norm std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl; std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl; std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl; // A matrix can be factored and solved using Teuchos::SerialDenseSolver. Teuchos::SerialSpdDenseSolver<int,double> My_Solver; Teuchos::SerialSymDenseMatrix<int,double> My_Matrix2( 3 ); My_Matrix2.random(); Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1); X = 1.0; B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 ); X = 0.0; // Make sure the computed answer is correct. int info = 0; My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) ); My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) ); info = My_Solver.factor(); if (info != 0) std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl; info = My_Solver.solve(); if (info != 0) std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl; // A matrix triple-product can be computed: C = alpha*W'*A*W double alpha=0.5; Teuchos::SerialDenseMatrix<int,double> W(3,2); Teuchos::SerialSymDenseMatrix<int,double> A1(2), A2(3); A1(0,0) = 1.0, A1(1,1) = 2.0; A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00; W = 1.0; Teuchos::SerialSymDenseMatrix<int,double> C1(3), C2(2); Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1); Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 ); // A matrix can be sent to the output stream: std::cout<< My_Matrix << std::endl; std::cout<< X << std::endl; return 0; }
int main(int argc, char* argv[]) { int i; bool verbose = 0; if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; if (verbose) std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; int numberFailedTests = 0; int returnCode = 0; std::string testName = ""; if (verbose) std::cout<<std::endl<<"********** CHECKING TEUCHOS SERIAL SYMMETRIC DENSE MATRIX **********"<<std::endl<<std::endl; // default constructor test SDMatrix DefConTest; if (verbose) std::cout <<"default constructor -- construct empty matrix "; if ( DefConTest.values()!=NULL || DefConTest.numCols()!=0 || DefConTest.numRows()!=0 ||DefConTest.stride()!=0 ||DefConTest.empty()!=true ) { if (verbose) std::cout << "unsuccessful."<<std::endl; numberFailedTests++; } else { if (verbose) std::cout << "successful."<<std::endl; } // constructor 1 (matrix w/ dimension but empty) SDMatrix Con1Test( 4 ); if (verbose) std::cout <<"constructor 1 -- empty matrix with given dimensions "; if ( Con1Test.numRows()!=4 || Con1Test.numCols()!=4 || Con1Test( 1, 2 )!=0.0 ) { if (verbose) std::cout << "unsuccessful."<<std::endl; numberFailedTests++; } else { if (verbose) std::cout << "successful."<<std::endl; } // constructor 2 (from array) tests STYPE a[9]; for(i = 0; i < 9; i++) { a[i] = i; } SDMatrix Con2Test1ExpRes; Con2Test1ExpRes.shape(3); Con2Test1ExpRes(0, 0) = 0; Con2Test1ExpRes(1, 0) = 1; Con2Test1ExpRes(1, 1) = 4; Con2Test1ExpRes(2, 0) = 2; Con2Test1ExpRes(2, 1) = 5; Con2Test1ExpRes(2, 2) = 8; // Create another lower triangular matrix with a view of 'a'. SDMatrix Con2Test1(Teuchos::View, false, a, 3, 3); numberFailedTests += PrintTestResults("constructor 2 -- construct matrix from array subrange", Con2Test1, Con2Test1ExpRes, verbose); // constructor 3 (copy constructor) SDMatrix Con3TestCopy( Con2Test1ExpRes ); if(verbose) std::cout <<"constructor 3 -- copy constructor "; if ( Con3TestCopy != Con2Test1ExpRes ) { if (verbose) std::cout << "unsuccessful."<<std::endl; numberFailedTests++; } else { if (verbose) std::cout << "successful."<<std::endl; } SDMatrix Con3TestCopyTrans( Con2Test1ExpRes ); Con3TestCopyTrans.setUpper(); if(verbose) std::cout <<"constructor 3 -- copy constructor (upper active storage) "; if ( Con3TestCopyTrans(2, 0) != Con2Test1ExpRes(2, 0) ) { if (verbose) std::cout << "unsuccessful."<<std::endl; numberFailedTests++; } else { if (verbose) std::cout << "successful."<<std::endl; } // constructor 4 (submatrix) SDMatrix Con4TestOrig(Teuchos::Copy, false, a, 3, 3); SDMatrix Con4TestSubmatrix; Con4TestSubmatrix.shape( 2 ); Con4TestSubmatrix(0, 0) = 4; Con4TestSubmatrix(1, 0) = 5; Con4TestSubmatrix(1, 1) = 8; SDMatrix Con4TestCopy1(Teuchos::Copy, Con4TestOrig, 2, 1); numberFailedTests += PrintTestResults("constructor 4 -- submatrix copy", Con4TestCopy1, Con4TestSubmatrix, verbose); SDMatrix Con4TestCopy2(Teuchos::Copy, Con4TestOrig, 3, 0); numberFailedTests += PrintTestResults("constructor 4 -- full matrix copy", Con4TestCopy2, Con4TestOrig, verbose); SDMatrix Con4TestView1(Teuchos::View, Con4TestOrig, 2, 1); numberFailedTests += PrintTestResults("constructor 4 -- full matrix view", Con4TestView1, Con4TestSubmatrix, verbose); SDMatrix Con4TestView2(Teuchos::View, Con4TestOrig, 3, 0); numberFailedTests += PrintTestResults("constructor 4 -- submatrix view", Con4TestView2, Con4TestOrig, verbose); // Norm Tests SDMatrix AAA; AAA.shape( 3 ); AAA(0, 0) = 8; AAA(1, 0) = 1; AAA(1, 1) = 8; AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; SDMatrix BBB; numberFailedTests += PrintTestResults("normOne of a 3x3", AAA.normOne(), 13.0, verbose); numberFailedTests += PrintTestResults("normInf of a 3x3", AAA.normInf(), 13.0, verbose); AAA = Teuchos::ScalarTraits<STYPE>::one(); numberFailedTests += PrintTestResults("normFrobenius of a 3x3", AAA.normFrobenius(), 3.0, verbose); numberFailedTests += PrintTestResults("normOne of a 0x0", BBB.normOne(), 0.0, verbose); numberFailedTests += PrintTestResults("normInf of a 0x0", BBB.normInf(), 0.0, verbose); numberFailedTests += PrintTestResults("normFrobenius of a 0x0", BBB.normFrobenius(), 0.0, verbose); // Multiplication Tests // Reset values of AAA. AAA(0, 0) = 8; AAA(1, 0) = 1; AAA(1, 1) = 8; AAA(2, 0) = 2; AAA(2, 1) = 3; AAA(2, 2) = 8; DMatrix My_Prod( 4, 3 ), My_GenMatrix( 4, 3 ); My_GenMatrix = Teuchos::ScalarTraits<STYPE>::one(); // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix ) My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = lower tri)", My_Prod.normOne(), 52.0, verbose); AAA.setUpper(); AAA(2, 1) = 1.0; My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, AAA, My_GenMatrix, 0.0 ); numberFailedTests += PrintTestResults("multiply() -- general times symmetric matrix (storage = upper tri)", My_Prod.normOne(), 44.0, verbose); // Set Method Tests. SDMatrix CCC( 5 ); // Randomize the entries in CCC. testName = "random() -- enter random entries into matrix"; returnCode = CCC.random(); numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); // Set the entries of CCC to 1.0. testName = "putScalar() -- set every entry of this matrix to 1.0"; returnCode = CCC.putScalar(Teuchos::ScalarTraits<STYPE>::one()); numberFailedTests += ReturnCodeCheck(testName, returnCode, 0, verbose); // Check assignment operator. SDMatrix CCC2( 5 ); CCC2.assign( CCC ); if (verbose) std::cout << "assign() -- copy the values of an input matrix "; if ( CCC( 3, 4 ) == Teuchos::ScalarTraits<STYPE>::one() ) { if (verbose) std::cout<< "successful" <<std::endl; } else { if (verbose) std::cout<< "unsuccessful" <<std::endl; numberFailedTests++; } // Create a view into a submatrix of CCC SDMatrix CCCview( Teuchos::View, CCC, 3 ); SDMatrix CCCtest1( 2 ); CCCtest1 = CCCview; if (verbose) std::cout << "operator= -- small(empty) = large(view) "; if (CCCtest1.numRows()==3 && CCCtest1.values()==CCC.values()) { if (verbose) std::cout<< "successful" <<std::endl; } else { if (verbose) std::cout<< "unsuccessful" <<std::endl; numberFailedTests++; } CCCtest1 = CCC; if (verbose) std::cout << "operator= -- small(view) = large(copy) "; if (CCCtest1.numRows()==5 && CCCtest1.values()!=CCC.values()) { if (verbose) std::cout<< "successful"<<std::endl; } else { if (verbose) std::cout<< "unsuccessful"<<std::endl; numberFailedTests++; } SDMatrix CCCtest2( 2 ); CCCtest2 = Teuchos::ScalarTraits<STYPE>::one(); CCCtest1 = CCCtest2; if (verbose) std::cout << "operator= -- large(copy) = small(copy) "; if (CCCtest1.numRows()==2 ) { if (verbose) std::cout<< "successful"<<std::endl; } else { if (verbose) std::cout<< "unsuccessful"<<std::endl; numberFailedTests++; } CCCtest1 = CCCview; if (verbose) std::cout << "operator= -- large(copy) = small(view) "; if (CCCtest1.numRows()==3 && CCCtest1.stride()==5) { if(verbose) std::cout<<"successful" <<std::endl; } else { if (verbose) std::cout<<"unsuccessful"<<std::endl; numberFailedTests++; } SDMatrix CCCtest3( CCCview ); CCCtest1 += CCCtest3; if (verbose) std::cout << "operator+= -- add two matrices of the same size, but different leading dimension "; if (CCCtest1(1,1)==2.0) { if(verbose) std::cout<<"successful" <<std::endl; } else { if (verbose) std::cout<<"unsuccessful"<<std::endl; numberFailedTests++; } if (verbose) std::cout << "operator+= -- add two matrices of different size (nothing should change) "; CCCtest1 += CCC; if (CCCtest1(1,1)==2.0) { if(verbose) std::cout<<"successful" <<std::endl; } else { if (verbose) std::cout<<"unsuccessful"<<std::endl; numberFailedTests++; } // Scale Tests. SDMatrix ScalTest( 8 ); ScalTest = Teuchos::ScalarTraits<STYPE>::one(); // Scale the entries by 8, it should be 8. // The matrix is lower triangular, by default, so check a lower triangular entry. if (verbose) std::cout << "operator*= -- scale matrix by some number "; ScalTest *= 8.0; if (ScalTest(7, 1) == 8.0) { if (verbose) std::cout<< "successful." <<std::endl; } else { if (verbose) std::cout<< "unsuccessful." <<std::endl; numberFailedTests++; } // Matrix Triple-Product Test STYPE alpha=0.5*Teuchos::ScalarTraits<STYPE>::one(); DMatrix W(3,2); SDMatrix A1(2), A2(3); A1(0,0) = 1.0, A1(1,1) = 2.0; A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00; W = Teuchos::ScalarTraits<STYPE>::one(); SDMatrix C1upper(3), C1lower(3), C2upper(2), C2lower(2); C1upper.setUpper(); C2upper.setUpper(); C1lower.setLower(); C2lower.setLower(); // Test all combinations of triple products. // These should return a matrix with 1.5 in all entries STYPE C1result = 1.5*Teuchos::ScalarTraits<STYPE>::one(); Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1upper ); Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::NO_TRANS, alpha, A1, W, C1lower ); // These should return a matrix with 3 in all entries STYPE C2result = 3.0*Teuchos::ScalarTraits<STYPE>::one(); Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2upper ); Teuchos::symMatTripleProduct<OTYPE,STYPE>( Teuchos::TRANS, alpha, A2, W, C2lower ); if (verbose) std::cout << "triple product -- compute C = W'*A*W or C = W*A*W' "; if (C1upper(2,1)==C1result && C1lower(1,2)==C1result && C2upper(1,0)==C2result && C2lower(0,1)==C2result) { if (verbose) std::cout<< "successful." <<std::endl; } else { if (verbose) std::cout<< "unsuccessful." <<std::endl; numberFailedTests++; } // // If a test failed output the number of failed tests. // if(numberFailedTests > 0) { if (verbose) { std::cout << "Number of failed tests: " << numberFailedTests << std::endl; std::cout << "End Result: TEST FAILED" << std::endl; return -1; } } if(numberFailedTests == 0) std::cout << "End Result: TEST PASSED" << std::endl; return 0; }