DiagonalMatrixTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Other;
  3. use MathPHP\Exception\MatrixException;
  4. use MathPHP\LinearAlgebra\NumericDiagonalMatrix;
  5. use MathPHP\LinearAlgebra\NumericMatrix;
  6. use MathPHP\LinearAlgebra\MatrixFactory;
  7. class DiagonalMatrixTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @test constructor builds the expected DiagonalMatrix
  11. * @dataProvider dataProviderMulti
  12. * @param array $A
  13. * @param array $R
  14. * @throws \Exception
  15. */
  16. public function testConstructor(array $A, array $R)
  17. {
  18. // Given
  19. $R = new NumericMatrix($R);
  20. // When
  21. $D = MatrixFactory::diagonal($A);
  22. // Then
  23. $this->assertInstanceOf(NumericDiagonalMatrix::class, $D);
  24. $this->assertTrue($R->isEqual($D));
  25. $this->assertTrue($D->isEqual($R));
  26. }
  27. /**
  28. * @test getMatrix returns the expected array
  29. * @dataProvider dataProviderMulti
  30. * @param array $A
  31. * @param array $R
  32. * @throws \Exception
  33. */
  34. public function testGetMatrix(array $A, array $R)
  35. {
  36. // Given
  37. $D = MatrixFactory::diagonal($A);
  38. // Then
  39. $this->assertEquals($R, $D->getMatrix());
  40. }
  41. /**
  42. * @test isSquare returns true
  43. * @dataProvider dataProviderMulti
  44. * @param array $A
  45. * @throws \Exception
  46. */
  47. public function testIsSquare(array $A)
  48. {
  49. // Given
  50. $D = MatrixFactory::diagonal($A);
  51. // Then
  52. $this->assertTrue($D->isSquare());
  53. }
  54. /**
  55. * @test isSymmetric returns true
  56. * @dataProvider dataProviderMulti
  57. * @param array $A
  58. * @throws \Exception
  59. */
  60. public function testIsSymmetric(array $A)
  61. {
  62. // Given
  63. $D = MatrixFactory::diagonal($A);
  64. // Then
  65. $this->assertTrue($D->isSymmetric());
  66. }
  67. /**
  68. * @test isLowerTriangular returns true
  69. * @dataProvider dataProviderMulti
  70. * @param array $A
  71. * @throws \Exception
  72. */
  73. public function testIsLowerTriangular(array $A)
  74. {
  75. // Given
  76. $D = MatrixFactory::diagonal($A);
  77. // Then
  78. $this->assertTrue($D->isLowerTriangular());
  79. }
  80. /**
  81. * @test isUpperTriangular returns true
  82. * @dataProvider dataProviderMulti
  83. * @param array $A
  84. * @throws \Exception
  85. */
  86. public function testIsUpperTriangular(array $A)
  87. {
  88. // Given
  89. $D = MatrixFactory::diagonal($A);
  90. // Then
  91. $this->assertTrue($D->isUpperTriangular());
  92. }
  93. /**
  94. * @test isTriangular returns true
  95. * @dataProvider dataProviderMulti
  96. * @param array $A
  97. * @throws \Exception
  98. */
  99. public function testIsTriangular(array $A)
  100. {
  101. // Given
  102. $D = MatrixFactory::diagonal($A);
  103. // Then
  104. $this->assertTrue($D->isTriangular());
  105. }
  106. /**
  107. * @test isDiagonal returns true
  108. * @dataProvider dataProviderMulti
  109. * @param array $A
  110. * @throws \Exception
  111. */
  112. public function testIsDiagonal(array $A)
  113. {
  114. // Given
  115. $D = MatrixFactory::diagonal($A);
  116. // Then
  117. $this->assertTrue($D->isDiagonal());
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function dataProviderMulti(): array
  123. {
  124. return [
  125. [
  126. [1, 2, 3],
  127. [
  128. [1, 0, 0],
  129. [0, 2, 0],
  130. [0, 0, 3],
  131. ],
  132. ],
  133. [
  134. [1],
  135. [
  136. [1]
  137. ]
  138. ],
  139. ];
  140. }
  141. /**
  142. * @test Construction error - square matrix
  143. * @throws MatrixException
  144. */
  145. public function testConstructionExceptionNotSquare()
  146. {
  147. // Given
  148. $A = [
  149. [1, 0, 0],
  150. [0, 2, 0],
  151. ];
  152. // Then
  153. $this->expectException(MatrixException::class);
  154. // When
  155. $matrix = new NumericDiagonalMatrix($A);
  156. }
  157. /**
  158. * @test Construction error - not diagonal matrix
  159. * @throws MatrixException
  160. */
  161. public function testConstructionExceptionNotDiagonal()
  162. {
  163. // Given
  164. $A = [
  165. [1, 1],
  166. [3, 2],
  167. ];
  168. // Then
  169. $this->expectException(MatrixException::class);
  170. // When
  171. $matrix = new NumericDiagonalMatrix($A);
  172. }
  173. }