/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #include #include #include #include #include #include #include class TemplateMatrixTest: public CppUnit::TestFixture { public: template void initFromArray1() { const Matrix m {1, 2, 3, 4, 5, 6, 7, 8}; Matrix m2; m2 = {1, 2, 3, 4, 5, 6, 7, 8}; CPPUNIT_ASSERT_EQUAL(m, m2); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]); CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]); } template void initFromArray2() { const Matrix m(1, 2, 3, 4, 5, 6, 7, 8); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]); CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]); } template void initFromArray3() { const Matrix m({1, 2, 3, 4, 5, 6, 7, 8}); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]); CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]); } template void initFromOtherMatrix() { Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); m1[1][2] = 13; Matrix m2(m1); m1[0][2] = 16; m2[0][0] = 0; Matrix m3; m3 = m2; CPPUNIT_ASSERT_EQUAL(m2, m3); CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]); CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m1[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m1[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m1[1][1]); CPPUNIT_ASSERT_EQUAL((T)13, m1[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m1[1][3]); CPPUNIT_ASSERT_EQUAL((T)0, m2[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m2[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m2[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m2[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m2[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m2[1][1]); CPPUNIT_ASSERT_EQUAL((T)13, m2[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m2[1][3]); } template void initFromDefault() { const Matrix m; CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[1][1]); } template void access() { Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); for (size_t row(0); row<2; ++row) for (size_t column(0); column<4; ++column) CPPUNIT_ASSERT_EQUAL(m1(row, column), m1[row][column]); } template void equality() { Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); Matrix m2(1, 2, 3, 4, 5, 6, 7, 8); Matrix m3(1, 2, 3, 4, 5, 6, 7, 9); Matrix m4(9, 2, 3, 4, 5, 6, 7, 8); Matrix m5(1, 2, 0, 4, 5, 6, 7, 8); Matrix m6(-m1); Matrix m7(-1, -2, -3, -4, -5, -6, -7, -8); CPPUNIT_ASSERT_EQUAL(m7, m6); CPPUNIT_ASSERT(m1==m2); CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m4); CPPUNIT_ASSERT(m1!=m5); CPPUNIT_ASSERT(m1!=m6); } template void operator_plus() { const Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); const Matrix m2(2, 4, 6, 8, 1, 3, 5, 7); const Matrix m(m1+m2); const Matrix res(3, 6, 9, 12, 6, 9, 12, 15); CPPUNIT_ASSERT_EQUAL(res, m); } template void operator_minus() { const Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); const Matrix m2(2, 4, 6, 8, 1, 3, 5, 7); const Matrix m(m1-m2); const Matrix res(-1, -2, -3, -4, 4, 3, 2, 1); CPPUNIT_ASSERT_EQUAL(res, m); CPPUNIT_ASSERT_EQUAL(res, -m2+m1); } template void scalar_mult() { const Matrix m1(1, 2, 3, 4, 5, 6, 7, 8); T two(2), three(3), four(4), big(32); CPPUNIT_ASSERT_EQUAL(m1*two, m1+m1); CPPUNIT_ASSERT_EQUAL(m1*three, m1+m1+m1); CPPUNIT_ASSERT_EQUAL(m1*four, m1+m1+m1+m1); CPPUNIT_ASSERT_EQUAL(two*m1, m1*two); CPPUNIT_ASSERT_EQUAL(three*m1, m1*three); CPPUNIT_ASSERT_EQUAL(four*m1, m1*four); CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four); CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big); } template void scalar_div() { const Matrix m1(2, 4, 6, 8, 10, 12, 14, 16); const Matrix m2(1, 2, 3, 4, 5, 6, 7, 8); CPPUNIT_ASSERT_EQUAL(m2, m1/(T)2); CPPUNIT_ASSERT_EQUAL(m2, (T)3*m1/(T)6); } template void matrix_mult() { const Matrix m1(1, 2, 3, 4, 5, 6); const Matrix m2(1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12); const Matrix m(m1*m2); const Matrix res(14, 32, 50, 68, 32, 77, 122, 167); CPPUNIT_ASSERT_EQUAL(res, m); } template void transpose() { const Matrix m(1, 2, 3, 4, 5, 6); const Matrix res(1, 4, 2, 5, 3, 6); CPPUNIT_ASSERT_EQUAL(res, m.t()); } template void apply() { Matrix m(2, -2, 4, -2, 1, -6, 1, 0, -2); Matrix o(m); CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;})); } template void norm() { Matrix m1(1, -1, 1, -1, 1, -1, 1, 1, -1); Matrix m2(-1, 3, 2, -2, 0, -3, 2, 1, -2); Matrix m3(0, 3, 0, -1, 0, 2, 0, -3, 1); CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, m1.norm(), 0.00001); CPPUNIT_ASSERT_DOUBLES_EQUAL(6.0, m2.norm(), 0.00001); CPPUNIT_ASSERT_DOUBLES_EQUAL(4.0, m3.norm(3), 0.00001); } template void gauss() { Matrix m(2, -2, 4, -2, 1, -6, 1, 0, -2); const Matrix res(1, -1, 2, 0, -1, -2, 0, 0, -6); T lambda(m.gauss()); CPPUNIT_ASSERT_EQUAL(res, m); CPPUNIT_ASSERT_EQUAL((T)2, lambda); } template void det() { Matrix m(2, -2, 4, -2, 1, -6, 1, 0, -2); CPPUNIT_ASSERT_EQUAL((T)12, m.det()); } template void i() { const Matrix m1(1, 0, 0, 0, 1, 0, 0, 0, 1); const Matrix m2(1, 0, 0, 1, 0, 0); const Matrix m3(1, 0, 0, 0, 1, 0); CPPUNIT_ASSERT_EQUAL(m1, m1.i()); CPPUNIT_ASSERT_EQUAL(m2, m2.i()); CPPUNIT_ASSERT_EQUAL(m3, m3.i()); } template void inv() { { Matrix m(2, -1, 0, 1, 2, -2, 0, -1, 1); const Matrix res(0, 1, 2, -1, 2, 4, -1, 2, 5); Matrix o1(m), o2(m); m.inv(); CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1); CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(1, 2, 3, 0, 1, 4, 5, 6, 0); const Matrix res(-24, 18, 5, 20, -15, -4, -5, 4, 1); m.inv(); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(1, 2, 3, 0, 4, 5, 1, 0, 6); const Matrix res((T)12/11, (T)-6/11, (T)-1/11, (T)5/22, (T)3/22, (T)-5/22, (T)-2/11, (T)1/11, (T)2/11); Matrix o1(m), o2(m); m.inv(); CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1); CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(-2, 0, 1, 9, 2, -3, 5, 1, -2); const Matrix res(-1, 1, -2, 3, -1, 3, -1, 2, -4); Matrix o(m); m.inv(); CPPUNIT_ASSERT_EQUAL(m.i(), m*o); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(1, 3, 1, 1, 1, 2, 2, 3, 4); const Matrix res(2, 9, -5, 0, -2, 1, -1, -3, 2); Matrix o(m); m.inv(); CPPUNIT_ASSERT_EQUAL(m.i(), m*o); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(2, 1, 4, 1, -1, 1, 0, 2, 0, 0, 2, 4, 2, -2, 0, 1); const Matrix res((T)1/3, (T)13/15, (T)-2/3, 0.6, (T)1/3, (T)16/15, (T)-2/3, 0.2, 0, -0.8, 0.5, -0.4, 0, 0.4, 0, 0.2); Matrix o(m); m.inv(); CPPUNIT_ASSERT_EQUAL(m.i(), m*o); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(4, 3, 3, 2); const Matrix res(-2, 3, 3, -4); m.inv(); CPPUNIT_ASSERT_EQUAL(res, m); } } template void stream() { const Matrix m1(1, 2, 3, 4, 5, 6, 7, 8, 1, 4, 2, 8); Matrix m2; std::string res("[3x4]{1,2,3,4,5,6,7,8,1,4,2,8}"); std::stringstream ss; ss<>m2; CPPUNIT_ASSERT_EQUAL(m1, m2); } CPPUNIT_TEST_SUITE(TemplateMatrixTest); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromArray3); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(inv); CPPUNIT_TEST(inv); CPPUNIT_TEST(inv); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest); class VariableMatrixTest: public CppUnit::TestFixture { public: template void initFromArray1() { Matrix m(2,4, 1, 2, 3, 4, 5, 6, 7, 8); Matrix m2(2, 4); m2 = {2, 4, 1, 2, 3, 4, 5, 6, 7, 8}; CPPUNIT_ASSERT_EQUAL(m, m2); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]); CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]); } template void initFromArray2() { Matrix m(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]); CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]); } template void initFromOtherMatrix() { Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); m1[1][2] = 13; Matrix m2(m1); m1[0][2] = 16; m2[0][0] = 0; Matrix m3(2, 4); m3 = m2; CPPUNIT_ASSERT_EQUAL(m2, m3); CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]); CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m1[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m1[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m1[1][1]); CPPUNIT_ASSERT_EQUAL((T)13, m1[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m1[1][3]); CPPUNIT_ASSERT_EQUAL((T)0, m2[0][0]); CPPUNIT_ASSERT_EQUAL((T)2, m2[0][1]); CPPUNIT_ASSERT_EQUAL((T)3, m2[0][2]); CPPUNIT_ASSERT_EQUAL((T)4, m2[0][3]); CPPUNIT_ASSERT_EQUAL((T)5, m2[1][0]); CPPUNIT_ASSERT_EQUAL((T)6, m2[1][1]); CPPUNIT_ASSERT_EQUAL((T)13, m2[1][2]); CPPUNIT_ASSERT_EQUAL((T)8, m2[1][3]); } template void initFromDefault() { Matrix m(2, 4); CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[1][1]); } template void access() { Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); for (size_t row(0); row<2; ++row) for (size_t column(0); column<4; ++column) CPPUNIT_ASSERT_EQUAL(m1(row, column), m1[row][column]); } template void equality() { Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); Matrix m2(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); Matrix m3(2, 4, 1, 2, 3, 4, 5, 6, 7, 9); Matrix m4(2, 4, 9, 2, 3, 4, 5, 6, 7, 8); Matrix m5(-m1); Matrix m6(2, 4, -1, -2, -3, -4, -5, -6, -7, -8); CPPUNIT_ASSERT_EQUAL(m6, m5); CPPUNIT_ASSERT(m1==m2); CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m4); CPPUNIT_ASSERT(m1!=m5); } template void operator_plus() { const Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); const Matrix m2(2, 4, 2, 4, 6, 8, 1, 3, 5, 7); const Matrix m(m1+m2); const Matrix res(2, 4, 3, 6, 9, 12, 6, 9, 12, 15); CPPUNIT_ASSERT_EQUAL(res, m); } template void operator_minus() { const Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); const Matrix m2(2, 4, 2, 4, 6, 8, 1, 3, 5, 7); const Matrix m(m1-m2); const Matrix res(2, 4, -1, -2, -3, -4, 4, 3, 2, 1); CPPUNIT_ASSERT_EQUAL(res, m); CPPUNIT_ASSERT_EQUAL(res, -m2+m1); } template void scalar_mult() { const Matrix m1(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); T two(2), three(3), four(4), big(32); CPPUNIT_ASSERT_EQUAL(m1*two, m1+m1); CPPUNIT_ASSERT_EQUAL(m1*three, m1+m1+m1); CPPUNIT_ASSERT_EQUAL(m1*four, m1+m1+m1+m1); CPPUNIT_ASSERT_EQUAL(two*m1, m1*two); CPPUNIT_ASSERT_EQUAL(three*m1, m1*three); CPPUNIT_ASSERT_EQUAL(four*m1, m1*four); CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four); CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big); } template void scalar_div() { const Matrix m1(2, 4, 2, 4, 6, 8, 10, 12, 14, 16); const Matrix m2(2, 4, 1, 2, 3, 4, 5, 6, 7, 8); CPPUNIT_ASSERT_EQUAL(m2, m1/(T)2); CPPUNIT_ASSERT_EQUAL(m2, (T)3*m1/(T)6); } template void matrix_mult() { const Matrix m1(2, 3, 1, 2, 3, 4, 5, 6); const Matrix m2(3, 4, 1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12); const Matrix m(m1*m2); const Matrix res(2, 4, 14, 32, 50, 68, 32, 77, 122, 167); CPPUNIT_ASSERT_EQUAL(res, m); } template void transpose() { const Matrix m(2, 3, 1, 2, 3, 4, 5, 6); const Matrix res(3, 2, 1, 4, 2, 5, 3, 6); CPPUNIT_ASSERT_EQUAL(res, m.t()); } template void apply() { Matrix m(3, 3, 2, -2, 4, -2, 1, -6, 1, 0, -2); Matrix o(m); CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;})); } template void norm() { Matrix m1(3, 3, 1, -1, 1, -1, 1, -1, 1, 1, -1); Matrix m2(3, 3, -1, 3, 2, -2, 0, -3, 2, 1, -2); Matrix m3(3, 3, 0, 3, 0, -1, 0, 2, 0, -3, 1); CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, m1.norm(), 0.00001); CPPUNIT_ASSERT_DOUBLES_EQUAL(6.0, m2.norm(), 0.00001); CPPUNIT_ASSERT_DOUBLES_EQUAL(4.0, m3.norm(3), 0.00001); } template void gauss() { Matrix m(3, 3, 2, -2, 4, -2, 1, -6, 1, 0, -2); const Matrix res(3,3, 1, -1, 2, 0, -1, -2, 0, 0, -6); T lambda(m.gauss()); CPPUNIT_ASSERT_EQUAL(res, m); CPPUNIT_ASSERT_EQUAL((T)2, lambda); } template void det() { Matrix m(3, 3, 2, -2, 4, -2, 1, -6, 1, 0, -2); CPPUNIT_ASSERT_EQUAL((T)12, m.det()); } template void i() { const Matrix m1(3, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1); const Matrix m2(3, 2, 1, 0, 0, 1, 0, 0); const Matrix m3(2, 3, 1, 0, 0, 0, 1, 0); CPPUNIT_ASSERT_EQUAL(m1, m1.i()); CPPUNIT_ASSERT_EQUAL(m2, m2.i()); CPPUNIT_ASSERT_EQUAL(m3, m3.i()); } template void inv() { { Matrix m(3, 3, 2, -1, 0, 1, 2, -2, 0, -1, 1); const Matrix res(3, 3, 0, 1, 2, -1, 2, 4, -1, 2, 5); Matrix o1(m), o2(m); m.inv(); CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1); CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(3, 3, 1, 2, 3, 0, 1, 4, 5, 6, 0); const Matrix res(3, 3, -24, 18, 5, 20, -15, -4, -5, 4, 1); m.inv(); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(3, 3, 1, 2, 3, 0, 4, 5, 1, 0, 6); const Matrix res(3, 3, (T)12/11, (T)-6/11, (T)-1/11, (T)5/22, (T)3/22, (T)-5/22, (T)-2/11, (T)1/11, (T)2/11); m.inv(); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(3, 3, -2, 0, 1, 9, 2, -3, 5, 1, -2); const Matrix res(3, 3, -1, 1, -2, 3, -1, 3, -1, 2, -4); Matrix o(m); m.inv(); CPPUNIT_ASSERT_EQUAL(m.i(), m*o); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(4, 4, 2, 1, 4, 1, -1, 1, 0, 2, 0, 0, 2, 4, 2, -2, 0, 1); const Matrix res(4, 4, (T)1/3, (T)13/15, (T)-2/3, 0.6, (T)1/3, (T)16/15, (T)-2/3, 0.2, 0, -0.8, 0.5, -0.4, 0, 0.4, 0, 0.2); Matrix o(m); m.inv(); CPPUNIT_ASSERT_EQUAL(m.i(), m*o); CPPUNIT_ASSERT_EQUAL(res, m); } { Matrix m(2, 2, 4, 3, 3, 2); const Matrix res(2, 2, -2, 3, 3, -4); m.inv(); CPPUNIT_ASSERT_EQUAL(res, m); } } template void stream() { const Matrix m1(3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 4, 2, 8); Matrix m2(1, 1); std::string res("[3x4]{1,2,3,4,5,6,7,8,1,4,2,8}"); std::stringstream ss; ss<>m2; CPPUNIT_ASSERT_EQUAL(m1, m2); } CPPUNIT_TEST_SUITE(VariableMatrixTest); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromArray2); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(initFromOtherMatrix); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(access); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(equality); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_plus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(operator_minus); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_mult); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(scalar_div); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(matrix_mult); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(transpose); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(apply); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(norm); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(gauss); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(det); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(i); CPPUNIT_TEST(inv); CPPUNIT_TEST(inv); CPPUNIT_TEST(inv); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST(stream); CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest); int main(int argc, char** argv) try { std::ofstream ofs((*argv+std::string(".xml")).c_str()); CppUnit::TextUi::TestRunner runner; runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs)); runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); return runner.run() ? 0 : 1; } catch (std::exception& e) { std::cerr<<"***Exception: "<