GivensMatrixTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Other;
  3. use MathPHP\Exception\OutOfBoundsException;
  4. use MathPHP\LinearAlgebra\MatrixFactory;
  5. class GivensMatrixTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test Test that the construction fails when parameters are out of bounds
  9. * @throws \Exception
  10. */
  11. public function testException()
  12. {
  13. // Given
  14. $m = 2;
  15. $n = 3;
  16. $angle = \M_PI;
  17. $size = 2;
  18. // Then
  19. $this->expectException(OutOfBoundsException::class);
  20. // When
  21. $matrix = MatrixFactory::givens($m, $n, $angle, $size);
  22. }
  23. /**
  24. * @test Test that the function returns a properly formatted Matrix
  25. * @dataProvider dataProviderForTestGivensMatrix
  26. * @param int $m
  27. * @param int $n
  28. * @param float $angle
  29. * @param int $size
  30. * @param array $expected
  31. * @throws \Exception
  32. */
  33. public function testGivensMatrix(int $m, int $n, float $angle, int $size, array $expected)
  34. {
  35. // When
  36. $G = MatrixFactory::givens($m, $n, $angle, $size);
  37. // Then
  38. $this->assertEqualsWithDelta($expected, $G->getMatrix(), 0.00001);
  39. }
  40. /**
  41. * @return array
  42. */
  43. public function dataProviderForTestGivensMatrix(): array
  44. {
  45. return [
  46. [
  47. 0, 1, \M_PI, 2,
  48. [[-1, 0],
  49. [0, -1]]
  50. ],
  51. [
  52. 0, 2, \M_PI / 4, 3,
  53. [[\M_SQRT1_2, 0, -1 * \M_SQRT1_2],
  54. [0, 1, 0],
  55. [\M_SQRT1_2, 0, \M_SQRT1_2]],
  56. ],
  57. ];
  58. }
  59. }