/* * ===================================================================================== * * Filename: sparse_matrix_test.cc * * Description: * * Version: 1.0 * Created: 12/08/2007 03:50:44 PM EST * Revision: none * Compiler: gcc * * Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org * Company: Georgia Tech Fastlab-ESP Lab * * ===================================================================================== */ #include #include #include #include #include #include #include #include "fastlib/fastlib.h" #include "base/test.h" #include "sparse/sparse_matrix.h" class SparseMatrixTest { public: SparseMatrixTest() { } ~SparseMatrixTest() { } void Init() { for(index_t i=0; iStartLoadingRows(); std::vector ind; std::vector val; for(index_t i=0; iLoadRow(i, ind, val); } for(index_t i=0; iget(i,j), mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("TestInit1 sucess!!\n"); } void TestInit2() { std::vector rows; std::vector cols; std::vector vals; std::vector nnz(num_of_rows_); for(index_t i=0; iInit(rows, cols, vals, *(std::max_element(nnz.begin(), nnz.end())), num_of_rows_); // printf("%s\n", smat_->Print().c_str()); for(index_t i=0; iget(i,j), mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("TestInit2 success!!\n"); } void TestInit3() { FILE *fp = fopen("temp.txt", "w"); if (fp==NULL) { FATAL("Cannot open temp.txt error %s", strerror(errno)); } for(index_t i=0; iInit("temp.txt"); unlink("temp.txt"); for(index_t i=0; iget(i,j), mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("TestInit3 success!!"); } void TestCopyConstructor() { smat_ = new SparseMatrix(); NOTIFY("TestCopyConstructor success!!\n"); } void TestMakeSymmetric() { TestInit1(); smat_->set(2, 3, 1.44); smat_->set(3, 2, 0.74); smat_->set(7, 8, 4.33); smat_->set(8, 7, 0.22); smat_->MakeSymmetric(); for(index_t i=0; iget(i,j), smat_->get(j, i), std::numeric_limits::epsilon()); } } NOTIFY("Test MakeSymmetric success!!\n"); } void TestEig() { TestInit1(); smat_->EndLoading(); Vector eigvalues_real; Vector eigvalues_imag; Matrix eigvectors; smat_->Eig(1, "LM", &eigvectors, &eigvalues_real, &eigvalues_imag); eigvectors.PrintDebug(); NOTIFY("Test Eigenvector success!!\n"); } void TestLinSolve() { TestInit1(); Vector b,x; b.Init(num_of_cols_); b.SetZero(); x.Init(num_of_cols_); x.SetAll(1); smat_->MakeSymmetric(); smat_->EndLoading(); smat_->LinSolve(b, &x); x.PrintDebug(); NOTIFY("Test Linear Solve success!!\n"); } void TestBasicOperations() { SparseMatrix a("A.txt"); SparseMatrix b("B.txt"); SparseMatrix a_plus_b("AplusB.txt"); SparseMatrix a_minus_b("AminusB.txt"); SparseMatrix a_times_b("AtimesB.txt"); SparseMatrix a_dot_times_b("AdottimesB.txt"); SparseMatrix temp; temp.Init(21,21, 3); Sparsem::Add(a, b, &temp); for(index_t i=0; i<20; i++) { for(index_t j=0; j<20; j++) { TEST_DOUBLE_APPROX(a_plus_b.get(i,j), temp.get(i,j), 0.01); } } temp.Destruct(); NOTIFY("Matrix addition sucess!!\n"); temp.Init(21,21, 3); Sparsem::Subtract(a, b, &temp); for(index_t i=0; i<21; i++) { for(index_t j=0; j<21; j++) { TEST_DOUBLE_APPROX(a_minus_b.get(i,j), temp.get(i,j), 0.01); } } temp.Destruct(); NOTIFY("Matrix subtraction success!!\n"); temp.Init(21,21, 3); Sparsem::Multiply(a, b, &temp); for(index_t i=0; i<21; i++) { for(index_t j=0; j<21; j++) { TEST_DOUBLE_APPROX(a_times_b.get(i,j), temp.get(i,j), 0.01); } } temp.Destruct(); NOTIFY("Matrix multiplication success!!\n"); temp.Init(21, 21, 3); Sparsem::DotMultiply(a, b, &temp); for(index_t i=0; i::epsilon()); } } temp.Destruct(); NOTIFY("Matrix scalar multiplicationn success!!\n"); } void TestAll() { Init(); TestInit1(); Destruct(); Init(); TestInit2(); Destruct(); Init(); TestInit3(); Destruct(); Init(); TestCopyConstructor(); Destruct(); Init(); TestMakeSymmetric(); Destruct(); Init(); TestEig(); Destruct(); Init(); TestLinSolve(); Destruct(); Init(); TestBasicOperations(); } private: SparseMatrix *smat_; static const index_t num_of_cols_ = 80; static const index_t num_of_rows_ = 80; static const index_t num_of_nnz_ = 4; double mat_[num_of_rows_][num_of_cols_]; std::vector indices_; std::vector rows_; }; int main() { SparseMatrixTest test; test.TestAll(); }