OpenShot Audio Library | OpenShotAudio: juce_Matrix_test.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
juce_Matrix_test.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12 27th April 2017).
13
14 End User License Agreement: www.juce.com/juce-5-licence
15 Privacy Policy: www.juce.com/juce-5-privacy-policy
16
17 Or: You may also use this code under the terms of the GPL v3 (see
18 www.gnu.org/licenses).
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29namespace dsp
30{
31
32struct LinearAlgebraUnitTest : public UnitTest
33{
34 LinearAlgebraUnitTest()
35 : UnitTest ("Linear Algebra UnitTests", UnitTestCategories::dsp)
36 {}
37
38 struct AdditionTest
39 {
40 template <typename ElementType>
41 static void run (LinearAlgebraUnitTest& u)
42 {
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 };
46
47 Matrix<ElementType> mat1 (2, 4, data1);
48 Matrix<ElementType> mat2 (2, 4, data2);
49 Matrix<ElementType> mat3 (2, 4, data3);
50
51 u.expect((mat1 + mat2) == mat3);
52 }
53 };
54
55 struct DifferenceTest
56 {
57 template <typename ElementType>
58 static void run (LinearAlgebraUnitTest& u)
59 {
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 };
63
64 Matrix<ElementType> mat1 (2, 4, data1);
65 Matrix<ElementType> mat2 (2, 4, data2);
66 Matrix<ElementType> mat3 (2, 4, data3);
67
68 u.expect((mat1 - mat2) == mat3);
69 }
70 };
71
72 struct ScalarMultiplicationTest
73 {
74 template <typename ElementType>
75 static void run (LinearAlgebraUnitTest& u)
76 {
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 };
80
81 Matrix<ElementType> x (2, 4, data1);
82 Matrix<ElementType> expected (2, 4, data2);
83
84 u.expect ((x * scalar) == expected);
85 }
86 };
87
88 struct HadamardProductTest
89 {
90 template <typename ElementType>
91 static void run (LinearAlgebraUnitTest& u)
92 {
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 };
96
97 Matrix<ElementType> mat1 (2, 4, data1);
98 Matrix<ElementType> mat2 (2, 4, data2);
99 Matrix<ElementType> mat3 (2, 4, data3);
100
101 u.expect (Matrix<ElementType>::hadarmard (mat1, mat2) == mat3);
102 }
103 };
104
105 struct MultiplicationTest
106 {
107 template <typename ElementType>
108 static void run (LinearAlgebraUnitTest& u)
109 {
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 };
113
114 Matrix<ElementType> mat1 (2, 4, data1);
115 Matrix<ElementType> mat2 (4, 2, data2);
116 Matrix<ElementType> mat3 (2, 2, data3);
117
118 u.expect((mat1 * mat2) == mat3);
119 }
120 };
121
122 struct IdentityMatrixTest
123 {
124 template <typename ElementType>
125 static void run (LinearAlgebraUnitTest& u)
126 {
127 const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
128 u.expect (Matrix<ElementType>::identity (4) == Matrix<ElementType> (4, 4, data1));
129 }
130 };
131
132 struct SolvingTest
133 {
134 template <typename ElementType>
135 static void run (LinearAlgebraUnitTest& u)
136 {
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 };
140
141 Matrix<ElementType> X (4, 1, data1);
142 Matrix<ElementType> B (4, 1, data2);
143 Matrix<ElementType> A (4, 4, data3);
144
145 u.expect (A.solve (B));
146 u.expect (Matrix<ElementType>::compare (X, B, (ElementType) 1e-4));
147 }
148 };
149
150 template <class TheTest>
151 void runTestForAllTypes (const char* unitTestName)
152 {
153 beginTest (unitTestName);
154
155 TheTest::template run<float> (*this);
156 TheTest::template run<double> (*this);
157 }
158
159 void runTest() override
160 {
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");
168 }
169};
170
171static LinearAlgebraUnitTest linearAlgebraUnitTest;
172
173} // namespace dsp
174} // namespace juce
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:165
juce::dsp::Matrix::compare
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition: juce_Matrix.cpp:149
juce::dsp::Matrix::identity
static Matrix identity(size_t size)
Definition: juce_Matrix.cpp:33