/* * ===================================================================================== * * 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 TestNegate() { TestInit1(); smat_->Negate(); for(index_t i=0; iget(i,j), -mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("Test Negate success!!"); } void TestColumnScale() { TestInit1(); Vector scale; scale.Init(num_of_cols_); for(index_t i=0; iEndLoading(); smat_->ColumnScale(scale); for(index_t i=0; iget(i,j), j*mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("Test ColumnScale success!!"); } void TestRowScale() { TestInit1(); Vector scale; scale.Init(num_of_cols_); for(index_t i=0; iEndLoading(); smat_->RowScale(scale); for(index_t i=0; iget(i,j), i*mat_[i][j], std::numeric_limits::epsilon()); } } NOTIFY("Test RowScale success!!"); } void TestRowSums() { TestInit1(); Vector row_sums; smat_->EndLoading(); smat_->RowSums(&row_sums); for(index_t i=0; iEndLoading(); smat_->InvRowSums(&row_sums); for(index_t i=0; i::epsilon()); } NOTIFY("Test InvRowSums success!!"); } void TestInvColMaxs() { TestInit1(); Vector col_maxs; smat_->EndLoading(); smat_->InvColMaxs(&col_maxs); for(index_t i=0; i::epsilon()); } NOTIFY("Test InvColMaxs success!!"); } 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; Sparsem::Add(a, b, &temp); temp.EndLoading(); // a_plus_b.EndLoading(); // printf("%s\n", temp.Print().c_str()); // printf("%s\n", a_plus_b.Print().c_str()); 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"); Sparsem::Subtract(a, b, &temp); temp.EndLoading(); a_minus_b.EndLoading(); 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"); Sparsem::Multiply(a, b, &temp); // printf("%s\n", temp.Print().c_str()); // printf("%s\n", a_times_b.Print().c_str()); temp.EndLoading(); a_times_b.EndLoading(); 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"); Sparsem::DotMultiply(a, b, &temp); // printf("%s\n", temp.Print().c_str()); // printf("%s\n", a_dot_times_b.Print().c_str()); temp.EndLoading(); a_dot_times_b.EndLoading(); 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(); TestNegate(); Destruct(); Init(); TestColumnScale(); Destruct(); Init(); TestRowScale(); Destruct(); Init(); TestRowSums(); Destruct(); Init(); TestInvRowSums(); Destruct(); Init(); TestInvColMaxs(); 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(); }