/***********************************************************************//** * @brief Test matrix copy ***************************************************************************/ void TestGSparseMatrix::copy_matrix(void) { // Copy matrix GSparseMatrix test = m_test; // Test if original and compied matrices are correct test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test), "Test matrix copy operator", "Found:\n"+test.print()+"\nExpected:\n"+m_test.print()); // Return return; }
/***********************************************************************//** * @brief Test matrix arithmetics * * Tests matrix arithmetics. ***************************************************************************/ void TestGSparseMatrix::matrix_arithmetics(void) { // -GSparseMatrix GSparseMatrix test = -m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, -1.0, 0.0), "Test -GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix += GSparseMatrix test = m_test; test += m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 2.0, 0.0), "Test GSparseMatrix += GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix -= GSparseMatrix test = m_test; test -= m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 0.0, 0.0), "Test GSparseMatrix -= GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix *= 3.0 test = m_test; test *= 3.0; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 3.0, 0.0), "Test GSparseMatrix *= 3.0", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix /= 3.0 test = m_test; test /= 3.0; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 1.0/3.0, 0.0), "Test GSparseMatrix /= 3.0", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix + GSparseMatrix test = m_test + m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 2.0, 0.0), "Test GSparseMatrix + GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix - GSparseMatrix test = m_test - m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 0.0, 0.0), "Test GSparseMatrix - GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix * 3.0 test = m_test * 3.0; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 3.0, 0.0), "Test GSparseMatrix * 3.0", "Unexpected result matrix:\n"+test.print()); // 3.0 * GSparseMatrix test = 3.0 * m_test; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 3.0, 0.0), "Test 3.0 * GSparseMatrix", "Unexpected result matrix:\n"+test.print()); // GSparseMatrix / 3.0 test = m_test / 3.0; test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test, 1.0/3.0, 0.0), "Test GSparseMatrix / 3.0", "Unexpected result matrix:\n"+test.print()); // Test invalid matrix addition test_try("Test invalid matrix addition"); try { test = m_test; test += m_bigger; test_try_failure("Expected GException::matrix_mismatch exception."); } catch (GException::matrix_mismatch &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Return return; }
/***********************************************************************//** * @brief Test value assignment ***************************************************************************/ void TestGSparseMatrix::assign_values(void) { // Setup 3x3 matrix GSparseMatrix test(3,3); // Assignment individual values for (int i = 0; i < 3; ++i) { for (int k = 0; k < 3; ++k) { double value = i*2.0 + k*2.0; test(i,k) = value; } } // Check assignment of individual values for (int i = 0; i < 3; ++i) { for (int k = 0; k < 3; ++k) { double value = i*2.0 + k*2.0; test_value(test(i,k), value, 1.0e-10, "Test matrix element assignment"); } } // Verify range checking #ifdef G_RANGE_CHECK test_try("Verify range checking"); try { test(3,3) = 1.0; test_try_failure("Expected GException::out_of_range exception."); } catch (GException::out_of_range &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } #endif // Setup 10x10 matrix and keep for reference GSparseMatrix sparse(10,10); for (int i = 3; i < 5; ++i) { sparse(i,i) = 5.0; } GSparseMatrix initial = sparse; GSparseMatrix reference = sparse; // Insert column into 10 x 10 matrix using large matrix stack and the // add_col(GVector) method sparse.stack_init(100,50); GVector column(10); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int i_min = (j < 2) ? 0 : j-2; int i_max = (j > 8) ? 10 : j+2; column = 0.0; for (int i = i_min; i < i_max; ++i) { column[i] = (i+1)*1; reference(i,col) += column[i]; } sparse.add_col(column, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with large stack using add_col(GVector) method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using small matrix stack and the // add_col(GVector) method sparse = initial; sparse.stack_init(100,3); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int i_min = (j < 2) ? 0 : j-2; int i_max = (j > 8) ? 10 : j+2; column = 0.0; for (int i = i_min; i < i_max; ++i) { column[i] = (i+1)*1; } sparse.add_col(column, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with small stack using add_col(GVector) method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using tiny matrix stack and the // add_col(GVector) method sparse = initial; sparse.stack_init(8,3); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int i_min = (j < 2) ? 0 : j-2; int i_max = (j > 8) ? 10 : j+2; column = 0.0; for (int i = i_min; i < i_max; ++i) { column[i] = (i+1)*1; } sparse.add_col(column, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with tiny stack using add_col(GVector) method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using no matrix stack and the // add_col(GVector) method sparse = initial; for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int i_min = (j < 2) ? 0 : j-2; int i_max = (j > 8) ? 10 : j+2; column = 0.0; for (int i = i_min; i < i_max; ++i) { column[i] = (i+1)*1; } sparse.add_col(column, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test fill using add_col(GVector) method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Set-up workspace for compressed column adding double* wrk_data = new double[10]; int* wrk_row = new int[10]; // Compressed tests test_try("Verify compressed column add_col() method"); try { // Insert column into 10 x 10 matrix using large matrix stack and the // compressed column add_col() method sparse = initial; reference = initial; sparse.stack_init(100,50); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int inx = 0; int i_min = (j < 3) ? 0 : j-3; int i_max = (j > 8) ? 10 : j+2; for (int i = i_min; i < i_max; ++i) { wrk_data[inx] = (i+1)*3.7; wrk_row[inx] = i; reference(i,col) += wrk_data[inx]; inx++; } sparse.add_col(wrk_data, wrk_row, inx, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with large stack using compressed add_col() method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using small matrix stack and the // compressed column add_col() method sparse = initial; sparse.stack_init(100,2); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int inx = 0; int i_min = (j < 3) ? 0 : j-3; int i_max = (j > 8) ? 10 : j+2; for (int i = i_min; i < i_max; ++i) { wrk_data[inx] = (i+1)*3.7; wrk_row[inx] = i; inx++; } sparse.add_col(wrk_data, wrk_row, inx, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with small stack using compressed add_col() method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using tiny matrix stack and the // compressed column add_col() method sparse = initial; sparse.stack_init(3,2); for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int inx = 0; int i_min = (j < 3) ? 0 : j-3; int i_max = (j > 8) ? 10 : j+2; for (int i = i_min; i < i_max; ++i) { wrk_data[inx] = (i+1)*3.7; wrk_row[inx] = i; inx++; } sparse.add_col(wrk_data, wrk_row, inx, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test stack fill with tiny stack using compressed add_col() method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Insert column into 10 x 10 matrix using no matrix stack and the // compressed column add_col() method sparse = initial; for (int j = 0; j < 10; ++j) { int col = int(0.8 * j + 0.5); // This allows that some columns are twice if (col > 9) col -= 10; // This avoids overflow int inx = 0; int i_min = (j < 3) ? 0 : j-3; int i_max = (j > 8) ? 10 : j+2; for (int i = i_min; i < i_max; ++i) { wrk_data[inx] = (i+1)*3.7; wrk_row[inx] = i; inx++; } sparse.add_col(wrk_data, wrk_row, inx, col); } sparse.stack_destroy(); test_assert((sparse == reference), "Test fill using compressed add_col() method", "Found:\n"+sparse.print()+"\nExpected:\n"+reference.print()); // Signal success test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Free workspace delete [] wrk_data; delete [] wrk_row; // Return return; }