#include "catch/catch.hpp" #include #include "JMath/JMatrixND.hh" using Catch::Matchers::Contains; // a nice "substr" checker TEST_CASE( "JMatrixND class", "[math]" ) { // This is our setup, which is executed before each SECTION size_t n = 4; JMATH::JMatrixND A(n); JMATH::JMatrixND B(n); for(size_t i = 0; i < n*n; ++i) { // A = [[0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15]] A.at(i/n, i%n) = i; // B = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] B.at(i/n, i%n) = i + 1; } // The actual tests: SECTION("setIdentity sets diagonals to 1 and everything else to 0") { A.setIdentity(); for(size_t i = 0; i != n; ++i) { REQUIRE( A.at(i, i) == 1 ); // diagonals for(size_t j = 0; j != i; ++j) { REQUIRE( A.at(i, j) == 0 ); // everything else } } } SECTION("multiplication with a scalar") { int s = 3; JMATH::JMatrixND C = A * s; for (size_t i = 0; i != C.size(); ++i) { for (size_t j = 0; j != C.size(); ++j) { REQUIRE( C.at(i, j) == A.at(i, j) * s ); } } } SECTION("multiplication with a matrix") { JMATH::JMatrixND C = A * B; // not a complete check, but it's a demo REQUIRE( C.at(0, 0) == Approx(62) ); // float comparisons, epsilon can be adjusted REQUIRE( C.at(3, 3) == Approx(560) ); REQUIRE( C.at(2, 0) == Approx(286) ); } SECTION("multiplication of two matrices with incompatible dimensions throws") { JMATH::JMatrixND C(n*2); REQUIRE_THROWS_WITH( A * C, Contains("inconsistent") && Contains("dimensions") ); } SECTION("addition of two matrices with incompatible dimensions throws") { JMATH::JMatrixND C(n*2); REQUIRE_THROWS_WITH( A + C, Contains("inconsistent") && Contains("dimensions") ); } /* SECTION("determinant") { JMATH::JMatrixND C(3); C.at(0, 0) = 6; C.at(0, 1) = 1; C.at(0, 2) = 1; C.at(1, 0) = 4; C.at(1, 1) = -2; C.at(1, 2) = 5; C.at(2, 0) = 2; C.at(2, 1) = 8; C.at(2, 2) = 7; REQUIRE( C.getDeterminant() == Approx(-306) ); } SECTION("determinant throws when zero pivot") { REQUIRE_THROWS_WITH( A.getDeterminant(), Contains("zero pivot") ); } */ }