32struct LinearAlgebraUnitTest :
public UnitTest
34 LinearAlgebraUnitTest()
35 :
UnitTest (
"Linear Algebra UnitTests", UnitTestCategories::dsp)
40 template <
typename ElementType>
41 static void run (LinearAlgebraUnitTest& u)
43 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
44 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
45 const ElementType data3[] = { 2, 1, 6, 3, 10, 5, 14, 7 };
47 Matrix<ElementType> mat1 (2, 4, data1);
48 Matrix<ElementType> mat2 (2, 4, data2);
49 Matrix<ElementType> mat3 (2, 4, data3);
51 u.expect((mat1 + mat2) == mat3);
57 template <
typename ElementType>
58 static void run (LinearAlgebraUnitTest& u)
60 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
61 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
62 const ElementType data3[] = { 0, 3, 0, 5, 0, 7, 0, 9 };
64 Matrix<ElementType> mat1 (2, 4, data1);
65 Matrix<ElementType> mat2 (2, 4, data2);
66 Matrix<ElementType> mat3 (2, 4, data3);
68 u.expect((mat1 - mat2) == mat3);
72 struct ScalarMultiplicationTest
74 template <
typename ElementType>
75 static void run (LinearAlgebraUnitTest& u)
77 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
78 const ElementType scalar = 2.0;
79 const ElementType data2[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
81 Matrix<ElementType> x (2, 4, data1);
82 Matrix<ElementType> expected (2, 4, data2);
84 u.expect ((x * scalar) == expected);
88 struct HadamardProductTest
90 template <
typename ElementType>
91 static void run (LinearAlgebraUnitTest& u)
93 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
94 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
95 const ElementType data3[] = { 1, -2, 9, -4, 25, -6, 49, -8 };
97 Matrix<ElementType> mat1 (2, 4, data1);
98 Matrix<ElementType> mat2 (2, 4, data2);
99 Matrix<ElementType> mat3 (2, 4, data3);
105 struct MultiplicationTest
107 template <
typename ElementType>
108 static void run (LinearAlgebraUnitTest& u)
110 const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
111 const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
112 const ElementType data3[] = { 50, -10, 114, -26 };
114 Matrix<ElementType> mat1 (2, 4, data1);
115 Matrix<ElementType> mat2 (4, 2, data2);
116 Matrix<ElementType> mat3 (2, 2, data3);
118 u.expect((mat1 * mat2) == mat3);
122 struct IdentityMatrixTest
124 template <
typename ElementType>
125 static void run (LinearAlgebraUnitTest& u)
127 const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
134 template <
typename ElementType>
135 static void run (LinearAlgebraUnitTest& u)
137 const ElementType data1[] = { 1, -1, 2, -2 };
138 const ElementType data2[] = { -1, 0, -1, -7 };
139 const ElementType data3[] = { 1, 4, 2, 1, -1, 1, 4, 3, -2, -1, 1, 1, -1, 0, 1, 4 };
141 Matrix<ElementType> X (4, 1, data1);
142 Matrix<ElementType> B (4, 1, data2);
143 Matrix<ElementType> A (4, 4, data3);
145 u.expect (A.solve (B));
150 template <
class TheTest>
151 void runTestForAllTypes (
const char* unitTestName)
155 TheTest::template run<float> (*
this);
156 TheTest::template run<double> (*
this);
159 void runTest()
override
161 runTestForAllTypes<AdditionTest> (
"AdditionTest");
162 runTestForAllTypes<DifferenceTest> (
"DifferenceTest");
163 runTestForAllTypes<ScalarMultiplicationTest> (
"ScalarMultiplication");
164 runTestForAllTypes<HadamardProductTest> (
"HadamardProductTest");
165 runTestForAllTypes<MultiplicationTest> (
"MultiplicationTest");
166 runTestForAllTypes<IdentityMatrixTest> (
"IdentityMatrixTest");
167 runTestForAllTypes<SolvingTest> (
"SolvingTest");
171static 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)