MatrixRowOperationsTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Base;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\Exception;
  5. class MatrixRowOperationsTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test rowInterchange
  9. * @dataProvider dataProviderForRowInterchange
  10. * @param array $A
  11. * @param int $mᵢ
  12. * @param int $mⱼ
  13. * @param array $expectedMatrix
  14. * @throws \Exception
  15. */
  16. public function testRowInterchange(array $A, int $mᵢ, int $mⱼ, array $expectedMatrix)
  17. {
  18. // Given
  19. $A = MatrixFactory::create($A);
  20. $expectedMatrix = MatrixFactory::create($expectedMatrix);
  21. // When
  22. $R = $A->rowInterchange($mᵢ, $mⱼ);
  23. // Then
  24. $this->assertEquals($expectedMatrix, $R);
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function dataProviderForRowInterchange(): array
  30. {
  31. return [
  32. [
  33. [
  34. [1, 2, 3],
  35. [2, 3, 4],
  36. [3, 4, 5],
  37. ], 0, 1,
  38. [
  39. [2, 3, 4],
  40. [1, 2, 3],
  41. [3, 4, 5],
  42. ]
  43. ],
  44. [
  45. [
  46. [5, 5],
  47. [4, 4],
  48. [2, 7],
  49. [9, 0],
  50. ], 2, 3,
  51. [
  52. [5, 5],
  53. [4, 4],
  54. [9, 0],
  55. [2, 7],
  56. ]
  57. ],
  58. [
  59. [
  60. [5, 5],
  61. [4, 4],
  62. [2, 7],
  63. [9, 0],
  64. ], 1, 2,
  65. [
  66. [5, 5],
  67. [2, 7],
  68. [4, 4],
  69. [9, 0],
  70. ]
  71. ]
  72. ];
  73. }
  74. /**
  75. * @test rowInterchange on a row greater than m
  76. * @throws \Exception
  77. */
  78. public function testRowInterchangeExceptionRowGreaterThanM()
  79. {
  80. // Given
  81. $A = MatrixFactory::create([
  82. [1, 2, 3],
  83. [2, 3, 4],
  84. [3, 4, 5],
  85. ]);
  86. // Then
  87. $this->expectException(Exception\MatrixException::class);
  88. // When
  89. $A->rowInterchange(4, 5);
  90. }
  91. /**
  92. * @test rowExclude
  93. * @dataProvider dataProviderForRowExclude
  94. * @param array $A
  95. * @param int $mᵢ
  96. * @param array $expectedMatrix
  97. * @throws \Exception
  98. */
  99. public function testRowExclude(array $A, int $mᵢ, array $expectedMatrix)
  100. {
  101. // Given
  102. $A = MatrixFactory::create($A);
  103. $expectedMatrix = MatrixFactory::create($expectedMatrix);
  104. // When
  105. $R = $A->rowExclude($mᵢ);
  106. // Then
  107. $this->assertEquals($expectedMatrix, $R);
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function dataProviderForRowExclude(): array
  113. {
  114. return [
  115. [
  116. [
  117. [1, 2, 3],
  118. [2, 3, 4],
  119. [3, 4, 5],
  120. ], 0,
  121. [
  122. [2, 3, 4],
  123. [3, 4, 5],
  124. ]
  125. ],
  126. [
  127. [
  128. [1, 2, 3],
  129. [2, 3, 4],
  130. [3, 4, 5],
  131. ], 1,
  132. [
  133. [1, 2, 3],
  134. [3, 4, 5],
  135. ]
  136. ],
  137. [
  138. [
  139. [1, 2, 3],
  140. [2, 3, 4],
  141. [3, 4, 5],
  142. ], 2,
  143. [
  144. [1, 2, 3],
  145. [2, 3, 4],
  146. ]
  147. ],
  148. ];
  149. }
  150. /**
  151. * @test rowExclude on a row that does not exist
  152. * @throws \Exception
  153. */
  154. public function testRowExcludeExceptionRowDoesNotExist()
  155. {
  156. // Given
  157. $A = MatrixFactory::create([
  158. [1, 2, 3],
  159. [2, 3, 4],
  160. [4, 5, 6],
  161. ]);
  162. // Then
  163. $this->expectException(Exception\MatrixException::class);
  164. // When
  165. $A->rowExclude(-5);
  166. }
  167. }