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.

171 lines
5.7KB

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