OpenShot Audio Library | OpenShotAudio: juce_Matrix_test.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_Matrix_test.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::dsp
27{
28
29struct LinearAlgebraUnitTest final : public UnitTest
30{
31 LinearAlgebraUnitTest()
32 : UnitTest ("Linear Algebra UnitTests", UnitTestCategories::dsp)
33 {}
34
35 struct AdditionTest
36 {
37 template <typename ElementType>
38 static void run (LinearAlgebraUnitTest& u)
39 {
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 };
43
44 Matrix<ElementType> mat1 (2, 4, data1);
45 Matrix<ElementType> mat2 (2, 4, data2);
46 Matrix<ElementType> mat3 (2, 4, data3);
47
48 u.expect ((mat1 + mat2) == mat3);
49 }
50 };
51
52 struct DifferenceTest
53 {
54 template <typename ElementType>
55 static void run (LinearAlgebraUnitTest& u)
56 {
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 };
60
61 Matrix<ElementType> mat1 (2, 4, data1);
62 Matrix<ElementType> mat2 (2, 4, data2);
63 Matrix<ElementType> mat3 (2, 4, data3);
64
65 u.expect ((mat1 - mat2) == mat3);
66 }
67 };
68
69 struct ScalarMultiplicationTest
70 {
71 template <typename ElementType>
72 static void run (LinearAlgebraUnitTest& u)
73 {
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 };
77
78 Matrix<ElementType> x (2, 4, data1);
79 Matrix<ElementType> expected (2, 4, data2);
80
81 u.expect ((x * scalar) == expected);
82 }
83 };
84
85 struct HadamardProductTest
86 {
87 template <typename ElementType>
88 static void run (LinearAlgebraUnitTest& u)
89 {
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 };
93
94 Matrix<ElementType> mat1 (2, 4, data1);
95 Matrix<ElementType> mat2 (2, 4, data2);
96 Matrix<ElementType> mat3 (2, 4, data3);
97
98 u.expect (Matrix<ElementType>::hadarmard (mat1, mat2) == mat3);
99 }
100 };
101
102 struct MultiplicationTest
103 {
104 template <typename ElementType>
105 static void run (LinearAlgebraUnitTest& u)
106 {
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 };
110
111 Matrix<ElementType> mat1 (2, 4, data1);
112 Matrix<ElementType> mat2 (4, 2, data2);
113 Matrix<ElementType> mat3 (2, 2, data3);
114
115 u.expect ((mat1 * mat2) == mat3);
116 }
117 };
118
119 struct IdentityMatrixTest
120 {
121 template <typename ElementType>
122 static void run (LinearAlgebraUnitTest& u)
123 {
124 const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
125 u.expect (Matrix<ElementType>::identity (4) == Matrix<ElementType> (4, 4, data1));
126 }
127 };
128
129 struct SolvingTest
130 {
131 template <typename ElementType>
132 static void run (LinearAlgebraUnitTest& u)
133 {
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 };
137
138 Matrix<ElementType> X (4, 1, data1);
139 Matrix<ElementType> B (4, 1, data2);
140 Matrix<ElementType> A (4, 4, data3);
141
142 u.expect (A.solve (B));
143 u.expect (Matrix<ElementType>::compare (X, B, (ElementType) 1e-4));
144 }
145 };
146
147 template <class TheTest>
148 void runTestForAllTypes (const char* unitTestName)
149 {
150 beginTest (unitTestName);
151
152 TheTest::template run<float> (*this);
153 TheTest::template run<double> (*this);
154 }
155
156 void runTest() override
157 {
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");
165 }
166};
167
168static LinearAlgebraUnitTest linearAlgebraUnitTest;
169
170} // namespace juce::dsp
juce::UnitTest::UnitTest
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
juce::UnitTest::beginTest
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
juce::dsp::Matrix::hadarmard
Matrix & hadarmard(const Matrix &other) noexcept
Definition juce_Matrix.h:162
juce::dsp::Matrix::compare
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition juce_Matrix.cpp:146
juce::dsp::Matrix::identity
static Matrix identity(size_t size)
Definition juce_Matrix.cpp:30