diff --git a/UnitTest_PZ/TestMatrix/TestMatrix.cpp b/UnitTest_PZ/TestMatrix/TestMatrix.cpp index 83f9eac7de..73f1d80721 100644 --- a/UnitTest_PZ/TestMatrix/TestMatrix.cpp +++ b/UnitTest_PZ/TestMatrix/TestMatrix.cpp @@ -107,6 +107,19 @@ Test MultiplyByScalar operation */ template void TestingMultiplyByScalar(int row, int col, int symmetric); + +template +void TestingAddOperator(int row, int col, int symmetric); +/* +Test Subtract operation +*/ +template +void TestingSubtractOperator(int row, int col, int symmetric); +/* +Test MultiplyByScalar operation +*/ +template +void TestingMultiplyByScalarOperator(int row, int col, int symmetric); /** * Check if the result is a identity matrix. */ @@ -363,6 +376,94 @@ template } } } +template + void AddOperator(){ + for (auto dim = 5; dim < 100; dim += 500) { + SECTION("TPZFMatrix"){ + TestingAddOperator, TVar>(10, dim, 0); + } + SECTION("TPZSFMatrix"){ + TestingAddOperator, TVar>(dim, dim, 1); + } + SECTION("TPZFBMatrix"){ + TestingAddOperator,TVar>(dim, dim, 0); + } + SECTION("TPZSBMatrix"){ + TestingAddOperator,TVar>(dim, dim,1); + } + SECTION("TPZFYsmpMatrix"){ + TestingAddOperator, TVar>(10, dim, 0); + } + SECTION("TPZSYsmpMatrix"){ + TestingAddOperator, TVar>(dim, dim, 1); + } + // SECTION("TPZSkylNSymMatrix"){ + // TestingAddOperator, TVar>(dim, dim, 0); + // } + SECTION("TPZSkylMatrix"){ + TestingAddOperator, TVar>(dim, dim, 1); + } + } + } + + template + void SubtractOperator(){ + for (auto dim = 5; dim < 100; dim += 500) { + SECTION("TPZFMatrix"){ + TestingSubtractOperator, TVar>(10, dim, 0); + } + SECTION("TPZSFMatrix"){ + TestingSubtractOperator, TVar>(dim, dim, 1); + } + SECTION("TPZFBMatrix"){ + TestingSubtractOperator,TVar>(dim, dim, 0); + } + SECTION("TPZSBMatrix"){ + TestingSubtractOperator,TVar>(dim, dim,1); + } + SECTION("TPZFYsmpMatrix"){ + TestingSubtractOperator, TVar>(10, dim, 0); + } + SECTION("TPZSYsmpMatrix"){ + TestingSubtractOperator, TVar>(dim, dim, 1); + } + // SECTION("TPZSkylNSymMatrix"){ + // TestingSubtractOperator, TVar>(dim, dim, 0); + // } + SECTION("TPZSkylMatrix"){ + TestingSubtractOperator, TVar>(dim, dim, 1); + } + } + } +template + void MultiplyByScalarOperator(){ + for (auto dim = 5; dim < 100; dim += 500) { + SECTION("TPZFMatrix"){ + TestingMultiplyByScalarOperator, TVar>(10, dim, 0); + } + SECTION("TPZSFMatrix"){ + TestingMultiplyByScalarOperator, TVar>(dim, dim, 1); + } + SECTION("TPZFBMatrix"){ + TestingMultiplyByScalarOperator,TVar>(dim, dim, 0); + } + SECTION("TPZSBMatrix"){ + TestingMultiplyByScalarOperator,TVar>(dim, dim,1); + } + SECTION("TPZFYsmpMatrix"){ + TestingMultiplyByScalarOperator, TVar>(10, dim, 0); + } + SECTION("TPZSYsmpMatrix"){ + TestingMultiplyByScalarOperator, TVar>(dim, dim, 1); + } + // SECTION("TPZSkylNSymMatrix"){ + // TestingMultiplyByScalarOperator, TVar>(dim, dim, 0); + // } + SECTION("TPZSkylMatrix"){ + TestingMultiplyByScalarOperator, TVar>(dim, dim, 1); + } + } + } template void MultiplyTranspose(){ @@ -623,6 +724,40 @@ TEMPLATE_TEST_CASE("MultiplyByScalar","[matrix_tests]", testmatrix::MultiplyByScalar(); } +TEMPLATE_TEST_CASE("AddOperator","[matrix_tests]", + float, + double, + long double, + std::complex, + std::complex, + std::complex + ) { + testmatrix::AddOperator(); +} + +TEMPLATE_TEST_CASE("SubtractOperator","[matrix_tests]", + float, + double, + long double, + std::complex, + std::complex, + std::complex + ) { + testmatrix::SubtractOperator(); +} + +TEMPLATE_TEST_CASE("MultiplyByScalarOperator","[matrix_tests]", + float, + double, + long double, + std::complex, + std::complex, + std::complex + ) { + testmatrix::MultiplyByScalarOperator(); +} + + TEMPLATE_TEST_CASE("Multiply transpose (REAL)","[matrix_tests]", float, double, @@ -1032,6 +1167,102 @@ void TestingMultiplyByScalar(int row, int col, int symmetric) { Catch::StringMaker::precision = oldPrecision; } +template +void TestingAddOperator(int row, int col, int symmetric) { + auto oldPrecision = Catch::StringMaker::precision; + Catch::StringMaker::precision = std::numeric_limits::max_digits10; + matx ma1; + ma1.AutoFill(row, col, symmetric); + //this is to ensure that they have the same sparsity pattern + matx ma2(ma1); + + + + TPZMatrix &&res = ma1 + ma2; + + bool check = true; + constexpr RTVar tol = [](){ + if constexpr (std::is_same_v) return (RTVar)10; + else return (RTVar)1; + }(); + for (int i = 0; i < col; i++) { + for (int j = 0; (j < col) && check; j++) { + CAPTURE(i,j,res.Get(i,j),(TVar)2*ma1.Get(i,j)); + if (!IsZero((res.Get(i, j)-(ma1.Get(i,j)+ma2.Get(i,j)))/tol)) { + check = false; + } + REQUIRE(check); + } + } + Catch::StringMaker::precision = oldPrecision; +} + +template +void TestingSubtractOperator(int row, int col, int symmetric) { + auto oldPrecision = Catch::StringMaker::precision; + Catch::StringMaker::precision = std::numeric_limits::max_digits10; + matx ma1; + ma1.AutoFill(row, col, symmetric); + //this is to ensure that they have the same sparsity pattern + matx ma2(ma1); + + + TPZMatrix&& res = ma1-ma2; + + bool check = true; + constexpr RTVar tol = [](){ + if constexpr (std::is_same_v) return (RTVar)10; + else return (RTVar)1; + }(); + for (int i = 0; i < col; i++) { + for (int j = 0; (j < col) && check; j++) { + CAPTURE(i,j,res.Get(i,j)); + if (!IsZero((res.Get(i, j)-(ma1.Get(i,j)-ma2.Get(i,j)))/tol)) { + check = false; + } + REQUIRE(check); + } + } + Catch::StringMaker::precision = oldPrecision; +} + +template +void TestingMultiplyByScalarOperator(int row, int col, int symmetric) { + auto oldPrecision = Catch::StringMaker::precision; + Catch::StringMaker::precision = std::numeric_limits::max_digits10; + matx ma1; + ma1.AutoFill(row, col, symmetric); + + const TVar val = [](){ + constexpr RTVar lower_bound = 0; + constexpr RTVar upper_bound = 1; + static std::uniform_real_distribution unif(lower_bound,upper_bound); + static std::default_random_engine re; + const TVar a_random = (TVar)unif(re); + return a_random; + }(); + + + TPZMatrix&& res = ma1 * val; + + bool check = true; + constexpr RTVar tol = [](){ + if constexpr (std::is_same_v) return (RTVar)10; + else return (RTVar)1; + }(); + for (int i = 0; i < col; i++) { + for (int j = 0; (j < col) && check; j++) { + CAPTURE(i,j,res.Get(i,j),ma1.Get(i,j)*val); + if (!IsZero((res.Get(i, j)-(ma1.Get(i,j)*val))/tol)) { + check = false; + } + REQUIRE(check); + } + } + Catch::StringMaker::precision = oldPrecision; +} + + template void TestingTransposeMultiply(int row, int col, int symmetric) { // ma times inv must to be a identity matrix