MatrixTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Base;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\LinearAlgebra\NumericMatrix;
  5. use MathPHP\Exception;
  6. class MatrixTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /** @var array */
  9. private $A;
  10. /** @var NumericMatrix */
  11. private $matrix;
  12. /**
  13. * @throws \Exception
  14. */
  15. public function setUp(): void
  16. {
  17. $this->A = [
  18. [1, 2, 3],
  19. [2, 3, 4],
  20. [4, 5, 6],
  21. ];
  22. $this->matrix = new NumericMatrix($this->A);
  23. }
  24. /**
  25. * @test Implemented interfaces
  26. * @throws \Exception
  27. */
  28. public function testInterfaces()
  29. {
  30. $this->assertInstanceOf(\ArrayAccess::class, $this->matrix);
  31. $this->assertInstanceOf(\JsonSerializable::class, $this->matrix);
  32. }
  33. /**
  34. * @test constructor throws Exception\MatrixException if the number of columns is not consistent
  35. * @throws \Exception
  36. */
  37. public function testConstructorExceptionNCountDiffers()
  38. {
  39. // Given
  40. $A = [
  41. [1, 2, 3],
  42. [2, 3, 4, 5],
  43. [3, 4, 5],
  44. ];
  45. // Then
  46. $this->expectException(Exception\MatrixException::class);
  47. // When
  48. $matrix = MatrixFactory::create($A);
  49. }
  50. /**
  51. * @test constructor throws Exception\BadDataException if the number of columns is not consistent
  52. * @throws \Exception
  53. */
  54. public function testRawConstructorExceptionNCountDiffers()
  55. {
  56. // Given
  57. $A = [
  58. [1, 2, 3],
  59. [2, 3, 4, 5],
  60. [3, 4, 5],
  61. ];
  62. // Then
  63. $this->expectException(Exception\BadDataException::class);
  64. // When
  65. $matrix = new NumericMatrix($A);
  66. }
  67. /**
  68. * @test Matrix implements \ArrayAccess
  69. * @throws \Exception
  70. */
  71. public function testArrayAccessInterfaceOffsetGet()
  72. {
  73. // Given
  74. $A = [
  75. [1, 2, 3],
  76. [2, 3, 4],
  77. [4, 5, 6],
  78. ];
  79. $matrix = MatrixFactory::create($A);
  80. // Then
  81. $this->assertInstanceOf(\ArrayAccess::class, $matrix);
  82. $this->assertEquals([1, 2, 3], $matrix[0]);
  83. $this->assertEquals([2, 3, 4], $matrix[1]);
  84. $this->assertEquals([4, 5, 6], $matrix[2]);
  85. $this->assertEquals(1, $matrix[0][0]);
  86. $this->assertEquals(2, $matrix[0][1]);
  87. $this->assertEquals(3, $matrix[0][2]);
  88. $this->assertEquals(2, $matrix[1][0]);
  89. $this->assertEquals(3, $matrix[1][1]);
  90. $this->assertEquals(4, $matrix[1][2]);
  91. $this->assertEquals(4, $matrix[2][0]);
  92. $this->assertEquals(5, $matrix[2][1]);
  93. $this->assertEquals(6, $matrix[2][2]);
  94. }
  95. /**
  96. * @test Matrix implements \ArrayAccess
  97. */
  98. public function testArrayAccessInterfaceOffsetSet()
  99. {
  100. // Given
  101. $this->assertTrue($this->matrix->offsetExists(0));
  102. // Then
  103. $this->expectException(Exception\MatrixException::class);
  104. // When
  105. $this->matrix[0] = [4, 3, 5];
  106. }
  107. /**
  108. * @test Matrix implements \ArrayAccess
  109. * @throws \Exception
  110. */
  111. public function testArrayAccessInterfaceOffExists()
  112. {
  113. $this->assertTrue($this->matrix->offsetExists(0));
  114. }
  115. /**
  116. * @test Matrix implements \ArrayAccess
  117. * @throws \Exception
  118. */
  119. public function testArrayAccessOffsetUnsetException()
  120. {
  121. // Then
  122. $this->expectException(Exception\MatrixException::class);
  123. // When
  124. unset($this->matrix[0]);
  125. }
  126. /**
  127. * @test __toString returns the expected string representation of the matrix
  128. * @throws \Exception
  129. */
  130. public function testToString()
  131. {
  132. // When
  133. $string = $this->matrix->__toString();
  134. // Then
  135. $this->assertTrue(\is_string($string));
  136. $this->assertEquals(
  137. "[1, 2, 3]\n[2, 3, 4]\n[4, 5, 6]",
  138. $string
  139. );
  140. }
  141. /**
  142. * @test Matrix implements \JsonSerializable
  143. * @dataProvider dataProviderForJsonSerialize
  144. * @param array $A
  145. * @param string $json
  146. * @throws \Exception
  147. */
  148. public function testJsonSerialize(array $A, string $json)
  149. {
  150. // Given
  151. $A = MatrixFactory::create($A);
  152. // Then
  153. $this->assertEquals($json, json_encode($A));
  154. }
  155. public function dataProviderForJsonSerialize(): array
  156. {
  157. return [
  158. [
  159. [
  160. [1, 2, 3],
  161. [4, 5, 6],
  162. [7, 8, 9],
  163. ],
  164. '[[1,2,3],[4,5,6],[7,8,9]]',
  165. ],
  166. [
  167. [
  168. [1],
  169. ],
  170. '[[1]]',
  171. ],
  172. ];
  173. }
  174. }