MatrixColumnOperationsTest.php 4.3 KB

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