123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- namespace MathPHP\Tests\LinearAlgebra\Matrix\Object;
- use MathPHP\Expression\Polynomial;
- use MathPHP\LinearAlgebra\MatrixFactory;
- use MathPHP\LinearAlgebra\ObjectSquareMatrix;
- use MathPHP\LinearAlgebra\Vector;
- use MathPHP\Number\Complex;
- use MathPHP\Exception;
- use MathPHP\Number\ObjectArithmetic;
- class ObjectSquareMatrixTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test The constructor throws the proper exceptions
- * @dataProvider dataProviderConstructorException
- * @param array $A
- * @param string $exception
- */
- public function testMatrixConstructorException(array $A, string $exception)
- {
- // Then
- $this->expectException($exception);
- // When
- $A = new ObjectSquareMatrix($A);
- }
- public function dataProviderConstructorException(): array
- {
- return [
- 'rows have different types' => [
- [[new \stdClass()]],
- Exception\IncorrectTypeException::class,
- ],
- 'columns have different types' => [
- [[new \stdClass(), new Polynomial([1, 2, 3])],
- [new \stdClass(), new Polynomial([1, 2, 3])]],
- Exception\IncorrectTypeException::class,
- ],
- 'not square' => [
- [
- [new Polynomial([1, 2]), new Polynomial([2, 1])],
- ],
- Exception\MatrixException::class,
- ],
- ];
- }
- /**
- * @test Addition throws the proper exceptions
- * @dataProvider dataProviderForArithmeticExceptions
- * @param array $A
- * @param ObjectArithmetic $B
- * @param string $exception
- */
- public function testMatrixAddException(array $A, ObjectArithmetic $B, string $exception)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- // Then
- $this->expectException($exception);
- // When
- $C = $A->add($B);
- }
- /**
- * @test Subtraction throws the proper exceptions
- * @dataProvider dataProviderForArithmeticExceptions
- * @param array $A
- * @param ObjectArithmetic $B
- * @param string $exception
- */
- public function testMatrixSubtractException(array $A, ObjectArithmetic $B, string $exception)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- // Then
- $this->expectException($exception);
- // When
- $C = $A->subtract($B);
- }
- /**
- * @test Multiplication throws the proper exceptions
- * @dataProvider dataProviderForArithmeticExceptions
- * @param array $A
- * @param ObjectArithmetic $B
- * @param string $exception
- */
- public function testMatrixMultiplyException(array $A, ObjectArithmetic $B, string $exception)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- // Then
- $this->expectException($exception);
- // When
- $C = $A->multiply($B);
- }
- public function dataProviderForArithmeticExceptions(): array
- {
- return[
- [ // Different Sizes
- [[new Polynomial([1, 2, 3]), new Polynomial([1, 2, 3])],
- [new Polynomial([1, 2, 3]), new Polynomial([1, 2, 3])]],
- MatrixFactory::create([[new Polynomial([1, 2, 3])]]),
- Exception\MatrixException::class,
- ],
- [ // Different Types
- [[new Polynomial([1, 2, 3])]],
- new ObjectSquareMatrix([[new Complex(1, 2)]]),
- Exception\IncorrectTypeException::class,
- ],
- [ // Not a Matrix
- [[new Polynomial([1, 2, 3])]],
- new Complex(1, 2),
- Exception\IncorrectTypeException::class,
- ],
- ];
- }
- /**
- * @test isEqual
- * @dataProvider dataProviderisEqual
- * @param array $A
- * @param array $B
- * @param bool $expected
- * @throws \Exception
- */
- public function testIsEqual(array $A, array $B, bool $expected)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- // When
- $comparison = $A->isEqual($B);
- // Then
- $this->assertEquals($expected, $comparison);
- }
- /**
- * @return array
- */
- public function dataProviderisEqual()
- {
- return [
- 'same' => [
- [[new Polynomial([1, 0])]],
- [[new Polynomial([1, 0])]],
- true,
- ],
- 'different types' => [
- [[new Polynomial([1, 0])]],
- [[1]],
- false,
- ],
- 'different contents' => [
- [[new Polynomial([1, 0])]],
- [[new Polynomial([1, 1])]],
- false,
- ],
- 'different shapes' => [
- [[new Polynomial([1, 0]), new Polynomial([1, 0])]],
- [[new Polynomial([1, 0])], [new Polynomial([1, 0])]],
- false,
- ],
- ];
- }
- /**
- * @test add
- * @dataProvider dataProviderAdd
- * @param array $A
- * @param array $B
- * @param array $expected
- * @throws \Exception
- */
- public function testAdd(array $A, array $B, array $expected)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- $B = new ObjectSquareMatrix($B);
- // And
- $expected = matrixFactory::create($expected);
- // When
- $sum = $A->add($B);
- // Then
- $this->assertEquals($expected, $sum);
- }
- /**
- * @return array
- */
- public function dataProviderAdd(): array
- {
- return [
- [
- [
- [new Polynomial([1, 0]), new Polynomial([0, 0])],
- [new Polynomial([0, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([1, 0]), new Polynomial([1, 1])],
- [new Polynomial([1, 1]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([2, 0]), new Polynomial([1, 1])],
- [new Polynomial([1, 1]), new Polynomial([2, 0])],
- ],
- ],
- [
- [
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([1, 0]), new Polynomial([1, 1])],
- [new Polynomial([1, 1]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([2, 0]), new Polynomial([2, 1])],
- [new Polynomial([2, 1]), new Polynomial([2, 0])],
- ],
- ],
- ];
- }
- /**
- * @test subtract
- * @dataProvider dataProviderSubtract
- * @param array $A
- * @param array $B
- * @param array $expected
- * @throws \Exception
- */
- public function testSubtract(array $A, array $B, array $expected)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- $B = new ObjectSquareMatrix($B);
- $expected = new ObjectSquareMatrix($expected);
- // When
- $difference = $A->subtract($B);
- // Then
- $this->assertEquals($expected, $difference);
- }
- /**
- * @return array
- */
- public function dataProviderSubtract(): array
- {
- return [
- [
- [
- [new Polynomial([1, 0]), new Polynomial([0, 0])],
- [new Polynomial([0, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([2, 1]), new Polynomial([2, 1])],
- [new Polynomial([1, -1]), new Polynomial([-1, 0])],
- ],
- [
- [new Polynomial([-1, -1]), new Polynomial([-2, -1])],
- [new Polynomial([-1, 1]), new Polynomial([2, 0])],
- ],
- ],
- [
- [
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([-2, 0]), new Polynomial([1, -1])],
- [new Polynomial([-2, 2]), new Polynomial([4, 4])],
- ],
- [
- [new Polynomial([3, 0]), new Polynomial([0, 1])],
- [new Polynomial([3, -2]), new Polynomial([-3, -4])],
- ],
- ],
- ];
- }
- /**
- * @test multiply
- * @dataProvider dataProviderMul
- * @param array $A
- * @param array $B
- * @param array $expected
- */
- public function testMul(array $A, array $B, array $expected)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- $B = new ObjectSquareMatrix($B);
- // And
- $expected = matrixFactory::create($expected);
- // When
- $sum = $A->multiply($B);
- // Then
- $this->assertEquals($expected, $sum);
- }
- /**
- * @return array
- */
- public function dataProviderMul(): array
- {
- return [
- [
- [
- [new Polynomial([1, 0]), new Polynomial([0, 0])],
- [new Polynomial([0, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([1, 0]), new Polynomial([1, 1])],
- [new Polynomial([1, 1]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([1, 0, 0]), new Polynomial([1, 1, 0])],
- [new Polynomial([1, 1, 0]), new Polynomial([1, 0, 0])],
- ],
- ],
- [
- [
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([1, 0]), new Polynomial([1, 1])],
- [new Polynomial([1, 1]), new Polynomial([1, 0])],
- ],
- [
- [new Polynomial([2, 1, 0]), new Polynomial([2, 1, 0])],
- [new Polynomial([2, 1, 0]), new Polynomial([2, 1, 0])],
- ],
- ],
- ];
- }
- /**
- * @test Matrix can be multiplied by a vector
- * @dataProvider dataProviderMultiplyVector
- * @param array $A
- * @param array $B
- * @param array $expected
- */
- public function testMultiplyVector(array $A, array $B, array $expected)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- $B = new Vector($B);
- // When
- $sum = $A->multiply($B);
- // Then
- $expected = MatrixFactory::create($expected);
- $this->assertEquals($expected, $sum);
- }
- public function dataProviderMultiplyVector(): array
- {
- return [
- [
- [
- [new Polynomial([1, 0]), new Polynomial([0, 0])],
- [new Polynomial([0, 0]), new Polynomial([1, 0])],
- ],
- [new Polynomial([1, 0]), new Polynomial([1, 1])],
- [
- [new Polynomial([1, 0, 0])],
- [new Polynomial([1, 1, 0])],
- ],
- ],
- ];
- }
- /**
- * @test det
- * @dataProvider dataProviderDet
- * @param array $A
- * @param Polynomial $expected
- */
- public function testDet(array $A, Polynomial $expected)
- {
- // Given
- $A = new ObjectSquareMatrix($A);
- // When
- $det = $A->det();
- // Then
- $this->assertEquals($det, $expected);
- // And when
- $det = $A->det();
- // Then
- $this->assertEquals($expected, $det);
- }
- /**
- * @return array
- */
- public function dataProviderDet(): array
- {
- return [
- [
- [
- [new Polynomial([1, 0])],
- ],
- new Polynomial([1, 0]),
- ],
- [
- [
- [new Polynomial([1, 0]), new Polynomial([1, 0])],
- [new Polynomial([1, 0]), new Polynomial([0, 4])],
- ],
- new Polynomial([-1, 4, 0]),
- ],
- ];
- }
- }
|