MatrixComparisonsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Numeric;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. class MatrixComparisonsTest extends \PHPUnit\Framework\TestCase
  5. {
  6. use \MathPHP\Tests\LinearAlgebra\Fixture\MatrixDataProvider;
  7. /**
  8. * @test isEqual finds two matrices to be equal
  9. * @dataProvider dataProviderForSquareMatrix
  10. * @dataProvider dataProviderForNotSquareMatrix
  11. * @dataProvider dataProviderForNonsingularMatrix
  12. * @dataProvider dataProviderForMatrixWithWeirdNumbers
  13. * @param array $A
  14. * @throws \Exception
  15. */
  16. public function testIsEqual(array $A)
  17. {
  18. // Given
  19. $A = MatrixFactory::create($A);
  20. // Then
  21. $this->assertTrue($A->isEqual($A));
  22. }
  23. /**
  24. * @test isEqual finds to matrices to not be equal
  25. * @dataProvider dataProviderForTwoSquareMatrices
  26. * @dataProvider dataProviderForTwoNonsingularMatrices
  27. * @param array $A
  28. * @param array $B
  29. * @throws \Exception
  30. */
  31. public function testIsNotEqual(array $A, array $B)
  32. {
  33. // Given
  34. $A = MatrixFactory::create($A);
  35. $B = MatrixFactory::create($B);
  36. // Then
  37. $this->assertFalse($A->isEqual($B));
  38. $this->assertFalse($B->isEqual($A));
  39. }
  40. /**
  41. * @test isEqual finds matrices to be equal because the values are within the error tolerance
  42. * @throws \Exception
  43. */
  44. public function testIsEqualBecauseWithinErrorTolerance()
  45. {
  46. // Given
  47. $A = MatrixFactory::create([
  48. [1.0001, 2.0002],
  49. [3.0003, 4.0004],
  50. ]);
  51. $B = MatrixFactory::create([
  52. [1.0002, 2.0003],
  53. [3.0004, 4.0005],
  54. ]);
  55. // When
  56. $A->setError(0.001);
  57. // Then
  58. $this->assertTrue($A->isEqual($B));
  59. }
  60. /**
  61. * @test isEqual finds matrices to be not equal because the values are within the error tolerance
  62. * @throws \Exception
  63. */
  64. public function testIsNotEqualBecauseOutsideOfErrorTolerance()
  65. {
  66. // Given
  67. $A = MatrixFactory::create([
  68. [1.0001, 2.0002],
  69. [3.0003, 4.0004],
  70. ]);
  71. $B = MatrixFactory::create([
  72. [1.0002, 2.0003],
  73. [3.0004, 4.0005],
  74. ]);
  75. // When
  76. $A->setError(0.00001);
  77. // Then
  78. $this->assertFalse($A->isEqual($B));
  79. }
  80. /**
  81. * @test isEqual finds matrices to be not equal because the dimensions are not the same
  82. * @throws \Exception
  83. */
  84. public function testIsEqualNotEqualBecauseDifferentDimmensions()
  85. {
  86. // Given
  87. $A = MatrixFactory::create([
  88. [1, 2, 3],
  89. [2, 3, 4],
  90. ]);
  91. $B = MatrixFactory::create([
  92. [1, 2],
  93. [2, 3],
  94. ]);
  95. // Then
  96. $this->assertFalse($A->isEqual($B));
  97. $this->assertFalse($B->isEqual($A));
  98. }
  99. }