123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395 |
- <?php
- namespace MathPHP\Tests\LinearAlgebra\MatrixNumeric;
- use MathPHP\LinearAlgebra\MatrixFactory;
- use MathPHP\LinearAlgebra\NumericMatrix;
- use MathPHP\LinearAlgebra\NumericSquareMatrix;
- use MathPHP\LinearAlgebra\Vector;
- use MathPHP\Exception;
- class MatrixArithmeticOperationsTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test add
- * @dataProvider dataProviderForAdd
- */
- public function testAdd(array $A, array $B, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- $R = MatrixFactory::create($R);
- // When
- $R2 = $A->add($B);
- // Then
- $this->assertEquals($R, $R2);
- }
- public function dataProviderForAdd(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [
- [1, 1, 1],
- [1, 1, 1],
- [1, 1, 1],
- ],
- [
- [2, 3, 4],
- [3, 4, 5],
- [4, 5, 6],
- ],
- ],
- [
- [
- [0, 1, 2],
- [9, 8, 7],
- ],
- [
- [6, 5, 4],
- [3, 4, 5],
- ],
- [
- [ 6, 6, 6],
- [12, 12, 12],
- ],
- ],
- ];
- }
- /**
- * @test add exception for rows
- * @throws \Exception
- */
- public function testAddExceptionRows()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2],
- [2, 3],
- ]);
- $B = MatrixFactory::create([
- [1, 2]
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // WHen
- $A->add($B);
- }
- /**
- * @test add exception for columns
- * @throws \Exception
- */
- public function testAddExceptionColumns()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- $B = MatrixFactory::create([
- [1, 2],
- [2, 3],
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A->add($B);
- }
- /**
- * @test directSum
- * @dataProvider dataProviderForDirectSum
- */
- public function testDirectSum(array $A, array $B, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- $R = MatrixFactory::create($R);
- // When
- $R2 = $A->directSum($B);
- // Then
- $this->assertEquals($R, $R2);
- }
- public function dataProviderForDirectSum(): array
- {
- return [
- [
- [
- [1, 3, 2],
- [2, 3, 1],
- ],
- [
- [1, 6],
- [0, 1],
- ],
- [
- [1, 3, 2, 0, 0],
- [2, 3, 1, 0, 0],
- [0, 0, 0, 1, 6],
- [0, 0, 0, 0, 1],
- ],
- ],
- ];
- }
- /**
- * @test kroneckerSum returns the expected SquareMatrix
- * @dataProvider dataProviderKroneckerSum
- * @param array A
- * @param array B
- * @param array $expected
- */
- public function testKroneckerSum(array $A, array $B, array $expected)
- {
- // Given
- $A = new NumericSquareMatrix($A);
- $B = new NumericSquareMatrix($B);
- $R = new NumericSquareMatrix($expected);
- // When
- $A⊕B = $A->kroneckerSum($B);
- // Then
- $this->assertEquals($R, $A⊕B);
- $this->assertEquals($R->getMatrix(), $A⊕B->getMatrix());
- $this->assertInstanceOf(NumericSquareMatrix::class, $A⊕B);
- }
- public function dataProviderKroneckerSum(): array
- {
- return [
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9],
- ],
- [
- [2, 2, 3, 2, 0, 0],
- [4, 6, 6, 0, 2, 0],
- [7, 8,10, 0, 0, 2],
- [3, 0, 0, 5, 2, 3],
- [0, 3, 0, 4, 9, 6],
- [0, 0, 3, 7, 8,13],
- ],
- ],
- [
- [
- [1, 1],
- [1, 1],
- ],
- [
- [1, 1],
- [1, 1],
- ],
- [
- [2, 1, 1, 0],
- [1, 2, 0, 1],
- [1, 0, 2, 1],
- [0, 1, 1, 2],
- ],
- ],
- [
- [
- [1, 1],
- [1, 1],
- ],
- [
- [2, 3],
- [4, 5],
- ],
- [
- [3, 3, 1, 0],
- [4, 6, 0, 1],
- [1, 0, 3, 3],
- [0, 1, 4, 6],
- ],
- ],
- [
- [
- [2, 3],
- [4, 5],
- ],
- [
- [1, 1],
- [1, 1],
- ],
- [
- [3, 1, 3, 0],
- [1, 3, 0, 3],
- [4, 0, 6, 1],
- [0, 4, 1, 6],
- ],
- ],
- ];
- }
- /**
- * @test kronecerSum throws a MatrixException if one of the matrices is not square
- * @dataProvider dataProviderForKroneckerSumSquareMatrixException
- * @param array A
- * @param array B
- */
- public function testKroneckerSumSquareMatrixException($A, $B)
- {
- // Given
- $A = new NumericMatrix($A);
- $B = new NumericMatrix($B);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A⊕B = $A->kroneckerSum($B);
- }
- public function dataProviderForKroneckerSumSquareMatrixException(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- ],
- [
- [1, 2],
- [2, 3],
- ]
- ],
- [
- [
- [1, 2],
- [2, 3],
- ],
- [
- [1, 2, 3],
- [2, 3, 4],
- ],
- ],
- [
- [
- [1, 2],
- [2, 3],
- [4, 5],
- ],
- [
- [1, 2, 3],
- [2, 3, 4],
- ],
- ],
- ];
- }
- /**
- * @test subtract
- * @dataProvider dataProviderForSubtract
- */
- public function testSubtract(array $A, array $B, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- $R = MatrixFactory::create($R);
- // When
- $R2 = $A->subtract($B);
- // Then
- $this->assertEquals($R, $R2);
- }
- public function dataProviderForSubtract(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [
- [1, 1, 1],
- [1, 1, 1],
- [1, 1, 1],
- ],
- [
- [ 0, 1, 2 ],
- [ 1, 2, 3 ],
- [ 2, 3, 4 ],
- ],
- ],
- [
- [
- [0, 1, 2],
- [9, 8, 7],
- ],
- [
- [6, 5, 4],
- [3, 4, 5],
- ],
- [
- [ -6, -4, -2 ],
- [ 6, 4, 2 ],
- ],
- ],
- ];
- }
- /**
- * @test subtract exception for rows
- * @throws \Exception
- */
- public function testSubtractExceptionRows()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2],
- [2, 3],
- ]);
- $B = MatrixFactory::create([
- [1, 2]
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A->subtract($B);
- }
- /**
- * @test subtract exception for columns
- * @throws \Exception
- */
- public function testSubtractExceptionColumns()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- $B = MatrixFactory::create([
- [1, 2],
- [2, 3],
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A->subtract($B);
- }
- /**
- * @test multiplication
- * @dataProvider dataProviderForMultiply
- * @param array $A
- * @param array $B
- * @param array $expected
- * @throws \Exception
- */
- public function testMultiply(array $A, array $B, array $expected)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- $expected = MatrixFactory::create($expected);
- // When
- $R = $A->multiply($B);
- // Then
- $this->assertEqualsWithDelta($expected, $R, 0.00001);
- $this->assertTrue($R->isEqual($expected));
- }
- public function dataProviderForMultiply(): array
- {
- return [
- [
- [
- [0]
- ],
- [
- [0]
- ],
- [
- [0]
- ],
- ],
- [
- [
- [0]
- ],
- [
- [1]
- ],
- [
- [0]
- ],
- ],
- [
- [
- [1]
- ],
- [
- [0]
- ],
- [
- [0]
- ],
- ],
- [
- [
- [1]
- ],
- [
- [2]
- ],
- [
- [2]
- ],
- ],
- [
- [
- [2]
- ],
- [
- [1]
- ],
- [
- [2]
- ],
- ],
- [
- [
- [2]
- ],
- [
- [3]
- ],
- [
- [6]
- ],
- ],
- [
- [
- [3]
- ],
- [
- [2]
- ],
- [
- [6]
- ],
- ],
- [
- [
- [1]
- ],
- [
- [1, 2, 3]
- ],
- [
- [1, 2, 3]
- ],
- ],
- [
- [
- [0]
- ],
- [
- [1, 2, 3]
- ],
- [
- [0, 0, 0]
- ],
- ],
- [
- [
- [4]
- ],
- [
- [1, 2, 3]
- ],
- [
- [4, 8, 12]
- ],
- ],
- [
- [
- [4]
- ],
- [
- [1, -3, 2]
- ],
- [
- [4, -12, 8]
- ],
- ],
- [
- [
- [1, -3, 2]
- ],
- [
- [2],
- [-1],
- [0],
- ],
- [
- [5],
- ],
- ],
- [
- [
- [1],
- [-3],
- [2],
- ],
- [
- [-1, -2],
- ],
- [
- [-1, -2],
- [3, 6],
- [-2, -4],
- ],
- ],
- [
- [
- [0, 1, 0]
- ],
- [
- [1, -2, 2],
- [4, 2, 0],
- [1, 2, 3],
- ],
- [
- [4, 2, 0],
- ],
- ],
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [0, 0],
- [0, 0],
- ],
- [
- [0, 0],
- [0, 0],
- ],
- ],
- [
- [
- [0, 0],
- [0, 0],
- ],
- [
- [1, 2],
- [3, 4],
- ],
- [
- [0, 0],
- [0, 0],
- ],
- ],
- [
- [
- [0, 1],
- [0, 0],
- ],
- [
- [0, 0],
- [1, 0],
- ],
- [
- [1, 0],
- [0, 0],
- ],
- ],
- [
- [
- [0, 0],
- [1, 0],
- ],
- [
- [0, 1],
- [0, 0],
- ],
- [
- [0, 0],
- [0, 1],
- ],
- ],
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [5, 6],
- [7, 8],
- ],
- [
- [19, 22],
- [43, 50],
- ],
- ],
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [2, 0],
- [1, 2],
- ],
- [
- [4, 4],
- [10, 8],
- ],
- ],
- [
- [
- [2, 0],
- [1, 2],
- ],
- [
- [1, 2],
- [3, 4],
- ],
- [
- [2, 4],
- [7, 10],
- ],
- ],
- [
- [
- [ 1, 0, -2 ],
- [ 0, 3, -1 ],
- ],
- [
- [ 0, 3 ],
- [ -2, -1 ],
- [ 0, 4 ],
- ],
- [
- [ 0, -5 ],
- [ -6, -7 ],
- ],
- ],
- [
- [
- [ 2, 3 ],
- [ 1, -5 ],
- ],
- [
- [ 4, 3, 6 ],
- [ 1, -2, 3 ],
- ],
- [
- [ 11, 0, 21 ],
- [ -1, 13, -9 ],
- ],
- ],
- [
- [
- [1, 2, 3],
- [4, 5, 6],
- ],
- [
- [7, 8],
- [9, 10],
- [11, 12],
- ],
- [
- [58, 64],
- [139, 154],
- ],
- ],
- [
- [
- [3, 4, 2],
- ],
- [
- [13, 9, 7, 15],
- [8, 7, 4, 6],
- [6, 4, 0, 3],
- ],
- [
- [83, 63, 37, 75],
- ],
- ],
- [
- [
- [0, 1, 2],
- [3, 4, 5],
- ],
- [
- [6, 7],
- [8, 9],
- [10, 11],
- ],
- [
- [28, 31],
- [100, 112],
- ],
- ],
- [
- [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9],
- ],
- [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9],
- ],
- [
- [30, 36, 42],
- [66, 81, 96],
- [102, 126, 150],
- ],
- ],
- [
- [
- [1, 2, 3, 4, 5, 6, 7, 8],
- [2, 3, 4, 5, 6, 7, 8, 9],
- [3, 4, 5, 6, 7, 8, 9, 1],
- [4, 5, 6, 7, 8, 9, 1, 2],
- [5, 6, 7, 8, 9, 1, 2, 3],
- [6, 7, 8, 9, 1, 2, 3, 4],
- [7, 8, 9, 1, 2, 3, 4, 5],
- [8, 9, 1, 2, 3, 4, 5, 6],
- [9, 1, 2, 3, 4, 5, 6, 7],
- ],
- [
- [7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8],
- [8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9],
- [9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1],
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2],
- [2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3],
- [3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4],
- [4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5],
- [5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6],
- ],
- [
- [150, 159, 177, 204, 240, 204, 177, 159, 150, 150 ,159],
- [189, 197, 214, 240, 284, 247, 219, 200, 190, 189, 197],
- [183, 181, 188, 204, 247, 281, 243, 214, 194, 183, 181],
- [186, 174, 171, 177, 219, 243, 276, 237, 207, 186, 174],
- [198, 176, 163, 159, 200, 214, 237, 269, 229, 198, 176],
- [219, 187, 164, 150, 190, 194, 207, 229, 260, 219, 187],
- [249, 207, 174, 150, 189, 183, 186, 198, 219, 249, 207],
- [207, 236, 193, 159, 197, 181, 174, 176, 187, 207, 236],
- [174, 193, 221, 177, 214, 188, 171, 163, 164, 174, 193],
- ],
- ],
- [
- [
- [1.4, 5.3, 4.8],
- [3.2, 2.3, 9.05],
- [9.54, 0.2, 1.85],
- ],
- [
- [3.5, 5.6, 6.7],
- [6.5, 4.2, 9.05],
- [0.6, 0.236, 4.5],
- ],
- [
- [42.23, 31.2328, 78.945],
- [31.58, 29.7158, 82.980],
- [35.80, 54.7006, 74.053],
- ],
- ],
- ];
- }
- /**
- * @test multiply vector
- * @dataProvider dataProviderForMultiplyVector
- */
- public function testMultiplyVector(array $A, array $B, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = new Vector($B);
- $R = MatrixFactory::create($R);
- // When
- $R2 = $A->multiply($B);
- // Then
- $this->assertEquals($R, $R2);
- }
- public function dataProviderForMultiplyVector(): array
- {
- return [
- [
- [
- [1],
- ],
- [1],
- [
- [1],
- ],
- ],
- [
- [
- [2],
- ],
- [3],
- [
- [6],
- ],
- ],
- [
- [
- [1, 2],
- [2, 3],
- ],
- [4, 5],
- [
- [14],
- [23],
- ],
- ],
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [1, 2, 3],
- [
- [14],
- [20],
- [26],
- ],
- ],
- [
- [
- [3, 6, 5],
- [1, 7, 5],
- [2, 3, 2],
- ],
- [1, 5, 4],
- [
- [53],
- [56],
- [25],
- ],
- ],
- [
- [
- [1, 1, 1],
- [2, 2, 2],
- ],
- [1, 2, 3],
- [
- [6],
- [12],
- ],
- ],
- [
- [
- [1, 1, 1],
- [2, 2, 2],
- [3, 3, 3],
- [4, 4, 4]
- ],
- [1, 2, 3],
- [
- [6],
- [12],
- [18],
- [24],
- ],
- ],
- ];
- }
- /**
- * @test multiple exception
- * @throws \Exception
- */
- public function testMultiplyExceptionDimensionsDoNotMatch()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- $B = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A->multiply($B);
- }
- /**
- * @test multiple exception
- * @throws \Exception
- */
- public function testMultiplyExceptionNotMatrixOrVector()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ]);
- $B = [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ];
- // Then
- $this->expectException(Exception\IncorrectTypeException::class);
- // When
- $A->multiply($B);
- }
- /**
- * @test scalarMultiply
- * @dataProvider dataProviderForScalarMultiply
- */
- public function testScalarMultiply(array $A, $k, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $R = MatrixFactory::create($R);
- // When
- $kA = $A->scalarMultiply($k);
- // Then
- $this->assertEquals($R, $kA);
- }
- public function dataProviderForScalarMultiply(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ], 3,
- [
- [3, 6, 9],
- [6, 9, 12],
- [9, 12, 15],
- ],
- ],
- [
- [
- [1, 2, 3],
- ], 3,
- [
- [3, 6, 9],
- ],
- ],
- [
- [
- [1],
- [2],
- [3],
- ], 3,
- [
- [3],
- [6],
- [9],
- ],
- ],
- [
- [
- [1],
- ], 3,
- [
- [3],
- ],
- ],
- ];
- }
- /**
- * @test negate
- * @dataProvider dataProviderForNegate
- * @param array $A
- * @param array $expected
- * @throws \Exception
- */
- public function testNegate(array $A, array $expected)
- {
- // Given
- $A = MatrixFactory::create($A);
- $expected = MatrixFactory::create($expected);
- // When
- $−A = $A->negate();
- // Then
- $this->assertEquals($expected, $−A);
- }
- /**
- * @return array [A, −A]
- */
- public function dataProviderForNegate(): array
- {
- return [
- [
- [
- [0]
- ],
- [
- [0]
- ],
- ],
- [
- [
- [1]
- ],
- [
- [-1]
- ],
- ],
- [
- [
- [-1]
- ],
- [
- [1]
- ],
- ],
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [-1, -2],
- [-3, -4],
- ],
- ],
- [
- [
- [1, -2, 3],
- [-4, 5, -6],
- [7, -8, 9],
- ],
- [
- [-1, 2, -3],
- [4, -5, 6],
- [-7, 8, -9],
- ]
- ],
- ];
- }
- /**
- * @test scalarDivide
- * @dataProvider dataProviderForScalarDivide
- */
- public function testScalarDivide(array $A, $k, array $R)
- {
- // Given
- $A = MatrixFactory::create($A);
- $R = MatrixFactory::create($R);
- // When
- $divided = $A->scalarDivide($k);
- // Then
- $this->assertEquals($R, $divided);
- }
- public function dataProviderForScalarDivide(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ], 3,
- [
- [1 / 3, 2 / 3, 1],
- [2 / 3, 1, 4 / 3],
- [1, 4 / 3, 5 / 3],
- ],
- ],
- [
- [
- [3, 6, 9],
- ], 3,
- [
- [1, 2, 3],
- ],
- ],
- [
- [
- [1],
- [2],
- [3],
- ], 3,
- [
- [1 / 3],
- [2 / 3],
- [1],
- ],
- ],
- [
- [
- [1],
- ], 3,
- [
- [1 / 3],
- ],
- ],
- ];
- }
- /**
- * @test scalarDivide by zero
- * @throws \Exception
- */
- public function testScalarDivideByZero()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- // Then
- $this->expectException(Exception\BadParameterException::class);
- // When
- $A->scalarDivide(0);
- }
- /**
- * @test hadamardProduct
- * @dataProvider dataProviderForHadamardProduct
- */
- public function testHadamardProduct(array $A, array $B, array $expected)
- {
- // Given
- $A = MatrixFactory::create($A);
- $B = MatrixFactory::create($B);
- $expected = MatrixFactory::create($expected);
- // When
- $A∘B = $A->hadamardProduct($B);
- // Then
- $this->assertEquals($expected, $A∘B);
- }
- public function dataProviderForHadamardProduct(): array
- {
- return [
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [
- [1, 4, 9],
- [4, 9, 16],
- [9, 16, 25],
- ]
- ],
- [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- ],
- [
- [6, 6, 4],
- [8, 7, 8],
- [3, 1, 7],
- ],
- [
- [6, 12, 12],
- [16, 21, 32],
- [9, 4, 35],
- ]
- ],
- ];
- }
- /**
- * @test hadamardProduct dimensions don't match
- * @throws \Exception
- */
- public function testHadamardProductDimensionsDoNotMatch()
- {
- // Given
- $A = MatrixFactory::create([
- [1, 2, 3],
- [2, 3, 4],
- ]);
- $B = MatrixFactory::create([
- [1, 2, 3, 4],
- [2, 3, 4, 5],
- ]);
- // Then
- $this->expectException(Exception\MatrixException::class);
- // When
- $A->hadamardProduct($B);
- }
- /**
- * @test kroneckerProduct
- * @dataProvider dataProviderForKroneckerProduct
- */
- public function testKroneckerProduct(array $A, array $B, array $expected)
- {
- // Given
- $A = new NumericMatrix($A);
- $B = new NumericMatrix($B);
- $expected = new NumericMatrix($expected);
- // When
- $A⊗B = $A->kroneckerProduct($B);
- // Then
- $this->assertEquals($expected->getMatrix(), $A⊗B->getMatrix());
- }
- public function dataProviderForKroneckerProduct(): array
- {
- return [
- [
- [
- [1, 2],
- [3, 4],
- ],
- [
- [0, 5],
- [6, 7],
- ],
- [
- [0, 5, 0, 10],
- [6, 7, 12, 14],
- [0, 15, 0, 20],
- [18, 21, 24, 28],
- ],
- ],
- [
- [
- [1, 1],
- [1, -1],
- ],
- [
- [1, 1],
- [1, -1],
- ],
- [
- [1, 1, 1, 1],
- [1, -1, 1, -1],
- [1, 1, -1, -1],
- [1, -1, -1, 1],
- ],
- ],
- [
- [
- [1, 2, 3],
- [4, 5, 6],
- ],
- [
- [7, 8],
- [9, 10],
- ],
- [
- [7, 8, 14, 16, 21, 24],
- [9, 10, 18, 20, 27, 30],
- [28, 32, 35, 40, 42, 48],
- [36, 40, 45, 50, 54, 60],
- ],
- ],
- [
- [
- [2, 3],
- [5, 4],
- ],
- [
- [5, 5],
- [4, 4],
- [2, 9]
- ],
- [
- [10, 10, 15, 15],
- [8, 8, 12, 12],
- [4, 18, 6, 27],
- [25, 25, 20, 20],
- [20, 20, 16, 16],
- [10, 45, 8, 36],
- ],
- ],
- [
- [
- [2, 3],
- [5, 4],
- ],
- [
- [5, 4, 2],
- [5, 4, 9],
- ],
- [
- [10, 8, 4, 15, 12, 6],
- [10, 8, 18, 15, 12, 27],
- [25, 20, 10, 20, 16, 8],
- [25, 20, 45, 20, 16, 36],
- ],
- ],
- ];
- }
- }
|