void TestMatrixExtras::test_add_DD() { size_t n = 10; size_t m = 15; size_t repetitions = 20; for (size_t r = 0; r < repetitions; r++) { Matrix A = MatrixFactory::MakeRandomMatrix(n, m, 2.0, 1.0); Matrix B = MatrixFactory::MakeRandomMatrix(n, m, 2.0, 1.0); double alpha = 2.0 * static_cast<double> (std::rand()) / static_cast<double> (RAND_MAX); double gamma = -0.5 + static_cast<double> (std::rand()) / static_cast<double> (RAND_MAX); Matrix A_copy(A); Matrix B_copy(B); // A = gamma*A + alpha*B int status = Matrix::add(A, alpha, B, gamma); _ASSERT_EQ(ForBESUtils::STATUS_OK, status); _ASSERT_EQ(B_copy, B); A_copy *= gamma; B_copy *= alpha; Matrix C = A_copy + B_copy; _ASSERT_EQ(C, A); } }
void TestMatrixExtras::test_add_STST() { size_t repetitions = 300; for (size_t r = 0; r < repetitions; r++) { size_t n = 10; size_t m = 15; size_t nnz = 70; Matrix A = MatrixFactory::MakeRandomSparse(n, m, nnz, 2.0, 1.0); Matrix B = MatrixFactory::MakeRandomSparse(n, m, nnz, 2.0, 1.0); double alpha = 2.0 * static_cast<float> (std::rand()) / static_cast<float> (RAND_MAX); double gamma = -0.5 + static_cast<float> (std::rand()) / static_cast<float> (RAND_MAX); Matrix A_copy(A); Matrix B_copy(B); A.transpose(); B.transpose(); int status = Matrix::add(A, alpha, B, gamma); // A' = gamma*A' + alpha*B _ASSERT_EQ(ForBESUtils::STATUS_OK, status); _ASSERT_EQ(m, A.getNrows()); _ASSERT_EQ(n, A.getNcols()); for (size_t i = 0; i < m; i++) { for (size_t j = 0; j < n; j++) { _ASSERT_EQ(gamma * A_copy.get(j, i) + alpha * B_copy.get(j, i), A.get(i, j)); } } } }
void TestMatrixExtras::test_add_DST() { for (size_t n = 11; n < 40; n += 5) { for (size_t m = 13; m < 30; m += 3) { size_t nnz = 100; Matrix A = MatrixFactory::MakeRandomMatrix(n, m, 2.0, 1.0); Matrix B = MatrixFactory::MakeRandomSparse(m, n, nnz, 2.0, 1.0); Matrix A_copy(A); Matrix B_copy(B); B.transpose(); double alpha = -1.0 + 2.0 * static_cast<double> (std::rand()) / static_cast<double> (RAND_MAX); double gamma = -0.5 + static_cast<double> (std::rand()) / static_cast<double> (RAND_MAX); const double tol = 1e-8; int status = Matrix::add(A, alpha, B, gamma); // A = gamma * A + alpha * B _ASSERT_EQ(ForBESUtils::STATUS_OK, status); _ASSERT_EQ(n, A.getNrows()); for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < m; j++) { _ASSERT_NUM_EQ(gamma * A_copy.get(i, j) + alpha * B_copy.get(j, i), A.get(i, j), tol); } } } } }
MatrixViewType top_block (const MatrixViewType& A, const bool contiguous_cache_blocks) const { typedef typename MatrixViewType::ordinal_type ordinal_type; const ordinal_type nrows_top = strategy_.top_block_split_nrows (A.nrows(), ncols(), nrows_cache_block()); MatrixViewType A_copy (A); return A_copy.split_top (nrows_top, contiguous_cache_blocks); }
MatrixViewType top_block (const MatrixViewType& A, const bool contiguous_cache_blocks) const { typedef typename MatrixViewType::ordinal_type ordinal_type; // Ignore the number of columns in A, since we want to block all // matrices using the same cache blocking strategy. const ordinal_type nrows_top = strategy_.top_block_split_nrows (A.nrows(), ncols(), nrows_cache_block()); MatrixViewType A_copy (A); return A_copy.split_top (nrows_top, contiguous_cache_blocks); }
int main() { //Test some constructors Vector<int> A_size(10); Vector<int> A_default; Vector<int> B_default; std::initializer_list<int> list; Vector<int> A_list(list); Vector<int> A_copy(A_default); for(int i = 0; i < 10; i++) { A_default.push_back(i); } return 0; }
void TestMatrixExtras::test_add_SST() { size_t n = 10; size_t m = 15; size_t nnz = std::ceil(n * m / 2); size_t repetitions = 300; for (size_t r = 0; r < repetitions; r++) { Matrix A = MatrixFactory::MakeRandomSparse(n, m, nnz, 2.0, 1.0); Matrix B = MatrixFactory::MakeRandomSparse(m, n, nnz, 2.0, 1.0); double alpha = 2.0 * static_cast<float> (std::rand()) / static_cast<float> (RAND_MAX); double gamma = -0.5 + static_cast<float> (std::rand()) / static_cast<float> (RAND_MAX); Matrix B_copy(B); B.transpose(); Matrix A_copy(A); int status = Matrix::add(A, alpha, B, gamma); // A = gamma*A + alpha*B' _ASSERT_EQ(ForBESUtils::STATUS_OK, status); _ASSERT_EQ(n, A.getNrows()); _ASSERT_EQ(m, A.getNcols()); for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < m; j++) { _ASSERT_NUM_EQ(gamma * A_copy.get(i, j) + alpha * B_copy.get(j, i), A.get(i, j), 1e-8); } } Matrix S(A_copy); S *= gamma; Matrix T(B); T *= alpha; S += T; _ASSERT_EQ(S, A); } }