SquareMatrixTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Numeric;
  3. use MathPHP\LinearAlgebra\NumericSquareMatrix;
  4. use MathPHP\LinearAlgebra\NumericMatrix;
  5. use MathPHP\Exception;
  6. class SquareMatrixTest extends \PHPUnit\Framework\TestCase
  7. {
  8. use \MathPHP\Tests\LinearAlgebra\Fixture\MatrixDataProvider;
  9. /**
  10. * @test Constructor constructs a proper SquareMatrix
  11. * @dataProvider dataProviderForSquareMatrix
  12. * @param array $A
  13. * @throws \Exception
  14. */
  15. public function testConstructor(array $A)
  16. {
  17. $S = new NumericSquareMatrix($A);
  18. $M = new NumericMatrix($A);
  19. $this->assertInstanceOf(NumericSquareMatrix::class, $S);
  20. $this->assertInstanceOf(NumericMatrix::class, $S);
  21. $m = $S->getM();
  22. for ($i = 0; $i < $m; $i++) {
  23. $this->assertEquals($M[$i], $S[$i]);
  24. }
  25. $m = $M->getM();
  26. for ($i = 0; $i < $m; $i++) {
  27. $this->assertEquals($M[$i], $S[$i]);
  28. }
  29. }
  30. /**
  31. * @test SquareMatrix throws a MatrixException if the input is not a square array.
  32. */
  33. public function testConstructorExceptionNotSquareMatrix()
  34. {
  35. $A = [
  36. [1, 2, 3],
  37. [2, 3, 4],
  38. ];
  39. $this->expectException(Exception\MatrixException::class);
  40. $M = new NumericSquareMatrix($A);
  41. }
  42. /**
  43. * @test getMatrix returns the expected matrix.
  44. * @dataProvider dataProviderForSquareMatrix
  45. * @param array $A
  46. * @throws \Exception
  47. */
  48. public function testGetMatrix(array $A)
  49. {
  50. $S = new NumericSquareMatrix($A);
  51. $M = new NumericMatrix($A);
  52. $this->assertEquals($M->getMatrix(), $S->getMatrix());
  53. }
  54. /**
  55. * @test isSquare always returns true for a SquareMatrix
  56. * @dataProvider dataProviderForSquareMatrix
  57. * @param array $A
  58. * @throws \Exception
  59. */
  60. public function testIsSquare(array $A)
  61. {
  62. $S = new NumericSquareMatrix($A);
  63. $M = new NumericMatrix($A);
  64. $this->assertTrue($S->isSquare());
  65. }
  66. }