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.

173 lines
5.7KB

  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() : UnitTest ("Linear Algebra UnitTests", "DSP") {}
  26. struct AdditionTest
  27. {
  28. template <typename ElementType>
  29. static void run (LinearAlgebraUnitTest& u)
  30. {
  31. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  32. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  33. const ElementType data3[] = { 2, 1, 6, 3, 10, 5, 14, 7 };
  34. Matrix<ElementType> mat1 (2, 4, data1);
  35. Matrix<ElementType> mat2 (2, 4, data2);
  36. Matrix<ElementType> mat3 (2, 4, data3);
  37. u.expect((mat1 + mat2) == mat3);
  38. }
  39. };
  40. struct DifferenceTest
  41. {
  42. template <typename ElementType>
  43. static void run (LinearAlgebraUnitTest& u)
  44. {
  45. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  46. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  47. const ElementType data3[] = { 0, 3, 0, 5, 0, 7, 0, 9 };
  48. Matrix<ElementType> mat1 (2, 4, data1);
  49. Matrix<ElementType> mat2 (2, 4, data2);
  50. Matrix<ElementType> mat3 (2, 4, data3);
  51. u.expect((mat1 - mat2) == mat3);
  52. }
  53. };
  54. struct ScalarMultiplicationTest
  55. {
  56. template <typename ElementType>
  57. static void run (LinearAlgebraUnitTest& u)
  58. {
  59. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  60. const ElementType scalar = 2.0;
  61. const ElementType data2[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  62. Matrix<ElementType> x (2, 4, data1);
  63. Matrix<ElementType> expected (2, 4, data2);
  64. u.expect ((x * scalar) == expected);
  65. }
  66. };
  67. struct HadamardProductTest
  68. {
  69. template <typename ElementType>
  70. static void run (LinearAlgebraUnitTest& u)
  71. {
  72. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  73. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  74. const ElementType data3[] = { 1, -2, 9, -4, 25, -6, 49, -8 };
  75. Matrix<ElementType> mat1 (2, 4, data1);
  76. Matrix<ElementType> mat2 (2, 4, data2);
  77. Matrix<ElementType> mat3 (2, 4, data3);
  78. u.expect (Matrix<ElementType>::hadarmard (mat1, mat2) == mat3);
  79. }
  80. };
  81. struct MultiplicationTest
  82. {
  83. template <typename ElementType>
  84. static void run (LinearAlgebraUnitTest& u)
  85. {
  86. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  87. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  88. const ElementType data3[] = { 50, -10, 114, -26 };
  89. Matrix<ElementType> mat1 (2, 4, data1);
  90. Matrix<ElementType> mat2 (4, 2, data2);
  91. Matrix<ElementType> mat3 (2, 2, data3);
  92. u.expect((mat1 * mat2) == mat3);
  93. }
  94. };
  95. struct IdentityMatrixTest
  96. {
  97. template <typename ElementType>
  98. static void run (LinearAlgebraUnitTest& u)
  99. {
  100. const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  101. u.expect (Matrix<ElementType>::identity (4) == Matrix<ElementType> (4, 4, data1));
  102. }
  103. };
  104. struct SolvingTest
  105. {
  106. template <typename ElementType>
  107. static void run (LinearAlgebraUnitTest& u)
  108. {
  109. const ElementType data1[] = { 1, -1, 2, -2 };
  110. const ElementType data2[] = { -1, 0, -1, -7 };
  111. const ElementType data3[] = { 1, 4, 2, 1, -1, 1, 4, 3, -2, -1, 1, 1, -1, 0, 1, 4 };
  112. Matrix<ElementType> X (4, 1, data1);
  113. Matrix<ElementType> B (4, 1, data2);
  114. Matrix<ElementType> A (4, 4, data3);
  115. u.expect (A.solve (B));
  116. u.expect (Matrix<ElementType>::compare (X, B, (ElementType) 1e-4));
  117. }
  118. };
  119. template <class TheTest>
  120. void runTestForAllTypes (const char* unitTestName)
  121. {
  122. beginTest (unitTestName);
  123. TheTest::template run<float> (*this);
  124. TheTest::template run<double> (*this);
  125. }
  126. void runTest() override
  127. {
  128. runTestForAllTypes<AdditionTest> ("AdditionTest");
  129. runTestForAllTypes<DifferenceTest> ("DifferenceTest");
  130. runTestForAllTypes<ScalarMultiplicationTest> ("ScalarMultiplication");
  131. runTestForAllTypes<HadamardProductTest> ("HadamardProductTest");
  132. runTestForAllTypes<MultiplicationTest> ("MultiplicationTest");
  133. runTestForAllTypes<IdentityMatrixTest> ("IdentityMatrixTest");
  134. runTestForAllTypes<SolvingTest> ("SolvingTest");
  135. }
  136. };
  137. static LinearAlgebraUnitTest linearAlgebraUnitTest;
  138. } // namespace dsp
  139. } // namespace juce