1 #include <msp/linal/matrix.h>
2 #include <msp/linal/squarematrix.h>
3 #include <msp/test/test.h>
7 class MatrixTests: public Test::RegisteredTest<MatrixTests>
12 static const char *get_name() { return "Matrix"; }
20 MatrixTests::MatrixTests()
22 add(&MatrixTests::multiply, "Multiplication");
23 add(&MatrixTests::invert, "Inversion");
26 void MatrixTests::multiply()
28 static double data[] =
32 3, 6, 3, 3, 6, 3, 6, 3, 6,
36 LinAl::Matrix<double, 3, 2> a(data);
37 LinAl::Matrix<double, 2, 3> b(data+6);
38 EXPECT(a*b == (LinAl::Matrix<double, 3, 3>(data+12)));
39 EXPECT(b*a == (LinAl::Matrix<double, 2, 2>(data+21)));
42 template<typename T, unsigned N>
43 bool is_identity(const LinAl::Matrix<T, N, N> &m)
45 for(unsigned i=0; i<N; ++i)
46 for(unsigned j=0; j<N; ++j)
47 if(abs(T(i==j)-m(i, j))>1e-10)
52 void MatrixTests::invert()
54 static double data[] =
56 1, 4, 2, 4, 8, 6, 2, 2, 4
59 LinAl::SquareMatrix<double, 3> m(data);
60 LinAl::SquareMatrix<double, 3> i = LinAl::invert(m);
61 EXPECT(is_identity(i*m));