29struct LinearAlgebraUnitTest final :
public UnitTest
31 LinearAlgebraUnitTest()
32 :
UnitTest (
"Linear Algebra UnitTests", UnitTestCategories::dsp)
37 template <
typename ElementType>
38 static void run (LinearAlgebraUnitTest& u)
40 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
41 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
42 const ElementType data3[] = { 2, 1, 6, 3, 10, 5, 14, 7 };
44 Matrix<ElementType> mat1 (2, 4, data1);
45 Matrix<ElementType> mat2 (2, 4, data2);
46 Matrix<ElementType> mat3 (2, 4, data3);
48 u.expect ((mat1 + mat2) == mat3);
54 template <
typename ElementType>
55 static void run (LinearAlgebraUnitTest& u)
57 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
58 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
59 const ElementType data3[] = { 0, 3, 0, 5, 0, 7, 0, 9 };
61 Matrix<ElementType> mat1 (2, 4, data1);
62 Matrix<ElementType> mat2 (2, 4, data2);
63 Matrix<ElementType> mat3 (2, 4, data3);
65 u.expect ((mat1 - mat2) == mat3);
69 struct ScalarMultiplicationTest
71 template <
typename ElementType>
72 static void run (LinearAlgebraUnitTest& u)
74 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
75 const ElementType scalar = 2.0;
76 const ElementType data2[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
78 Matrix<ElementType> x (2, 4, data1);
79 Matrix<ElementType> expected (2, 4, data2);
81 u.expect ((x * scalar) == expected);
85 struct HadamardProductTest
87 template <
typename ElementType>
88 static void run (LinearAlgebraUnitTest& u)
90 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
91 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
92 const ElementType data3[] = { 1, -2, 9, -4, 25, -6, 49, -8 };
94 Matrix<ElementType> mat1 (2, 4, data1);
95 Matrix<ElementType> mat2 (2, 4, data2);
96 Matrix<ElementType> mat3 (2, 4, data3);
102 struct MultiplicationTest
104 template <
typename ElementType>
105 static void run (LinearAlgebraUnitTest& u)
107 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
108 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
109 const ElementType data3[] = { 50, -10, 114, -26 };
111 Matrix<ElementType> mat1 (2, 4, data1);
112 Matrix<ElementType> mat2 (4, 2, data2);
113 Matrix<ElementType> mat3 (2, 2, data3);
115 u.expect ((mat1 * mat2) == mat3);
119 struct IdentityMatrixTest
121 template <
typename ElementType>
122 static void run (LinearAlgebraUnitTest& u)
124 const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
131 template <
typename ElementType>
132 static void run (LinearAlgebraUnitTest& u)
134 const ElementType data1[] = { 1, -1, 2, -2 };
135 const ElementType data2[] = { -1, 0, -1, -7 };
136 const ElementType data3[] = { 1, 4, 2, 1, -1, 1, 4, 3, -2, -1, 1, 1, -1, 0, 1, 4 };
138 Matrix<ElementType> X (4, 1, data1);
139 Matrix<ElementType> B (4, 1, data2);
140 Matrix<ElementType> A (4, 4, data3);
142 u.expect (A.solve (B));
147 template <
class TheTest>
148 void runTestForAllTypes (
const char* unitTestName)
152 TheTest::template run<float> (*
this);
153 TheTest::template run<double> (*
this);
158 runTestForAllTypes<AdditionTest> (
"AdditionTest");
159 runTestForAllTypes<DifferenceTest> (
"DifferenceTest");
160 runTestForAllTypes<ScalarMultiplicationTest> (
"ScalarMultiplication");
161 runTestForAllTypes<HadamardProductTest> (
"HadamardProductTest");
162 runTestForAllTypes<MultiplicationTest> (
"MultiplicationTest");
163 runTestForAllTypes<IdentityMatrixTest> (
"IdentityMatrixTest");
164 runTestForAllTypes<SolvingTest> (
"SolvingTest");
168static LinearAlgebraUnitTest linearAlgebraUnitTest;
UnitTest(const String &name, const String &category=String())
void beginTest(const String &testName)
Matrix & hadarmard(const Matrix &other) noexcept
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
static Matrix identity(size_t size)