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.

167 lines
5.5KB

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