diff --git a/COPYING b/COPYING index 88798ab..caeca07 120000 --- a/COPYING +++ b/COPYING @@ -1 +1 @@ -/usr/share/automake-1.15/COPYING \ No newline at end of file +/usr/share/automake-1.14/COPYING \ No newline at end of file diff --git a/INSTALL b/INSTALL index ddcdb76..f812f5a 120000 --- a/INSTALL +++ b/INSTALL @@ -1 +1 @@ -/usr/share/automake-1.15/INSTALL \ No newline at end of file +/usr/share/automake-1.14/INSTALL \ No newline at end of file diff --git a/ax_cxx_compile_stdcxx_11.m4 b/ax_cxx_compile_stdcxx_11.m4 index e3fc158..e3c8d75 100644 --- a/ax_cxx_compile_stdcxx_11.m4 +++ b/ax_cxx_compile_stdcxx_11.m4 @@ -201,15 +201,16 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX_14], [dnl AC_MSG_ERROR([*** A compiler with support for C++14 language features is required.]) fi else + HAVE_CXX11=${HAVE_CXX14} if test x$ac_success = xno; then HAVE_CXX14=0 AC_MSG_NOTICE([No compiler with C++14 support was found]) + AX_CXX_COMPILE_STDCXX_11([$1], [optional]) else HAVE_CXX14=1 AC_DEFINE(HAVE_CXX14,1, [define if the compiler supports basic C++14 syntax]) fi - HAVE_CXX11=${HAVE_CXX14} AC_SUBST(HAVE_CXX11) AC_SUBST(HAVE_CXX14) fi diff --git a/ax_init_standard_project.m4 b/ax_init_standard_project.m4 index 4128d9f..04f89ac 100644 --- a/ax_init_standard_project.m4 +++ b/ax_init_standard_project.m4 @@ -129,7 +129,7 @@ AC_DEFUN([AX_SUBST], [ # m4_define(x_minor, MINOR_NUMBER) # project's minor version # m4_include(ax_init_standard_project.m4) # AC_INIT(x_package_name, x_version, x_bugreport, x_package_name) -# AM_INIT_AUTOMAKE([1.9 tar-pax]) +# AM_INIT_AUTOMAKE([1.9 tar-pax parallel-tests color-tests]) # AX_INIT_STANDARD_PROJECT # # you change nothing but: YOUR_PACKAGE_NAME, MAJOR_NUMBER, MINOR_NUMBER @@ -242,6 +242,21 @@ AC_DEFUN([AX_INIT_STANDARD_PROJECT], [ else AM_CPPFLAGS="${AM_CPPFLAGS} -DQT_NO_DEBUG_OUTPUT -DQT_NO_DEBUG" fi + + AC_ARG_WITH(gcov, + [AS_HELP_STRING([--with-gcov=FILE], + [enable gcov, set gcov file (defaults to gcov)])], + [GCOV="$enableval"], [GCOV="no"]) + AM_CONDITIONAL(COVERAGE, test "$GCOV" != "no") + if test "$GCOV" != "no"; then + if test "$GCOV" == "yes"; then + GCOV=gcov + fi + AC_MSG_NOTICE([Coverage tests enabled, using ${GCOV}]); + AM_CXXFLAGS="${AM_CXXFLAGS:-} -O0 --coverage -fprofile-arcs -ftest-coverage" + AM_LDFLAGS="${AM_LDFLAGS} -O0 --coverage -fprofile-arcs" + AX_SUBST(GCOV) + fi if test -f ${PACKAGE_NAME}.desktop.in; then AC_CONFIG_FILES([${PACKAGE_NAME}.desktop]) @@ -273,7 +288,7 @@ EOF AC_DEFUN([AX_USE_CXX], [ m4_include(ax_cxx_compile_stdcxx_11.m4) AC_LANG(C++) - AX_CXX_COMPILE_STDCXX_11(noext, optional) + AX_CXX_COMPILE_STDCXX_14(noext, optional) AC_PROG_CXX AC_PROG_CPP diff --git a/bootstrap.sh b/bootstrap.sh index 22e0c7b..35cf0b3 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -129,6 +129,7 @@ GENERATED FILES * ax_init_standard_project.m4 - auxiliary macro definition file * ax_cxx_compile_stdcxx_11.m4 - auxiliary macro definition file * ax_check_qt.m4 - auxiliary macro definition file + * makefile_test.inc.am - makefile to be included in tests * resolve-debbuilddeps.sh - script to install debian package dependencies * resolve-rpmbuilddeps.sh - script to install RPM package dependencies * build-in-docker.sh - script to build the project encapsulated in a docker container @@ -500,6 +501,7 @@ copy ${MY_NAME} copy ax_init_standard_project.m4 copy ax_cxx_compile_stdcxx_11.m4 copy ax_check_qt.m4 +copy makefile_test.inc.am copy resolve-debbuilddeps.sh copy resolve-rpmbuilddeps.sh copy build-in-docker.sh diff --git a/makefile_test.inc.am b/makefile_test.inc.am new file mode 100644 index 0000000..a4e05fa --- /dev/null +++ b/makefile_test.inc.am @@ -0,0 +1,5 @@ +## @id $Id$ + +## 1 2 3 4 5 6 7 8 +## 45678901234567890123456789012345678901234567890123456789012345678901234567890 + diff --git a/src/matrix.hxx b/src/matrix.hxx index f6c3538..c331739 100644 --- a/src/matrix.hxx +++ b/src/matrix.hxx @@ -84,6 +84,41 @@ template class MatrixBase { ///} + /// @name element access + ///{ + + TYPE& at(size_t row, size_t column) { + assert(row class MatrixBase { ///} - /// @name element access + /// @name special operations ///{ - - TYPE& operator()(size_t row, size_t column) { - assert(row class Matrix: /// @name construction ///{ - Matrix(): Parent(TROWS, TCOLUMNS) {} - - template - Matrix(ARGS...t): Parent(TROWS, TCOLUMNS, t...) { - static_assert(sizeof...(t)==TROWS*TCOLUMNS, - "wrong array size"); + Matrix(): Parent(TROWS, TCOLUMNS) { + memset(Parent::_c, 0, Parent::MEM_SIZE); } Matrix(const Matrix& o): Matrix() { memcpy(Parent::_c, o._c, Parent::MEM_SIZE); } + template + Matrix(ARGS...t): Parent(TROWS, TCOLUMNS, t...) { + static_assert(sizeof...(t)==TROWS*TCOLUMNS, "wrong array size"); + } + ///} /// @name operators @@ -218,14 +253,6 @@ template class Matrix: return *this; } - Matrix T() const { - Matrix res; - for (size_t row(0); row(TYPE*)(Parent::_c); --to) @@ -234,15 +261,30 @@ template class Matrix: } template - Matrix operator*(const Matrix& o) { + Matrix + operator*(const Matrix& o) const { Matrix res; for (size_t i(0); iat(i, j) * o(j, k); return res; } + + ///} + /// @name special operations + ///{ + + Matrix t() const { + Matrix res; + for (size_t row(0); rowat(row, column); + return res; + } + + ///} }; //============================================================================== @@ -259,12 +301,18 @@ template class Matrix: public MatrixBase { /// @name construction ///{ + + Matrix() = delete; Matrix(size_t rows, size_t columns): Parent(rows, columns) { Parent::_c = new TYPE[rows*columns]; memset(Parent::_c, 0, Parent::MEM_SIZE); } + + Matrix(const Matrix& o): Matrix(o.ROWS, o.Parent::COLUMNS) { + memcpy(Parent::_c, o.Parent::_c, Parent::MEM_SIZE); + } template Matrix(size_t rows, size_t columns, ARGS...t): @@ -273,10 +321,6 @@ template class Matrix: public MatrixBase { copy_args(Parent::_c, t...); } - Matrix(const Matrix& o): Matrix(o.ROWS, o.Parent::COLUMNS) { - memcpy(Parent::_c, o.Parent::_c, Parent::MEM_SIZE); - } - ///} /// @name destruction @@ -303,11 +347,11 @@ template class Matrix: public MatrixBase { return Parent::operator=(o); } - Matrix T() const { + Matrix t() const { Matrix res(Parent::COLUMNS, Parent::ROWS); for (size_t row(0); rowat(row, column); return res; } @@ -318,6 +362,16 @@ template class Matrix: public MatrixBase { return res; } + Matrix operator*(const Matrix& o) const { + Matrix res(this->ROWS, o.COLUMNS); + assert(this->COLUMNS==o.ROWS); + for (size_t i(0); iROWS; ++i) + for (size_t k(0); kCOLUMNS; ++j) + res(i, k) += this->at(i, j) * o(j, k); + return res; + } + ///} //................................................................methods @@ -343,6 +397,38 @@ template class Matrix: public MatrixBase { //============================================================================== +template + Matrix operator+(const Matrix& a, + const Matrix& b) { + Matrix res(a); + res += b; + return res; +} + +template + Matrix operator-(const Matrix& a, + const Matrix& b) { + Matrix res(a); + res -= b; + return res; +} + +template + Matrix operator*(const TYPE& v, + const Matrix& m) { + Matrix res(m); + res *= v; + return res; +} + +template + Matrix operator*(const Matrix& m, + const TYPE& v) { + Matrix res(m); + res *= v; + return res; +} + template std::ostream& operator<<(std::ostream& s, const Matrix& m) { for (size_t w = 0; w < m.ROWS; ++w) { diff --git a/test/basic.cxx b/test/basic.cxx index fce9437..a678871 100644 --- a/test/basic.cxx +++ b/test/basic.cxx @@ -17,8 +17,8 @@ class TemplateMatrixTest: public CppUnit::TestFixture { public: template void initFromArray1() { - Matrix m {1, 2, 3, 4, - 5, 6, 7, 8}; + 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]); @@ -30,8 +30,8 @@ class TemplateMatrixTest: public CppUnit::TestFixture { } template void initFromArray2() { - Matrix m(1, 2, 3, 4, - 5, 6, 7, 8); + 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]); @@ -43,8 +43,8 @@ class TemplateMatrixTest: public CppUnit::TestFixture { } template void initFromArray3() { - Matrix m({1, 2, 3, 4, - 5, 6, 7, 8}); + 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]); @@ -81,7 +81,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture { } template void initFromDefault() { - Matrix m; + 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]); @@ -105,9 +105,69 @@ class TemplateMatrixTest: public CppUnit::TestFixture { 5, 6, 7, 9); Matrix m4(9, 2, 3, 4, 5, 6, 7, 8); + Matrix m5(1, 2, 0, 4, + 5, 6, 7, 8); CPPUNIT_ASSERT(m1==m2); CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m4); + CPPUNIT_ASSERT(m1!=m5); + } + 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); + } + 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 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()); } CPPUNIT_TEST_SUITE(TemplateMatrixTest); CPPUNIT_TEST(initFromArray1); @@ -146,6 +206,36 @@ class TemplateMatrixTest: public CppUnit::TestFixture { 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_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(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_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest); @@ -241,6 +331,75 @@ class VariableMatrixTest: public CppUnit::TestFixture { CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m4); } + 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); + } + 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 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()); + } CPPUNIT_TEST_SUITE(VariableMatrixTest); CPPUNIT_TEST(initFromArray1); CPPUNIT_TEST(initFromArray1); @@ -272,6 +431,42 @@ class VariableMatrixTest: public CppUnit::TestFixture { 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_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_mult); + CPPUNIT_TEST(scalar_mult); + CPPUNIT_TEST(scalar_mult); + CPPUNIT_TEST(scalar_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_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest);