The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. struct LinearAlgebraUnitTest : public UnitTest
  24. {
  25. LinearAlgebraUnitTest()
  26. : UnitTest ("Linear Algebra UnitTests", UnitTestCategories::dsp)
  27. {}
  28. struct AdditionTest
  29. {
  30. template <typename ElementType>
  31. static void run (LinearAlgebraUnitTest& u)
  32. {
  33. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  34. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  35. const ElementType data3[] = { 2, 1, 6, 3, 10, 5, 14, 7 };
  36. Matrix<ElementType> mat1 (2, 4, data1);
  37. Matrix<ElementType> mat2 (2, 4, data2);
  38. Matrix<ElementType> mat3 (2, 4, data3);
  39. u.expect((mat1 + mat2) == mat3);
  40. }
  41. };
  42. struct DifferenceTest
  43. {
  44. template <typename ElementType>
  45. static void run (LinearAlgebraUnitTest& u)
  46. {
  47. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  48. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  49. const ElementType data3[] = { 0, 3, 0, 5, 0, 7, 0, 9 };
  50. Matrix<ElementType> mat1 (2, 4, data1);
  51. Matrix<ElementType> mat2 (2, 4, data2);
  52. Matrix<ElementType> mat3 (2, 4, data3);
  53. u.expect((mat1 - mat2) == mat3);
  54. }
  55. };
  56. struct ScalarMultiplicationTest
  57. {
  58. template <typename ElementType>
  59. static void run (LinearAlgebraUnitTest& u)
  60. {
  61. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  62. const ElementType scalar = 2.0;
  63. const ElementType data2[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  64. Matrix<ElementType> x (2, 4, data1);
  65. Matrix<ElementType> expected (2, 4, data2);
  66. u.expect ((x * scalar) == expected);
  67. }
  68. };
  69. struct HadamardProductTest
  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 data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  76. const ElementType data3[] = { 1, -2, 9, -4, 25, -6, 49, -8 };
  77. Matrix<ElementType> mat1 (2, 4, data1);
  78. Matrix<ElementType> mat2 (2, 4, data2);
  79. Matrix<ElementType> mat3 (2, 4, data3);
  80. u.expect (Matrix<ElementType>::hadarmard (mat1, mat2) == mat3);
  81. }
  82. };
  83. struct MultiplicationTest
  84. {
  85. template <typename ElementType>
  86. static void run (LinearAlgebraUnitTest& u)
  87. {
  88. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  89. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  90. const ElementType data3[] = { 50, -10, 114, -26 };
  91. Matrix<ElementType> mat1 (2, 4, data1);
  92. Matrix<ElementType> mat2 (4, 2, data2);
  93. Matrix<ElementType> mat3 (2, 2, data3);
  94. u.expect((mat1 * mat2) == mat3);
  95. }
  96. };
  97. struct IdentityMatrixTest
  98. {
  99. template <typename ElementType>
  100. static void run (LinearAlgebraUnitTest& u)
  101. {
  102. const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  103. u.expect (Matrix<ElementType>::identity (4) == Matrix<ElementType> (4, 4, data1));
  104. }
  105. };
  106. struct SolvingTest
  107. {
  108. template <typename ElementType>
  109. static void run (LinearAlgebraUnitTest& u)
  110. {
  111. const ElementType data1[] = { 1, -1, 2, -2 };
  112. const ElementType data2[] = { -1, 0, -1, -7 };
  113. const ElementType data3[] = { 1, 4, 2, 1, -1, 1, 4, 3, -2, -1, 1, 1, -1, 0, 1, 4 };
  114. Matrix<ElementType> X (4, 1, data1);
  115. Matrix<ElementType> B (4, 1, data2);
  116. Matrix<ElementType> A (4, 4, data3);
  117. u.expect (A.solve (B));
  118. u.expect (Matrix<ElementType>::compare (X, B, (ElementType) 1e-4));
  119. }
  120. };
  121. template <class TheTest>
  122. void runTestForAllTypes (const char* unitTestName)
  123. {
  124. beginTest (unitTestName);
  125. TheTest::template run<float> (*this);
  126. TheTest::template run<double> (*this);
  127. }
  128. void runTest() override
  129. {
  130. runTestForAllTypes<AdditionTest> ("AdditionTest");
  131. runTestForAllTypes<DifferenceTest> ("DifferenceTest");
  132. runTestForAllTypes<ScalarMultiplicationTest> ("ScalarMultiplication");
  133. runTestForAllTypes<HadamardProductTest> ("HadamardProductTest");
  134. runTestForAllTypes<MultiplicationTest> ("MultiplicationTest");
  135. runTestForAllTypes<IdentityMatrixTest> ("IdentityMatrixTest");
  136. runTestForAllTypes<SolvingTest> ("SolvingTest");
  137. }
  138. };
  139. static LinearAlgebraUnitTest linearAlgebraUnitTest;
  140. } // namespace dsp
  141. } // namespace juce