From 0cd2fb0dd18015fd6a30438626cbec635bc06ba8 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Mon, 18 Sep 2023 14:54:16 -0500 Subject: [PATCH 01/10] Initial commit --- ext/php_tensor.h | 2 +- tests/VectorTest.php | 954 ++++++++++++++++++++++++++----------------- 2 files changed, 586 insertions(+), 370 deletions(-) diff --git a/ext/php_tensor.h b/ext/php_tensor.h index 73c7087..2d7a15e 100644 --- a/ext/php_tensor.h +++ b/ext/php_tensor.h @@ -13,7 +13,7 @@ #define PHP_TENSOR_NAME "tensor" #define PHP_TENSOR_VERSION "3.0.3" #define PHP_TENSOR_EXTNAME "tensor" -#define PHP_TENSOR_AUTHOR "Andrew DalPino" +#define PHP_TENSOR_AUTHOR "The Rubix ML Community" #define PHP_TENSOR_ZEPVERSION "0.17.0-$Id$" #define PHP_TENSOR_DESCRIPTION "A library and extension that provides objects for scientific computing in PHP." diff --git a/tests/VectorTest.php b/tests/VectorTest.php index 2fef4c5..f26fd21 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -14,64 +14,29 @@ use Tensor\ColumnVector; use Tensor\Trigonometric; use PHPUnit\Framework\TestCase; +use Generator; /** * @covers \Tensor\Vector */ class VectorTest extends TestCase { - /** - * @var \Tensor\Vector - */ - protected \Tensor\Vector $a; - - /** - * @var \Tensor\Vector - */ - protected \Tensor\Vector $b; - - /** - * @var \Tensor\Vector - */ - protected \Tensor\Vector $c; - - /** - * @var \Tensor\Matrix - */ - protected \Tensor\Matrix $d; - - /** - * @before - */ - public function setUp() : void - { - $this->a = Vector::build([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $this->b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $this->c = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $this->d = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - } - /** * @test */ public function build() : void { - $this->assertInstanceOf(Vector::class, $this->a); - $this->assertInstanceOf(Tensor::class, $this->a); - $this->assertInstanceOf(ArrayLike::class, $this->a); - $this->assertInstanceOf(Arithmetic::class, $this->a); - $this->assertInstanceOf(Comparable::class, $this->a); - $this->assertInstanceOf(Algebraic::class, $this->a); - $this->assertInstanceOf(Trigonometric::class, $this->a); - $this->assertInstanceOf(Statistical::class, $this->a); - $this->assertInstanceOf(Special::class, $this->a); + $vector = Vector::build([1, 2, 3, 4, 5]); + + $this->assertInstanceOf(Vector::class, $vector); + $this->assertInstanceOf(Tensor::class, $vector); + $this->assertInstanceOf(ArrayLike::class, $vector); + $this->assertInstanceOf(Arithmetic::class, $vector); + $this->assertInstanceOf(Comparable::class, $vector); + $this->assertInstanceOf(Algebraic::class, $vector); + $this->assertInstanceOf(Trigonometric::class, $vector); + $this->assertInstanceOf(Statistical::class, $vector); + $this->assertInstanceOf(Special::class, $vector); } /** @@ -79,12 +44,11 @@ public function build() : void */ public function zeros() : void { - $z = Vector::zeros(4); + $zeros = Vector::zeros(4); $expected = [0, 0, 0, 0]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $zeros->asArray()); } /** @@ -92,12 +56,11 @@ public function zeros() : void */ public function ones() : void { - $z = Vector::ones(4); + $ones = Vector::ones(4); $expected = [1, 1, 1, 1]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $ones->asArray()); } /** @@ -105,12 +68,11 @@ public function ones() : void */ public function fill() : void { - $z = Vector::fill(16, 4); + $vector = Vector::fill(16, 4); $expected = [16, 16, 16, 16]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -118,10 +80,9 @@ public function fill() : void */ public function rand() : void { - $z = Vector::rand(4); + $vector = Vector::rand(4); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(4, $z); + $this->assertCount(4, $vector); } /** @@ -129,10 +90,9 @@ public function rand() : void */ public function gaussian() : void { - $z = Vector::gaussian(4); + $vector = Vector::gaussian(4); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(4, $z); + $this->assertCount(4, $vector); } /** @@ -140,10 +100,9 @@ public function gaussian() : void */ public function poisson() : void { - $z = Vector::poisson(10, 2.); + $vector = Vector::poisson(4, 2.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(10, $z); + $this->assertCount(4, $vector); } /** @@ -151,10 +110,9 @@ public function poisson() : void */ public function uniform() : void { - $z = Vector::uniform(5); + $vector = Vector::uniform(4); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(5, $z); + $this->assertCount(4, $vector); } /** @@ -162,12 +120,11 @@ public function uniform() : void */ public function range() : void { - $z = Vector::range(5.0, 12.0, 2.0); + $vector = Vector::range(5.0, 12.0, 2.0); $expected = [5.0, 7.0, 9.0, 11.0]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -175,26 +132,38 @@ public function range() : void */ public function linspace() : void { - $z = Vector::linspace(-5, 5, 10); + $vector = Vector::linspace(-5.0, 5.0, 10); $expected = [ -5.0, -3.888888888888889, -2.7777777777777777, -1.6666666666666665, -0.5555555555555554, 0.5555555555555558, 1.666666666666667, 2.777777777777778, 3.8888888888888893, 5.0, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(10, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $vector->asArray()); } /** * @test + * @dataProvider shapeProvider + * + * @param \Tensor\Vector $vector + * @param array $expected */ - public function shape() : void + public function shape(Vector $vector, array $expected) : void { - $this->assertEquals([8], $this->a->shape()); - $this->assertEquals([8], $this->b->shape()); - $this->assertEquals([6], $this->c->shape()); + $this->assertEquals($expected, $vector->shape()); + } + + /** + * @return \Generator + */ + public function shapeProvider() : Generator + { + yield [Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), [8]]; + + yield [Vector::quick([0.25]), [1]]; + + yield [Vector::quick([]), [0]]; } /** @@ -202,9 +171,9 @@ public function shape() : void */ public function shapeString() : void { - $this->assertEquals('8', $this->a->shapeString()); - $this->assertEquals('8', $this->b->shapeString()); - $this->assertEquals('6', $this->c->shapeString()); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals('8', $vector->shapeString()); } /** @@ -212,9 +181,9 @@ public function shapeString() : void */ public function size() : void { - $this->assertEquals(8, $this->a->size()); - $this->assertEquals(8, $this->b->size()); - $this->assertEquals(6, $this->c->size()); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(8, $vector->size()); } /** @@ -222,9 +191,9 @@ public function size() : void */ public function m() : void { - $this->assertEquals(1, $this->a->m()); - $this->assertEquals(1, $this->b->m()); - $this->assertEquals(1, $this->c->m()); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(1, $vector->m()); } /** @@ -232,9 +201,9 @@ public function m() : void */ public function n() : void { - $this->assertEquals(8, $this->a->n()); - $this->assertEquals(8, $this->b->n()); - $this->assertEquals(6, $this->c->n()); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(8, $vector->n()); } /** @@ -242,11 +211,11 @@ public function n() : void */ public function asArray() : void { - $z = $this->a->asArray(); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-15, 25, 35, -36, -72, 89, 106, 45]; + $expected = [-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]; - $this->assertEquals($expected, $z); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -254,12 +223,14 @@ public function asArray() : void */ public function asRowMatrix() : void { - $z = $this->a->asRowMatrix(); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [[-15, 25, 35, -36, -72, 89, 106, 45]]; + $matrix = $vector->asRowMatrix(); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [[-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]]; + + $this->assertInstanceOf(Matrix::class, $matrix); + $this->assertEquals($expected, $matrix->asArray()); } /** @@ -267,12 +238,14 @@ public function asRowMatrix() : void */ public function asColumnMatrix() : void { - $z = $this->a->asColumnMatrix(); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [[-15], [25], [35], [-36], [-72], [89], [106], [45]]; + $matrix = $vector->asColumnMatrix(); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [[-15.0], [25.0], [35.0], [-36.0], [-72.0], [89.0], [106.0], [45.0]]; + + $this->assertInstanceOf(Matrix::class, $matrix); + $this->assertEquals($expected, $matrix->asArray()); } /** @@ -280,18 +253,20 @@ public function asColumnMatrix() : void */ public function reshape() : void { - $z = $this->a->reshape(4, 2); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $matrix = $vector->reshape(4, 2); $expected = [ - [-15, 25], - [35, -36], - [-72, 89], - [106, 45], + [-15.0, 25.0], + [35.0, -36.0], + [-72.0, 89.0], + [106.0, 45.0], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals([4, 2], $z->shape()); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $matrix); + $this->assertEquals([4, 2], $matrix->shape()); + $this->assertEquals($expected, $matrix->asArray()); } /** @@ -299,12 +274,14 @@ public function reshape() : void */ public function transpose() : void { - $z = $this->a->transpose(); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $outcome = [-15, 25, 35, -36, -72, 89, 106, 45]; + $vector = $vector->transpose(); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($outcome, $z->asArray()); + $expected = [-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]; + + $this->assertInstanceOf(ColumnVector::class, $vector); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -312,14 +289,17 @@ public function transpose() : void */ public function map() : void { - $z = $this->a->map(function ($value) { - return $value > 0. ? 1 : 0; - }); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $sign = function ($value) { + return $value >= 0.0 ? 1 : 0; + }; + + $vector = $vector->map($sign); $expected = [0, 1, 1, 0, 0, 1, 1, 1]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -327,15 +307,16 @@ public function map() : void */ public function reciprocal() : void { - $z = $this->a->reciprocal(); + $vector = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $vector = $vector->reciprocal(); $expected = [ -0.06666666666666667, 0.04, 0.02857142857142857, -0.027777777777777776, -0.013888888888888888, 0.011235955056179775, 0.009433962264150943, 0.022222222222222223, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $vector->asArray()); } /** @@ -343,7 +324,13 @@ public function reciprocal() : void */ public function dot() : void { - $this->assertEquals(331.54999999999995, $this->a->dot($this->b)); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->dot($b); + + $this->assertEquals(331.54999999999995, $c); } /** @@ -351,11 +338,24 @@ public function dot() : void */ public function matmul() : void { + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [1.1, 0.01, 6.23], + [5.0, 2.01, -1.0], + [-5.0, 1.0, 0.03], + [30.0, 0.02, -0.01], + [-0.005, 0.05, -0.5], + [-0.001, -1.0, 2.0], + ]); + + $c = $a->matmul($b); + $expected = [ - [40.807, 4.634999999999993, 622.3751], + [622.3751, 4.634999999999993, 40.807], ]; - $this->assertEqualsWithDelta($expected, $this->c->matmul($this->d->transpose())->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $c->asArray(), 1e-8); } /** @@ -363,7 +363,13 @@ public function matmul() : void */ public function inner() : void { - $this->assertEquals(331.54999999999995, $this->a->inner($this->b)); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->inner($b); + + $this->assertEquals(331.54999999999995, $c); } /** @@ -371,7 +377,11 @@ public function inner() : void */ public function outer() : void { - $z = $this->a->outer($this->b); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->outer($b); $expected = [ [-3.75, -1.5, -30.0, 7.5, 15.0, 45.0, -49.5, -30.], @@ -384,8 +394,8 @@ public function outer() : void [11.25, 4.5, 90.0, -22.5, -45.0, -135.0, 148.5, 90.], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -393,15 +403,18 @@ public function outer() : void */ public function convolve() : void { - $z = $this->a->convolve($this->c, 1); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $c = $a->convolve($b, 1); $expected = [ -60, 2.5, 259, -144, 40.5, 370.1, 462.20000000000005, 10, 1764.3000000000002, 1625.1, 2234.7000000000003, 1378.4, 535.5, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c->asArray()); } /** @@ -409,7 +422,15 @@ public function convolve() : void */ public function multiplyMatrix() : void { - $z = $this->c->multiply($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]); + + $c = $a->multiply($b); $expected = [ [24.92, -6.5, 0.087, -0.2, -1.3, 23.8], @@ -417,8 +438,8 @@ public function multiplyMatrix() : void [4.4, 32.5, -14.5, 600.0, -0.013000000000000001, -0.0119], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -426,7 +447,15 @@ public function multiplyMatrix() : void */ public function divideMatrix() : void { - $z = $this->c->divide($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]); + + $c = $a->divide($b); $expected = [ [0.6420545746388443, -6.5, 96.66666666666667, -2000.0, -5.2, 5.95], @@ -434,8 +463,8 @@ public function divideMatrix() : void [3.6363636363636362, 1.3, -0.58, 0.6666666666666666, -520.0, -11900.], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -443,7 +472,15 @@ public function divideMatrix() : void */ public function addMatrix() : void { - $z = $this->c->add($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]); + + $c = $a->add($b); $expected = [ [10.23, 5.5, 2.9299999999999997, 19.99, 2.1, 13.9], @@ -451,8 +488,8 @@ public function addMatrix() : void [5.1, 11.5, -2.1, 50.0, 2.595, 11.899000000000001], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -460,7 +497,15 @@ public function addMatrix() : void */ public function subtractMatrix() : void { - $z = $this->c->subtract($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]); + + $c = $a->subtract($b); $expected = [ [-2.2300000000000004, 7.5, 2.87, 20.01, 3.1, 9.9], @@ -468,8 +513,8 @@ public function subtractMatrix() : void [2.9, 1.5, 7.9, -10.0, 2.605, 11.901], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -477,7 +522,15 @@ public function subtractMatrix() : void */ public function powMatrix() : void { - $z = $this->c->pow($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]); + + $c = $a->pow($b); $expected = [ [5634.219287100394, 0.15384615384615385, 1.0324569211337775, 0.9704869503929601, 0.6201736729460423, 141.61], @@ -485,8 +538,8 @@ public function powMatrix() : void [4.59479341998814, 11602.90625, 0.004875397277841432, 1.073741824E+39, 0.9952338371484033, 0.9975265256911376], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -494,16 +547,24 @@ public function powMatrix() : void */ public function equalMatrix() : void { - $z = $this->c->equal($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->equal($b); $expected = [ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], + [1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 1], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -511,16 +572,24 @@ public function equalMatrix() : void */ public function notEqualMatrix() : void { - $z = $this->c->notEqual($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->notEqual($b); $expected = [ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1], + [1, 1, 1, 0, 1, 1], + [1, 1, 1, 1, 1, 0], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -528,16 +597,24 @@ public function notEqualMatrix() : void */ public function greaterMatrix() : void { - $z = $this->c->greater($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->greater($b); $expected = [ [0, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1], + [1, 1, 1, 0, 1, 0], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -545,16 +622,24 @@ public function greaterMatrix() : void */ public function greaterEqualMatrix() : void { - $z = $this->c->greaterEqual($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->greaterEqual($b); $expected = [ - [0, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -562,16 +647,24 @@ public function greaterEqualMatrix() : void */ public function lessMatrix() : void { - $z = $this->c->less($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->less($b); $expected = [ - [1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -579,16 +672,24 @@ public function lessMatrix() : void */ public function lessEqualMatrix() : void { - $z = $this->c->lessEqual($this->d); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]); + + $c = $a->lessEqual($b); $expected = [ [1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], + [0, 0, 0, 1, 0, 1], ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Matrix::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -596,12 +697,16 @@ public function lessEqualMatrix() : void */ public function multiplyVector() : void { - $z = $this->a->multiply($this->b); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-3.75, 2.5, 70.0, 18.0, 72.0, -267.0, 349.79999999999995, 90.]; + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->multiply($b); + + $expected = [-3.75, 2.5, 70.0, 18.0, 72.0, -267.0, 349.79999999999995, 90.0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -609,12 +714,16 @@ public function multiplyVector() : void */ public function divideVector() : void { - $z = $this->a->divide($this->b); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->divide($b); $expected = [-60.0, 250.0, 17.5, 72.0, 72.0, -29.666666666666668, 32.121212121212125, 22.5]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -622,12 +731,16 @@ public function divideVector() : void */ public function addVector() : void { - $z = $this->a->add($this->b); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-14.75, 25.1, 37.0, -36.5, -73.0, 86.0, 109.3, 47.]; + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->add($b); + + $expected = [-14.75, 25.1, 37.0, -36.5, -73.0, 86.0, 109.3, 47.0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -635,12 +748,16 @@ public function addVector() : void */ public function subtractVector() : void { - $z = $this->a->subtract($this->b); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->subtract($b); $expected = [-15.25, 24.9, 33.0, -35.5, -71.0, 92.0, 102.7, 43.]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -648,15 +765,19 @@ public function subtractVector() : void */ public function powVector() : void { - $z = $this->b->pow($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->pow($b); $expected = [ 1073741824.0, 1.0000000000000014E-25, 34359738368.0, 68719476736.0, 1.0, -2.909321189362571E+42, 9.172286825801562E+54, 35184372088832.0, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -664,12 +785,16 @@ public function powVector() : void */ public function modVector() : void { - $z = $this->b->mod($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); + + $c = $a->mod($b); $expected = [0, 0, 2, 0, -1, -3, 3, 2]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -677,12 +802,16 @@ public function modVector() : void */ public function equalVector() : void { - $z = $this->b->equal($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [0, 0, 0, 0, 0, 0, 0, 0]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->equal($b); + + $expected = [0, 0, 0, 1, 0, 0, 0, 0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -690,12 +819,16 @@ public function equalVector() : void */ public function notEqualVector() : void { - $z = $this->b->notEqual($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [1, 1, 1, 1, 1, 1, 1, 1]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->notEqual($b); + + $expected = [1, 1, 1, 0, 1, 1, 1, 1]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -703,12 +836,16 @@ public function notEqualVector() : void */ public function greaterVector() : void { - $z = $this->b->greater($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->greater($b); + + $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -716,12 +853,16 @@ public function greaterVector() : void */ public function greaterEqualVector() : void { - $z = $this->b->greaterEqual($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->greaterEqual($b); + + $expected = [0, 1, 1, 1, 0, 1, 1, 1]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -729,12 +870,16 @@ public function greaterEqualVector() : void */ public function lessVector() : void { - $z = $this->b->less($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->less($b); + + $expected = [1, 0, 0, 0, 1, 0, 0, 0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -742,12 +887,16 @@ public function lessVector() : void */ public function lessEqualVector() : void { - $z = $this->b->lessEqual($this->a); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $c = $a->lessEqual($b); + + $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -755,12 +904,14 @@ public function lessEqualVector() : void */ public function multiplyScalar() : void { - $z = $this->a->multiply(2); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-30, 50, 70, -72, -144, 178, 212, 90]; + $c = $a->multiply(2.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [-30.0, 50.0, 70.0, -72.0, -144.0, 178.0, 212.0, 90.0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -768,12 +919,14 @@ public function multiplyScalar() : void */ public function divideScalar() : void { - $z = $this->a->divide(2); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-7.5, 12.5, 17.5, -18, -36, 44.5, 53, 22.5]; + $c = $a->divide(2.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [-7.5, 12.5, 17.5, -18.0, -36.0, 44.5, 53, 22.5]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -781,12 +934,14 @@ public function divideScalar() : void */ public function addScalar() : void { - $z = $this->a->add(10); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-5, 35, 45, -26, -62, 99, 116, 55]; + $c = $a->add(10); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [-5.0, 35.0, 45.0, -26.0, -62.0, 99.0, 116.0, 55.0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -794,12 +949,14 @@ public function addScalar() : void */ public function subtractScalar() : void { - $z = $this->a->subtract(10); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [-25, 15, 25, -46, -82, 79, 96, 35]; + $c = $a->subtract(10); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [-25.0, 15.0, 25.0, -46.0, -82.0, 79.0, 96.0, 35.0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -807,14 +964,16 @@ public function subtractScalar() : void */ public function powScalar() : void { - $z = $this->a->pow(4); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $c = $a->pow(4); $expected = [ 50625, 390625, 1500625, 1679616, 26873856, 62742241, 126247696, 4100625 ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -822,12 +981,14 @@ public function powScalar() : void */ public function modScalar() : void { - $z = $this->a->mod(4); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $c = $a->mod(4); $expected = [-3, 1, 3, 0, 0, 1, 2, 1]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -835,12 +996,14 @@ public function modScalar() : void */ public function equalScalar() : void { - $z = $this->a->equal(25); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $c = $a->equal(25.0); $expected = [0, 1, 0, 0, 0, 0, 0, 0]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -848,12 +1011,14 @@ public function equalScalar() : void */ public function notEqualScalar() : void { - $z = $this->a->notEqual(25); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $c = $a->notEqual(25.0); $expected = [1, 0, 1, 1, 1, 1, 1, 1]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -861,12 +1026,14 @@ public function notEqualScalar() : void */ public function greaterScalar() : void { - $z = $this->b->greater(1); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [0, 0, 1, 0, 0, 0, 1, 1]; + $c = $a->greater(1.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -874,12 +1041,14 @@ public function greaterScalar() : void */ public function greaterEqualScalar() : void { - $z = $this->b->greaterEqual(1); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [0, 0, 1, 0, 0, 0, 1, 1]; + $c = $a->greaterEqual(25.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -887,12 +1056,14 @@ public function greaterEqualScalar() : void */ public function lessScalar() : void { - $z = $this->b->less(1); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [1, 1, 0, 1, 1, 1, 0, 0]; + $c = $a->less(25.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -900,12 +1071,14 @@ public function lessScalar() : void */ public function lessEqualScalar() : void { - $z = $this->b->lessEqual(1); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [1, 1, 0, 1, 1, 1, 0, 0]; + $c = $a->lessEqual(25.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [1, 1, 0, 1, 1, 0, 0, 0]; + + $this->assertInstanceOf(Vector::class, $c); + $this->assertEquals($expected, $c->asArray()); } /** @@ -913,12 +1086,14 @@ public function lessEqualScalar() : void */ public function abs() : void { - $z = $this->a->abs(); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->abs(); $expected = [15, 25, 35, 36, 72, 89, 106, 45]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -926,12 +1101,14 @@ public function abs() : void */ public function square() : void { - $z = $this->a->square(); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->square(); $expected = [225, 625, 1225, 1296, 5184, 7921, 11236, 2025]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -939,12 +1116,14 @@ public function square() : void */ public function pow() : void { - $z = $this->a->pow(3); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->pow(3); $expected = [-3375, 15625, 42875, -46656, -373248, 704969, 1191016, 91125]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -952,15 +1131,17 @@ public function pow() : void */ public function sqrt() : void { - $z = $this->c->sqrt(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->sqrt(); $expected = [ 2.0, 2.5495097567963922, 1.70293863659264, 4.47213595499958, 1.61245154965971, 3.449637662132068, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -968,16 +1149,17 @@ public function sqrt() : void */ public function exp() : void { - $z = $this->a->exp(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->exp(); $expected = [ - 3.059023205018258E-7, 72004899337.38588, 1586013452313430.8, - 2.3195228302435696E-16, 5.380186160021138E-32, 4.4896128191743455E+38, - 1.0844638552900231E+46, 3.4934271057485095E+19, + 54.598150033144236, 665.1416330443618, 18.17414536944306, + 485165195.4097903, 13.463738035001692, 147266.6252405527, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -985,15 +1167,17 @@ public function exp() : void */ public function log() : void { - $z = $this->c->log(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->log(); $expected = [ 1.3862943611198906, 1.8718021769015913, 1.0647107369924282, 2.995732273553991, 0.9555114450274363, 2.4765384001174837, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1001,15 +1185,17 @@ public function log() : void */ public function sin() : void { - $z = $this->c->sin(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->sin(); $expected = [ -0.7568024953079282, 0.21511998808781552, 0.23924932921398243, 0.9129452507276277, 0.5155013718214642, -0.6181371122370333, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1017,14 +1203,16 @@ public function sin() : void */ public function asin() : void { - $z = Vector::quick([0.1, 0.3, -0.5])->asin(); + $a = Vector::quick([0.1, 0.3, -0.5]); + + $b = $a->asin(); $expected = [ 0.1001674211615598, 0.3046926540153975, -0.5235987755982989, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1032,15 +1220,17 @@ public function asin() : void */ public function cos() : void { - $z = $this->c->cos(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->cos(); $expected = [ -0.6536436208636119, 0.9765876257280235, -0.9709581651495905, 0.40808206181339196, -0.8568887533689473, 0.7860702961410393, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1048,14 +1238,16 @@ public function cos() : void */ public function acos() : void { - $z = Vector::quick([0.1, 0.3, -0.5])->acos(); + $a = Vector::quick([0.1, 0.3, -0.5]); + + $b = $a->acos(); $expected = [ 1.4706289056333368, 1.2661036727794992, 2.0943951023931957, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1063,15 +1255,17 @@ public function acos() : void */ public function tan() : void { - $z = $this->c->tan(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->tan(); $expected = [ 1.1578212823495777, 0.22027720034589682, -0.24640539397196634, 2.237160944224742, -0.6015966130897586, -0.7863636563696398, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEqualsWithDelta($expected, $b->asArray(), 1e-8); } /** @@ -1079,15 +1273,17 @@ public function tan() : void */ public function atan() : void { - $z = $this->c->atan(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->atan(); $expected = [ 1.3258176636680326, 1.4181469983996315, 1.2387368592520112, 1.5208379310729538, 1.2036224929766774, 1.486959684726482, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1095,15 +1291,17 @@ public function atan() : void */ public function rad2deg() : void { - $z = $this->c->rad2deg(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->rad2deg(); $expected = [ 229.1831180523293, 372.42256683503507, 166.15776058793872, 1145.9155902616465, 148.96902673401405, 681.8197762056797, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1111,15 +1309,17 @@ public function rad2deg() : void */ public function deg2rad() : void { - $z = $this->c->deg2rad(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $b = $a->deg2rad(); $expected = [ 0.06981317007977318, 0.11344640137963141, 0.05061454830783556, 0.3490658503988659, 0.04537856055185257, 0.2076941809873252, ]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1127,9 +1327,9 @@ public function deg2rad() : void */ public function sum() : void { - $this->assertEquals(177.0, $this->a->sum()); - $this->assertEquals(3.15, $this->b->sum()); - $this->assertEquals(47.9, $this->c->sum()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(177.0, $a->sum()); } /** @@ -1137,9 +1337,9 @@ public function sum() : void */ public function product() : void { - $this->assertEquals(-14442510600000.0, $this->a->product()); - $this->assertEquals(-0.49500000000000005, $this->b->product()); - $this->assertEquals(46657.52, $this->c->product()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(-14442510600000.0, $a->product()); } /** @@ -1147,9 +1347,9 @@ public function product() : void */ public function min() : void { - $this->assertEquals(-72, $this->a->min()); - $this->assertEquals(-3, $this->b->min()); - $this->assertEquals(2.6, $this->c->min()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(-72, $a->min()); } /** @@ -1157,9 +1357,9 @@ public function min() : void */ public function max() : void { - $this->assertEquals(106, $this->a->max()); - $this->assertEquals(3.3, $this->b->max()); - $this->assertEquals(20.0, $this->c->max()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(106, $a->max()); } /** @@ -1167,9 +1367,9 @@ public function max() : void */ public function mean() : void { - $this->assertEquals(22.125, $this->a->mean()); - $this->assertEquals(0.39375, $this->b->mean()); - $this->assertEquals(7.983333333333333, $this->c->mean()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(22.125, $a->mean()); } /** @@ -1177,9 +1377,9 @@ public function mean() : void */ public function median() : void { - $this->assertEquals(30.0, $this->a->median()); - $this->assertEquals(0.175, $this->b->median()); - $this->assertEquals(5.25, $this->c->median()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(30.0, $a->median()); } /** @@ -1187,9 +1387,9 @@ public function median() : void */ public function quantile() : void { - $this->assertEquals(30.0, $this->a->quantile(0.5)); - $this->assertEquals(-0.625, $this->b->quantile(0.25)); - $this->assertEquals(10.55, $this->c->quantile(0.75)); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(30.0, $a->quantile(0.5)); } /** @@ -1197,9 +1397,9 @@ public function quantile() : void */ public function variance() : void { - $this->assertEquals(3227.609375, $this->a->variance()); - $this->assertEquals(3.4965234374999996, $this->b->variance()); - $this->assertEquals(38.77138888888888, $this->c->variance()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(3227.609375, $a->variance()); } /** @@ -1207,7 +1407,9 @@ public function variance() : void */ public function round() : void { - $z = $this->c->round(2); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); + + $z = $a->round(2); $expected = [4.0, 6.5, 2.9, 20.0, 2.6, 11.9]; @@ -1220,12 +1422,14 @@ public function round() : void */ public function floor() : void { - $z = $this->c->floor(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - $expected = [4.0, 6.0, 2.0, 20.0, 2.0, 11.]; + $b = $a->floor(); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [4.0, 6.0, 2.0, 20.0, 2.0, 11.0]; + + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1233,12 +1437,14 @@ public function floor() : void */ public function ceil() : void { - $z = $this->c->ceil(); + $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - $expected = [4.0, 7.0, 3.0, 20.0, 3.0, 12.]; + $b = $a->ceil(); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [4.0, 7.0, 3.0, 20.0, 3.0, 12.0]; + + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1246,9 +1452,9 @@ public function ceil() : void */ public function l1Norm() : void { - $this->assertEquals(423.0, $this->a->l1Norm()); - $this->assertEquals(12.149999999999999, $this->b->l1Norm()); - $this->assertEquals(47.9, $this->c->l1Norm()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(423.0, $a->l1Norm()); } /** @@ -1256,9 +1462,9 @@ public function l1Norm() : void */ public function l2Norm() : void { - $this->assertEquals(172.4441938715247, $this->a->l2Norm()); - $this->assertEquals(5.404858925078433, $this->b->l2Norm()); - $this->assertEquals(24.799798386277256, $this->c->l2Norm()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(172.4441938715247, $a->l2Norm()); } /** @@ -1266,9 +1472,9 @@ public function l2Norm() : void */ public function pNorm() : void { - $this->assertEquals(135.15554088861361, $this->a->pNorm(3.)); - $this->assertEquals(3.7063242195906976, $this->b->pNorm(5.)); - $this->assertEquals(20.01112107057168, $this->c->pNorm(10.)); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(135.15554088861361, $a->pNorm(3.0)); } /** @@ -1276,9 +1482,9 @@ public function pNorm() : void */ public function maxNorm() : void { - $this->assertEquals(106.0, $this->a->maxNorm()); - $this->assertEquals(3.3, $this->b->maxNorm()); - $this->assertEquals(20.0, $this->c->maxNorm()); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $this->assertEquals(106.0, $a->maxNorm()); } /** @@ -1286,12 +1492,14 @@ public function maxNorm() : void */ public function clip() : void { - $z = $this->a->clip(0.0, 100); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->clip(0.0, 100); $expected = [0.0, 25, 35, 0.0, 0.0, 89, 100.0, 45]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1299,12 +1507,14 @@ public function clip() : void */ public function clipLower() : void { - $z = $this->a->clipLower(60.); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $expected = [60.0, 60.0, 60.0, 60.0, 60.0, 89, 106.0, 60.]; + $b = $a->clipLower(60.0); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = [60.0, 60.0, 60.0, 60.0, 60.0, 89, 106.0, 60.0]; + + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1312,12 +1522,14 @@ public function clipLower() : void */ public function clipUpper() : void { - $z = $this->a->clipUpper(50.); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->clipUpper(50.0); $expected = [-15.0, 25, 35, -36.0, -72.0, 50.0, 50.0, 45]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1325,12 +1537,14 @@ public function clipUpper() : void */ public function sign() : void { - $z = $this->a->sign(); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->sign(); $expected = [-1, 1, 1, -1, -1, 1, 1, 1]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } /** @@ -1338,11 +1552,13 @@ public function sign() : void */ public function negate() : void { - $z = $this->a->negate(); + $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); + + $b = $a->negate(); $expected = [15, -25, -35, 36, 72, -89, -106, -45]; - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertInstanceOf(Vector::class, $b); + $this->assertEquals($expected, $b->asArray()); } } From b394dea92cd40d17b730c5bb3ab9b5c629324a2e Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Mon, 18 Sep 2023 16:35:20 -0500 Subject: [PATCH 02/10] Nicer --- tests/VectorTest.php | 1087 ++++++++++++++++++------------------------ 1 file changed, 476 insertions(+), 611 deletions(-) diff --git a/tests/VectorTest.php b/tests/VectorTest.php index f26fd21..ef44a5a 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -46,9 +46,9 @@ public function zeros() : void { $zeros = Vector::zeros(4); - $expected = [0, 0, 0, 0]; + $expected = Vector::quick([0, 0, 0, 0]); - $this->assertEquals($expected, $zeros->asArray()); + $this->assertEquals($expected, $zeros); } /** @@ -58,9 +58,9 @@ public function ones() : void { $ones = Vector::ones(4); - $expected = [1, 1, 1, 1]; + $expected = Vector::quick([1, 1, 1, 1]); - $this->assertEquals($expected, $ones->asArray()); + $this->assertEquals($expected, $ones); } /** @@ -70,9 +70,9 @@ public function fill() : void { $vector = Vector::fill(16, 4); - $expected = [16, 16, 16, 16]; + $expected = Vector::quick([16, 16, 16, 16]); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -122,9 +122,9 @@ public function range() : void { $vector = Vector::range(5.0, 12.0, 2.0); - $expected = [5.0, 7.0, 9.0, 11.0]; + $expected = Vector::quick([5.0, 7.0, 9.0, 11.0]); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -134,12 +134,12 @@ public function linspace() : void { $vector = Vector::linspace(-5.0, 5.0, 10); - $expected = [ + $expected = Vector::quick([ -5.0, -3.888888888888889, -2.7777777777777777, -1.6666666666666665, -0.5555555555555554, 0.5555555555555558, 1.666666666666667, 2.777777777777778, 3.8888888888888893, 5.0, - ]; + ]); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -242,10 +242,9 @@ public function asColumnMatrix() : void $matrix = $vector->asColumnMatrix(); - $expected = [[-15.0], [25.0], [35.0], [-36.0], [-72.0], [89.0], [106.0], [45.0]]; + $expected = Matrix::quick([[-15.0], [25.0], [35.0], [-36.0], [-72.0], [89.0], [106.0], [45.0]]); - $this->assertInstanceOf(Matrix::class, $matrix); - $this->assertEquals($expected, $matrix->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -257,16 +256,14 @@ public function reshape() : void $matrix = $vector->reshape(4, 2); - $expected = [ + $expected = Matrix::quick([ [-15.0, 25.0], [35.0, -36.0], [-72.0, 89.0], [106.0, 45.0], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $matrix); - $this->assertEquals([4, 2], $matrix->shape()); - $this->assertEquals($expected, $matrix->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -278,10 +275,9 @@ public function transpose() : void $vector = $vector->transpose(); - $expected = [-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]; + $expected = ColumnVector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - $this->assertInstanceOf(ColumnVector::class, $vector); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -297,9 +293,9 @@ public function map() : void $vector = $vector->map($sign); - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + $expected = Vector::quick([0, 1, 1, 0, 0, 1, 1, 1]); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -311,12 +307,12 @@ public function reciprocal() : void $vector = $vector->reciprocal(); - $expected = [ + $expected = Vector::quick([ -0.06666666666666667, 0.04, 0.02857142857142857, -0.027777777777777776, -0.013888888888888888, 0.011235955056179775, 0.009433962264150943, 0.022222222222222223, - ]; + ]); - $this->assertEquals($expected, $vector->asArray()); + $this->assertEquals($expected, $vector); } /** @@ -351,11 +347,11 @@ public function matmul() : void $c = $a->matmul($b); - $expected = [ + $expected = Matrix::quick([ [622.3751, 4.634999999999993, 40.807], - ]; + ]); - $this->assertEqualsWithDelta($expected, $c->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $c, 1e-8); } /** @@ -383,7 +379,7 @@ public function outer() : void $c = $a->outer($b); - $expected = [ + $expected = Matrix::quick([ [-3.75, -1.5, -30.0, 7.5, 15.0, 45.0, -49.5, -30.], [6.25, 2.5, 50.0, -12.5, -25.0, -75.0, 82.5, 50.], [8.75, 3.5, 70.0, -17.5, -35.0, -105.0, 115.5, 70.], @@ -392,10 +388,9 @@ public function outer() : void [22.25, 8.9, 178.0, -44.5, -89.0, -267.0, 293.7, 178.], [26.5, 10.600000000000001, 212.0, -53.0, -106.0, -318.0, 349.79999999999995, 212.], [11.25, 4.5, 90.0, -22.5, -45.0, -135.0, 148.5, 90.], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** @@ -409,676 +404,568 @@ public function convolve() : void $c = $a->convolve($b, 1); - $expected = [ + $expected = Vector::quick([ -60, 2.5, 259, -144, 40.5, 370.1, 462.20000000000005, 10, 1764.3000000000002, 1625.1, 2234.7000000000003, 1378.4, 535.5, - ]; + ]); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** * @test + * @dataProvider multiplyProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function multiplyMatrix() : void + public function multiply(Vector $a, $b, $expected) : void { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - $c = $a->multiply($b); - $expected = [ - [24.92, -6.5, 0.087, -0.2, -1.3, 23.8], - [0.04, 13.064999999999998, 2.9, 0.4, 0.13, -11.9], - [4.4, 32.5, -14.5, 600.0, -0.013000000000000001, -0.0119], - ]; - - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function divideMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - - $c = $a->divide($b); - - $expected = [ - [0.6420545746388443, -6.5, 96.66666666666667, -2000.0, -5.2, 5.95], - [400.0, 3.2338308457711444, 2.9, 1000.0, 52.0, -11.9], - [3.6363636363636362, 1.3, -0.58, 0.6666666666666666, -520.0, -11900.], + public function multiplyProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]), + Matrix::quick([ + [24.92, -6.5, 0.087, -0.2, -1.3, 23.8], + [0.04, 13.064999999999998, 2.9, 0.4, 0.13, -11.9], + [4.4, 32.5, -14.5, 600.0, -0.013000000000000001, -0.0119], + ]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function addMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - - $c = $a->add($b); - - $expected = [ - [10.23, 5.5, 2.9299999999999997, 19.99, 2.1, 13.9], - [4.01, 8.51, 3.9, 20.02, 2.65, 10.9], - [5.1, 11.5, -2.1, 50.0, 2.595, 11.899000000000001], + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([-3.75, 2.5, 70.0, 18.0, 72.0, -267.0, 349.79999999999995, 90.0]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function subtractMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - - $c = $a->subtract($b); - - $expected = [ - [-2.2300000000000004, 7.5, 2.87, 20.01, 3.1, 9.9], - [3.99, 4.49, 1.9, 19.98, 2.5500000000000003, 12.9], - [2.9, 1.5, 7.9, -10.0, 2.605, 11.901], + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 2.0, + Vector::quick([-30.0, 50.0, 70.0, -72.0, -144.0, 178.0, 212.0, 90.0]), ]; - - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); } /** * @test + * @dataProvider divideProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function powMatrix() : void + public function divide(Vector $a, $b, $expected) : void { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, -0.001], - ]); - - $c = $a->pow($b); - - $expected = [ - [5634.219287100394, 0.15384615384615385, 1.0324569211337775, 0.9704869503929601, 0.6201736729460423, 141.61], - [1.013959479790029, 43.048284263459465, 2.9, 1.0617459178549786, 1.0489352187366092, 0.08403361344537814], - [4.59479341998814, 11602.90625, 0.004875397277841432, 1.073741824E+39, 0.9952338371484033, 0.9975265256911376], - ]; + $c = $a->divide($b); - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function equalMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->equal($b); - - $expected = [ - [1, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 0, 0], - [0, 0, 0, 0, 0, 1], + public function divideProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]), + Matrix::quick([ + [0.6420545746388443, -6.5, 96.66666666666667, -2000.0, -5.2, 5.95], + [400.0, 3.2338308457711444, 2.9, 1000.0, 52.0, -11.9], + [3.6363636363636362, 1.3, -0.58, 0.6666666666666666, -520.0, -11900.], + ]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function notEqualMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->notEqual($b); - - $expected = [ - [0, 1, 1, 1, 1, 1], - [1, 1, 1, 0, 1, 1], - [1, 1, 1, 1, 1, 0], + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([-60.0, 250.0, 17.5, 72.0, 72.0, -29.666666666666668, 32.121212121212125, 22.5]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function greaterMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->greater($b); - - $expected = [ - [0, 1, 1, 1, 1, 1], - [1, 1, 1, 0, 1, 1], - [1, 1, 1, 0, 1, 0], + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 2.0, + Vector::quick([-7.5, 12.5, 17.5, -18.0, -36.0, 44.5, 53, 22.5]), ]; - - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); } /** * @test + * @dataProvider addProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function greaterEqualMatrix() : void + public function add(Vector $a, $b, $expected) : void { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->greaterEqual($b); - - $expected = [ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 0, 1, 1], - ]; + $c = $a->add($b); - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function lessMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->less($b); - - $expected = [ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 0, 0], + public function addProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]), + Matrix::quick([ + [10.23, 5.5, 2.9299999999999997, 19.99, 2.1, 13.9], + [4.01, 8.51, 3.9, 20.02, 2.65, 10.9], + [5.1, 11.5, -2.1, 50.0, 2.595, 11.899000000000001], + ]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function lessEqualMatrix() : void - { - $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - - $b = Matrix::quick([ - [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], - [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], - [1.1, 5.0, -5.0, 30, -0.005, 11.9], - ]); - - $c = $a->lessEqual($b); - - $expected = [ - [1, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 0, 0], - [0, 0, 0, 1, 0, 1], + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([-14.75, 25.1, 37.0, -36.5, -73.0, 86.0, 109.3, 47.0]), ]; - $this->assertInstanceOf(Matrix::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function multiplyVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->multiply($b); - - $expected = [-3.75, 2.5, 70.0, 18.0, 72.0, -267.0, 349.79999999999995, 90.0]; - - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function divideVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->divide($b); - - $expected = [-60.0, 250.0, 17.5, 72.0, 72.0, -29.666666666666668, 32.121212121212125, 22.5]; - - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function addVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->add($b); - - $expected = [-14.75, 25.1, 37.0, -36.5, -73.0, 86.0, 109.3, 47.0]; - - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 10.0, + Vector::quick([-5.0, 35.0, 45.0, -26.0, -62.0, 99.0, 116.0, 55.0]), + ]; } /** * @test + * @dataProvider subtractProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function subtractVector() : void + public function subtract(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - $c = $a->subtract($b); - $expected = [-15.25, 24.9, 33.0, -35.5, -71.0, 92.0, 102.7, 43.]; - - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function powVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->pow($b); - - $expected = [ - 1073741824.0, 1.0000000000000014E-25, 34359738368.0, 68719476736.0, - 1.0, -2.909321189362571E+42, 9.172286825801562E+54, 35184372088832.0, + public function subtractProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]), + Matrix::quick([ + [-2.2300000000000004, 7.5, 2.87, 20.01, 3.1, 9.9], + [3.99, 4.49, 1.9, 19.98, 2.5500000000000003, 12.9], + [2.9, 1.5, 7.9, -10.0, 2.605, 11.901], + ]), ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function modVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->mod($b); - - $expected = [0, 0, 2, 0, -1, -3, 3, 2]; - - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function equalVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->equal($b); - - $expected = [0, 0, 0, 1, 0, 0, 0, 0]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([-15.25, 24.9, 33.0, -35.5, -71.0, 92.0, 102.7, 43.0]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 10.0, + Vector::quick([-25.0, 15.0, 25.0, -46.0, -82.0, 79.0, 96.0, 35.0]), + ]; } /** * @test + * @dataProvider powProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function notEqualVector() : void + public function power(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->notEqual($b); - - $expected = [1, 1, 1, 0, 1, 1, 1, 1]; + $c = $a->pow($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function greaterVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->greater($b); + public function powProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [6.23, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 0.02, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, -0.001], + ]), + Matrix::quick([ + [5634.219287100394, 0.15384615384615385, 1.0324569211337775, 0.9704869503929601, 0.6201736729460423, 141.61], + [1.013959479790029, 43.048284263459465, 2.9, 1.0617459178549786, 1.0489352187366092, 0.08403361344537814], + [4.59479341998814, 11602.90625, 0.004875397277841432, 1.073741824E+39, 0.9952338371484033, 0.9975265256911376], + ]), + ]; - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([ + 1073741824.0, 1.0000000000000014E-25, 34359738368.0, 68719476736.0, + 1.0, -2.909321189362571E+42, 9.172286825801562E+54, 35184372088832.0, + ]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 4.0, + Vector::quick([ + 50625, 390625, 1500625, 1679616, 26873856, 62742241, 126247696, 4100625 + ]), + ]; } /** * @test + * @dataProvider equalProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function greaterEqualVector() : void + public function equal(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->greaterEqual($b); - - $expected = [0, 1, 1, 1, 0, 1, 1, 1]; + $c = $a->equal($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function lessVector() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->less($b); + public function equalProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 1], + ]), + ]; - $expected = [1, 0, 0, 0, 1, 0, 0, 0]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([0, 0, 0, 1, 0, 0, 0, 0]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 25.0, + Vector::quick([0, 1, 0, 0, 0, 0, 0, 0]), + ]; } /** * @test + * @dataProvider notEqualProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function lessEqualVector() : void + public function notEqual(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $b = Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]); - - $c = $a->lessEqual($b); - - $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + $c = $a->notEqual($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function multiplyScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->multiply(2.0); + public function notEqualProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [0, 1, 1, 1, 1, 1], + [1, 1, 1, 0, 1, 1], + [1, 1, 1, 1, 1, 0], + ]), + ]; - $expected = [-30.0, 50.0, 70.0, -72.0, -144.0, 178.0, 212.0, 90.0]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([1, 1, 1, 0, 1, 1, 1, 1]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 25.0, + Vector::quick([1, 0, 1, 1, 1, 1, 1, 1]), + ]; } /** * @test + * @dataProvider greaterProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function divideScalar() : void + public function greater(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->divide(2.0); - - $expected = [-7.5, 12.5, 17.5, -18.0, -36.0, 44.5, 53, 22.5]; + $c = $a->greater($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function addScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->add(10); + public function greaterProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [0, 1, 1, 1, 1, 1], + [1, 1, 1, 0, 1, 1], + [1, 1, 1, 0, 1, 0], + ]), + ]; - $expected = [-5.0, 35.0, 45.0, -26.0, -62.0, 99.0, 116.0, 55.0]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([0, 1, 1, 0, 0, 1, 1, 1]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 1.0, + Vector::quick([0, 1, 1, 0, 0, 1, 1, 1]), + ]; } /** * @test + * @dataProvider greaterEqualProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function subtractScalar() : void + public function greaterEqual(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->subtract(10); - - $expected = [-25.0, 15.0, 25.0, -46.0, -82.0, 79.0, 96.0, 35.0]; + $c = $a->greaterEqual($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function powScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->pow(4); - - $expected = [ - 50625, 390625, 1500625, 1679616, 26873856, 62742241, 126247696, 4100625 + public function greaterEqualProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1], + [1, 1, 1, 0, 1, 1], + ]), ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); - } - - /** - * @test - */ - public function modScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->mod(4); - - $expected = [-3, 1, 3, 0, 0, 1, 2, 1]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([0, 1, 1, 1, 0, 1, 1, 1]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 25.0, + Vector::quick([0, 1, 1, 0, 0, 1, 1, 1]), + ]; } /** * @test + * @dataProvider lessProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function equalScalar() : void + public function less(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->equal(25.0); - - $expected = [0, 1, 0, 0, 0, 0, 0, 0]; + $c = $a->less($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function notEqualScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->notEqual(25.0); + public function lessProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0], + ]), + ]; - $expected = [1, 0, 1, 1, 1, 1, 1, 1]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([1, 0, 0, 0, 1, 0, 0, 0]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 25.0, + Vector::quick([1, 0, 0, 1, 1, 0, 0, 0]), + ]; } /** * @test + * @dataProvider lessEqualProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function greaterScalar() : void + public function lessEqual(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->greater(1.0); - - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + $c = $a->lessEqual($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function greaterEqualScalar() : void - { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->greaterEqual(25.0); + public function lessEqualProvider() : Generator + { + yield [ + Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]), + Matrix::quick([ + [4.0, -1.0, 0.03, -0.01, -0.5, 2.0], + [0.01, 2.01, 1.0, 20.0, 0.05, -1.0], + [1.1, 5.0, -5.0, 30, -0.005, 11.9], + ]), + Matrix::quick([ + [1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0], + [0, 0, 0, 1, 0, 1], + ]), + + ]; - $expected = [0, 1, 1, 0, 0, 1, 1, 1]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -36.0, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([1, 0, 0, 1, 1, 0, 0, 0]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 25.0, + Vector::quick([1, 1, 0, 1, 1, 0, 0, 0]), + ]; } /** * @test + * @dataProvider modProvider + * + * @param \Tensor\Vector $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function lessScalar() : void + public function mod(Vector $a, $b, $expected) : void { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->less(25.0); - - $expected = [1, 0, 0, 1, 1, 0, 0, 0]; + $c = $a->mod($b); - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function lessEqualScalar() : void + public function modProvider() : Generator { - $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - - $c = $a->lessEqual(25.0); - - $expected = [1, 1, 0, 1, 1, 0, 0, 0]; + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([0, 0, 2, 0, -1, -3, 3, 2]), + ]; - $this->assertInstanceOf(Vector::class, $c); - $this->assertEquals($expected, $c->asArray()); + yield [ + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), + 4, + Vector::quick([-3, 1, 3, 0, 0, 1, 2, 1]), + ]; } /** @@ -1090,10 +977,9 @@ public function abs() : void $b = $a->abs(); - $expected = [15, 25, 35, 36, 72, 89, 106, 45]; + $expected = Vector::quick([15, 25, 35, 36, 72, 89, 106, 45]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1105,10 +991,9 @@ public function square() : void $b = $a->square(); - $expected = [225, 625, 1225, 1296, 5184, 7921, 11236, 2025]; + $expected = Vector::quick([225, 625, 1225, 1296, 5184, 7921, 11236, 2025]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1120,10 +1005,9 @@ public function pow() : void $b = $a->pow(3); - $expected = [-3375, 15625, 42875, -46656, -373248, 704969, 1191016, 91125]; + $expected = Vector::quick([-3375, 15625, 42875, -46656, -373248, 704969, 1191016, 91125]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1135,13 +1019,12 @@ public function sqrt() : void $b = $a->sqrt(); - $expected = [ + $expected = Vector::quick([ 2.0, 2.5495097567963922, 1.70293863659264, 4.47213595499958, 1.61245154965971, 3.449637662132068, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1153,13 +1036,12 @@ public function exp() : void $b = $a->exp(); - $expected = [ + $expected = Vector::quick([ 54.598150033144236, 665.1416330443618, 18.17414536944306, 485165195.4097903, 13.463738035001692, 147266.6252405527, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1171,13 +1053,12 @@ public function log() : void $b = $a->log(); - $expected = [ + $expected = Vector::quick([ 1.3862943611198906, 1.8718021769015913, 1.0647107369924282, 2.995732273553991, 0.9555114450274363, 2.4765384001174837, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1189,13 +1070,12 @@ public function sin() : void $b = $a->sin(); - $expected = [ + $expected = Vector::quick([ -0.7568024953079282, 0.21511998808781552, 0.23924932921398243, 0.9129452507276277, 0.5155013718214642, -0.6181371122370333, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1207,12 +1087,11 @@ public function asin() : void $b = $a->asin(); - $expected = [ + $expected = Vector::quick([ 0.1001674211615598, 0.3046926540153975, -0.5235987755982989, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1224,13 +1103,12 @@ public function cos() : void $b = $a->cos(); - $expected = [ + $expected = Vector::quick([ -0.6536436208636119, 0.9765876257280235, -0.9709581651495905, 0.40808206181339196, -0.8568887533689473, 0.7860702961410393, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1242,12 +1120,11 @@ public function acos() : void $b = $a->acos(); - $expected = [ + $expected = Vector::quick([ 1.4706289056333368, 1.2661036727794992, 2.0943951023931957, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1259,13 +1136,12 @@ public function tan() : void $b = $a->tan(); - $expected = [ + $expected = Vector::quick([ 1.1578212823495777, 0.22027720034589682, -0.24640539397196634, 2.237160944224742, -0.6015966130897586, -0.7863636563696398, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEqualsWithDelta($expected, $b->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1277,13 +1153,12 @@ public function atan() : void $b = $a->atan(); - $expected = [ + $expected = Vector::quick([ 1.3258176636680326, 1.4181469983996315, 1.2387368592520112, 1.5208379310729538, 1.2036224929766774, 1.486959684726482, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1295,13 +1170,12 @@ public function rad2deg() : void $b = $a->rad2deg(); - $expected = [ + $expected = Vector::quick([ 229.1831180523293, 372.42256683503507, 166.15776058793872, 1145.9155902616465, 148.96902673401405, 681.8197762056797, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1313,13 +1187,12 @@ public function deg2rad() : void $b = $a->deg2rad(); - $expected = [ + $expected = Vector::quick([ 0.06981317007977318, 0.11344640137963141, 0.05061454830783556, 0.3490658503988659, 0.04537856055185257, 0.2076941809873252, - ]; + ]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1409,12 +1282,11 @@ public function round() : void { $a = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - $z = $a->round(2); + $b = $a->round(2); - $expected = [4.0, 6.5, 2.9, 20.0, 2.6, 11.9]; + $expected = Vector::quick([4.0, 6.5, 2.9, 20.0, 2.6, 11.9]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1426,10 +1298,9 @@ public function floor() : void $b = $a->floor(); - $expected = [4.0, 6.0, 2.0, 20.0, 2.0, 11.0]; + $expected = Vector::quick([4.0, 6.0, 2.0, 20.0, 2.0, 11.0]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1441,10 +1312,9 @@ public function ceil() : void $b = $a->ceil(); - $expected = [4.0, 7.0, 3.0, 20.0, 3.0, 12.0]; + $expected = Vector::quick([4.0, 7.0, 3.0, 20.0, 3.0, 12.0]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1496,10 +1366,9 @@ public function clip() : void $b = $a->clip(0.0, 100); - $expected = [0.0, 25, 35, 0.0, 0.0, 89, 100.0, 45]; + $expected = Vector::quick([0.0, 25, 35, 0.0, 0.0, 89, 100.0, 45]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1511,10 +1380,9 @@ public function clipLower() : void $b = $a->clipLower(60.0); - $expected = [60.0, 60.0, 60.0, 60.0, 60.0, 89, 106.0, 60.0]; + $expected = Vector::quick([60.0, 60.0, 60.0, 60.0, 60.0, 89, 106.0, 60.0]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1526,10 +1394,9 @@ public function clipUpper() : void $b = $a->clipUpper(50.0); - $expected = [-15.0, 25, 35, -36.0, -72.0, 50.0, 50.0, 45]; + $expected = Vector::quick([-15.0, 25, 35, -36.0, -72.0, 50.0, 50.0, 45]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1541,10 +1408,9 @@ public function sign() : void $b = $a->sign(); - $expected = [-1, 1, 1, -1, -1, 1, 1, 1]; + $expected = Vector::quick([-1, 1, 1, -1, -1, 1, 1, 1]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1556,9 +1422,8 @@ public function negate() : void $b = $a->negate(); - $expected = [15, -25, -35, 36, 72, -89, -106, -45]; + $expected = Vector::quick([15, -25, -35, 36, 72, -89, -106, -45]); - $this->assertInstanceOf(Vector::class, $b); - $this->assertEquals($expected, $b->asArray()); + $this->assertEquals($expected, $b); } } From 637f158646998497c8826397515ed62c34cdba41 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Mon, 18 Sep 2023 18:51:51 -0500 Subject: [PATCH 03/10] Clean up Column Vector tests --- tests/ColumnVectorTest.php | 261 ++++++++++++++++++++++--------------- tests/VectorTest.php | 11 +- 2 files changed, 160 insertions(+), 112 deletions(-) diff --git a/tests/ColumnVectorTest.php b/tests/ColumnVectorTest.php index 4cc3a5d..b98c7d5 100644 --- a/tests/ColumnVectorTest.php +++ b/tests/ColumnVectorTest.php @@ -20,51 +20,22 @@ */ class ColumnVectorTest extends TestCase { - /** - * @var \Tensor\ColumnVector - */ - protected \Tensor\ColumnVector $a; - - /** - * @var \Tensor\Vector - */ - protected \Tensor\Vector $b; - - /** - * @var \Tensor\Matrix - */ - protected \Tensor\Matrix $c; - - /** - * @before - */ - public function setUp() : void - { - $this->a = ColumnVector::build([-15, 25, 35]); - - $this->b = Vector::quick([0.25, 0.1, 2.]); - - $this->c = Matrix::quick([ - [6.23, -1, 0.03], - [0.01, 2.01, 1], - [1.1, 5, -5], - ]); - } - /** * @test */ public function build() : void { - $this->assertInstanceOf(ColumnVector::class, $this->a); - $this->assertInstanceOf(Tensor::class, $this->a); - $this->assertInstanceOf(ArrayLike::class, $this->a); - $this->assertInstanceOf(Arithmetic::class, $this->a); - $this->assertInstanceOf(Comparable::class, $this->a); - $this->assertInstanceOf(Algebraic::class, $this->a); - $this->assertInstanceOf(Trigonometric::class, $this->a); - $this->assertInstanceOf(Statistical::class, $this->a); - $this->assertInstanceOf(Special::class, $this->a); + $vector = ColumnVector::build([-15, 25, 35]); + + $this->assertInstanceOf(ColumnVector::class, $vector); + $this->assertInstanceOf(Tensor::class, $vector); + $this->assertInstanceOf(ArrayLike::class, $vector); + $this->assertInstanceOf(Arithmetic::class, $vector); + $this->assertInstanceOf(Comparable::class, $vector); + $this->assertInstanceOf(Algebraic::class, $vector); + $this->assertInstanceOf(Trigonometric::class, $vector); + $this->assertInstanceOf(Statistical::class, $vector); + $this->assertInstanceOf(Special::class, $vector); } /** @@ -72,7 +43,9 @@ public function build() : void */ public function shape() : void { - $this->assertEquals([3], $this->a->shape()); + $vector = ColumnVector::quick([-15, 25, 35]); + + $this->assertEquals([3], $vector->shape()); } /** @@ -80,7 +53,9 @@ public function shape() : void */ public function shapeString() : void { - $this->assertEquals('3', $this->a->shapeString()); + $vector = ColumnVector::quick([-15, 25, 35]); + + $this->assertEquals('3', $vector->shapeString()); } /** @@ -88,7 +63,9 @@ public function shapeString() : void */ public function size() : void { - $this->assertEquals(3, $this->a->size()); + $vector = ColumnVector::quick([-15, 25, 35]); + + $this->assertEquals(3, $vector->size()); } /** @@ -96,7 +73,9 @@ public function size() : void */ public function m() : void { - $this->assertEquals(3, $this->a->m()); + $vector = ColumnVector::quick([-15, 25, 35]); + + $this->assertEquals(3, $vector->m()); } /** @@ -104,176 +83,248 @@ public function m() : void */ public function n() : void { - $this->assertEquals(1, $this->a->n()); + $vector = ColumnVector::quick([-15, 25, 35]); + + $this->assertEquals(1, $vector->n()); } /** * @test */ - public function multiplyMatrix() : void + public function multiply() : void { - $z = $this->a->multiply($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); - $expected = [ + $c = $a->multiply($b); + + $expected = Matrix::quick([ [-93.45, 15, -0.44999999999999996], [0.25, 50.24999999999999, 25], [38.5, 175, -175], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function divideMatrix() : void + public function divide() : void { - $z = $this->a->divide($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->divide($b); - $expected = [ + $expected = Matrix::quick([ [-2.407704654895666, 15, -500.], [2500.0, 12.437810945273633, 25], [31.818181818181817, 7, -7], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function addMatrix() : void + public function add() : void { - $z = $this->a->add($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->add($b); - $expected = [ + $expected = Matrix::quick([ [-8.77, -16, -14.97], [25.01, 27.009999999999998, 26], [36.1, 40, 30], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function subtractMatrix() : void + public function subtract() : void { - $z = $this->a->subtract($this->c); + $a = ColumnVector::quick([-15, 25, 35]); - $expected = [ + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->subtract($b); + + $expected = Matrix::quick([ [-21.23, -14, -15.03], [24.99, 22.990000000000002, 24], [33.9, 30, 40], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function equalMatrix() : void + public function equal() : void { - $z = $this->a->equal($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->equal($b); - $expected = [ + $expected = Matrix::quick([ [0, 0, 0], [0, 0, 0], [0, 0, 0], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function notEqualMatrix() : void + public function notEqual() : void { - $z = $this->a->notEqual($this->c); + $a = ColumnVector::quick([-15, 25, 35]); - $expected = [ + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->notEqual($b); + + $expected = Matrix::quick([ [1, 1, 1], [1, 1, 1], [1, 1, 1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function greaterMatrix() : void + public function greater() : void { - $z = $this->a->greater($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); - $expected = [ + $c = $a->greater($b); + + $expected = Matrix::quick([ [0, 0, 0], [1, 1, 1], [1, 1, 1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function greaterEqualMatrix() : void + public function greaterEqual() : void { - $z = $this->a->greaterEqual($this->c); + $a = ColumnVector::quick([-15, 25, 35]); + + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->greaterEqual($b); - $expected = [ + $expected = Matrix::quick([ [0, 0, 0], [1, 1, 1], [1, 1, 1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function lessMatrix() : void + public function less() : void { - $z = $this->a->less($this->c); + $a = ColumnVector::quick([-15, 25, 35]); - $expected = [ + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->less($b); + + $expected = Matrix::quick([ [1, 1, 1], [0, 0, 0], [0, 0, 0], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test */ - public function lessEqualMatrix() : void + public function lessEqual() : void { - $z = $this->a->lessEqual($this->c); + $a = ColumnVector::quick([-15, 25, 35]); - $expected = [ + $b = Matrix::quick([ + [6.23, -1, 0.03], + [0.01, 2.01, 1], + [1.1, 5, -5], + ]); + + $c = $a->lessEqual($b); + + $expected = Matrix::quick([ [1, 1, 1], [0, 0, 0], [0, 0, 0], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } } diff --git a/tests/VectorTest.php b/tests/VectorTest.php index ef44a5a..0d3ab05 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -635,12 +635,9 @@ public function powProvider() : Generator ]; yield [ - Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), - Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), - Vector::quick([ - 1073741824.0, 1.0000000000000014E-25, 34359738368.0, 68719476736.0, - 1.0, -2.909321189362571E+42, 9.172286825801562E+54, 35184372088832.0, - ]), + Vector::quick([3.0, 6.0, 9.0]), + Vector::quick([3.0, 2.0, 1.0]), + Vector::quick([27.0, 36.0, 9.0]), ]; yield [ @@ -956,8 +953,8 @@ public function mod(Vector $a, $b, $expected) : void public function modProvider() : Generator { yield [ - Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), Vector::quick([0.25, 0.1, 2.0, -0.5, -1.0, -3.0, 3.3, 2.0]), + Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), Vector::quick([0, 0, 2, 0, -1, -3, 3, 2]), ]; From 8c8c78db1d0946ac33f300bf038d9ce115e4ba6d Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Mon, 18 Sep 2023 23:52:16 -0500 Subject: [PATCH 04/10] Nicer Matrix tests --- tests/MatrixTest.php | 2737 +++++++++++++++++++++++++----------------- 1 file changed, 1615 insertions(+), 1122 deletions(-) diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php index c9b8b90..f0a80de 100644 --- a/tests/MatrixTest.php +++ b/tests/MatrixTest.php @@ -16,9 +16,11 @@ use Tensor\Reductions\REF; use Tensor\Reductions\RREF; use Tensor\Decompositions\LU; +use Tensor\Decompositions\SVD; use Tensor\Decompositions\Eigen; use Tensor\Decompositions\Cholesky; use PHPUnit\Framework\TestCase; +use Generator; /** * @covers \Tensor\Matrix @@ -26,72 +28,25 @@ class MatrixTest extends TestCase { /** - * @var \Tensor\Matrix - */ - protected \Tensor\Matrix $a; - - /** - * @var \Tensor\Matrix - */ - protected \Tensor\Matrix $b; - - /** - * @var \Tensor\Matrix - */ - protected \Tensor\Matrix $c; - - /** - * @var \Tensor\Vector - */ - protected \Tensor\Vector $d; - - /** - * @var \Tensor\ColumnVector - */ - protected \Tensor\ColumnVector $e; - - /** - * @before + * @test */ - public function setUp() : void + public function build() : void { - $this->a = Matrix::build([ + $matrix = Matrix::build([ [22, -17, 12], [4, 11, -2], [20, -6, -9], ]); - $this->b = Matrix::quick([ - [13], - [11], - [9], - ]); - - $this->c = Matrix::quick([ - [4, 6, -12], - [1, 3, 5], - [-10, -1, 14], - ]); - - $this->d = Vector::quick([2, 10, -1]); - - $this->e = ColumnVector::quick([2.5, -1, 4.8]); - } - - /** - * @test - */ - public function build() : void - { - $this->assertInstanceOf(Matrix::class, $this->a); - $this->assertInstanceOf(Tensor::class, $this->a); - $this->assertInstanceOf(ArrayLike::class, $this->a); - $this->assertInstanceOf(Arithmetic::class, $this->a); - $this->assertInstanceOf(Comparable::class, $this->a); - $this->assertInstanceOf(Algebraic::class, $this->a); - $this->assertInstanceOf(Trigonometric::class, $this->a); - $this->assertInstanceOf(Statistical::class, $this->a); - $this->assertInstanceOf(Special::class, $this->a); + $this->assertInstanceOf(Matrix::class, $matrix); + $this->assertInstanceOf(Tensor::class, $matrix); + $this->assertInstanceOf(ArrayLike::class, $matrix); + $this->assertInstanceOf(Arithmetic::class, $matrix); + $this->assertInstanceOf(Comparable::class, $matrix); + $this->assertInstanceOf(Algebraic::class, $matrix); + $this->assertInstanceOf(Trigonometric::class, $matrix); + $this->assertInstanceOf(Statistical::class, $matrix); + $this->assertInstanceOf(Special::class, $matrix); } /** @@ -99,17 +54,16 @@ public function build() : void */ public function identity() : void { - $z = Matrix::identity(4); + $matrix = Matrix::identity(4); - $expected = [ + $expected = Matrix::quick([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -117,15 +71,14 @@ public function identity() : void */ public function zeros() : void { - $z = Matrix::zeros(2, 4); + $matrix = Matrix::zeros(2, 4); - $expected = [ + $expected = Matrix::quick([ [0, 0, 0, 0], [0, 0, 0, 0], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -133,17 +86,16 @@ public function zeros() : void */ public function ones() : void { - $z = Matrix::ones(4, 2); + $matrix = Matrix::ones(4, 2); - $expected = [ + $expected = Matrix::quick([ [1, 1], [1, 1], [1, 1], [1, 1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -151,17 +103,16 @@ public function ones() : void */ public function diagonal() : void { - $z = Matrix::diagonal([0, 1, 4, 5]); + $matrix = Matrix::diagonal([0, 1, 4, 5]); - $expected = [ + $expected = Matrix::quick([ [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 4, 0], [0, 0, 0, 5], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -169,17 +120,16 @@ public function diagonal() : void */ public function fill() : void { - $z = Matrix::fill(5, 4, 4); + $matrix = Matrix::fill(5, 4, 4); - $expected = [ + $expected = Matrix::quick([ [5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $matrix); } /** @@ -187,10 +137,9 @@ public function fill() : void */ public function rand() : void { - $z = Matrix::rand(4, 4); + $matrix = Matrix::rand(4, 4); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertCount(16, $z); + $this->assertCount(16, $matrix); } /** @@ -198,10 +147,9 @@ public function rand() : void */ public function gaussian() : void { - $z = Matrix::gaussian(3, 3); + $matrix = Matrix::gaussian(3, 3); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertCount(9, $z); + $this->assertCount(9, $matrix); } /** @@ -209,10 +157,9 @@ public function gaussian() : void */ public function poisson() : void { - $z = Matrix::poisson(6, 4, 2.); + $matrix = Matrix::poisson(6, 4, 2.); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertCount(24, $z); + $this->assertCount(24, $matrix); } /** @@ -220,10 +167,9 @@ public function poisson() : void */ public function uniform() : void { - $z = Matrix::uniform(3, 3); + $matrix = Matrix::uniform(3, 3); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertCount(9, $z); + $this->assertCount(9, $matrix); } /** @@ -231,9 +177,13 @@ public function uniform() : void */ public function shape() : void { - $this->assertEquals([3, 3], $this->a->shape()); - $this->assertEquals([3, 1], $this->b->shape()); - $this->assertEquals([3, 3], $this->c->shape()); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $this->assertEquals([3, 3], $matrix->shape()); } /** @@ -241,19 +191,47 @@ public function shape() : void */ public function shapeString() : void { - $this->assertEquals('3 x 3', $this->a->shapeString()); - $this->assertEquals('3 x 1', $this->b->shapeString()); - $this->assertEquals('3 x 3', $this->c->shapeString()); + $matrix = Matrix::quick([ + [22, -17, 12, 16], + [4, 11, -2, 18], + ]); + + $this->assertEquals('2 x 4', $matrix->shapeString()); } /** * @test + * @dataProvider isSquareProvider + * + * @param \Tensor\Matrix $matrix + * @param bool $expected + */ + public function isSquare(Matrix $matrix, $expected) : void + { + $this->assertEquals($expected, $matrix->isSquare()); + } + + /** + * @return \Generator */ - public function isSquare() : void + public function isSquareProvider() : Generator { - $this->assertTrue($this->a->isSquare()); - $this->assertFalse($this->b->isSquare()); - $this->assertTrue($this->c->isSquare()); + yield [ + Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]), + true, + ]; + + yield [ + Matrix::quick([ + [22, -17, 12, 16], + [4, 11, -2, 18], + ]), + false, + ]; } /** @@ -261,9 +239,13 @@ public function isSquare() : void */ public function size() : void { - $this->assertEquals(9, $this->a->size()); - $this->assertEquals(3, $this->b->size()); - $this->assertEquals(9, $this->c->size()); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $this->assertEquals(9, $matrix->size()); } /** @@ -271,9 +253,13 @@ public function size() : void */ public function m() : void { - $this->assertEquals(3, $this->a->m()); - $this->assertEquals(3, $this->b->m()); - $this->assertEquals(3, $this->c->m()); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $this->assertEquals(3, $matrix->m()); } /** @@ -281,9 +267,13 @@ public function m() : void */ public function n() : void { - $this->assertEquals(3, $this->a->n()); - $this->assertEquals(1, $this->b->n()); - $this->assertEquals(3, $this->c->n()); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $this->assertEquals(3, $matrix->n()); } /** @@ -291,13 +281,17 @@ public function n() : void */ public function rowAsVector() : void { - $z = $this->a->rowAsVector(1); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $b = $a->rowAsVector(1); - $expected = [4, 11, -2]; + $expected = Vector::quick([4, 11, -2]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(3, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -305,13 +299,17 @@ public function rowAsVector() : void */ public function columnAsVector() : void { - $z = $this->a->columnAsVector(1); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $b = $a->columnAsVector(1); - $expected = [-17, 11, -6]; + $expected = ColumnVector::quick([-17, 11, -6]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(3, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -319,13 +317,17 @@ public function columnAsVector() : void */ public function diagonalAsVector() : void { - $z = $this->a->diagonalAsVector(); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $b = $a->diagonalAsVector(); - $expected = [22, 11, -9]; + $expected = Vector::quick([22, 11, -9]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertCount(3, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -333,7 +335,11 @@ public function diagonalAsVector() : void */ public function asArray() : void { - $z = $this->a->asArray(); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); $expected = [ [22, -17, 12], @@ -341,7 +347,7 @@ public function asArray() : void [20, -6, -9], ]; - $this->assertEquals($expected, $z); + $this->assertEquals($expected, $matrix->asArray()); } /** @@ -349,15 +355,23 @@ public function asArray() : void */ public function asVectors() : void { - $vectors = $this->a->asVectors(); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); - $this->assertInstanceOf(Vector::class, $vectors[0]); - $this->assertInstanceOf(Vector::class, $vectors[1]); - $this->assertInstanceOf(Vector::class, $vectors[2]); + $vectors = $matrix->asVectors(); + + $expected = [ + Vector::quick([22, -17, 12]), + Vector::quick([4, 11, -2]), + Vector::quick([20, -6, -9]), + ]; - $this->assertEquals([22, -17, 12], $vectors[0]->asArray()); - $this->assertEquals([4, 11, -2], $vectors[1]->asArray()); - $this->assertEquals([20, -6, -9], $vectors[2]->asArray()); + foreach ($vectors as $i => $vector) { + $this->assertEquals($expected[$i], $vector); + } } /** @@ -365,15 +379,23 @@ public function asVectors() : void */ public function asColumnVectors() : void { - $vectors = $this->a->asColumnVectors(); + $matrix = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $vectors = $matrix->asColumnVectors(); - $this->assertInstanceOf(ColumnVector::class, $vectors[0]); - $this->assertInstanceOf(ColumnVector::class, $vectors[1]); - $this->assertInstanceOf(ColumnVector::class, $vectors[2]); + $expected = [ + ColumnVector::quick([22, 4, 20]), + ColumnVector::quick([-17, 11, -6]), + ColumnVector::quick([12, -2, -9]), + ]; - $this->assertEquals([22, 4, 20], $vectors[0]->asArray()); - $this->assertEquals([-17, 11, -6], $vectors[1]->asArray()); - $this->assertEquals([12, -2, -9], $vectors[2]->asArray()); + foreach ($vectors as $i => $vector) { + $this->assertEquals($expected[$i], $vector); + } } /** @@ -381,12 +403,17 @@ public function asColumnVectors() : void */ public function flatten() : void { - $z = $this->a->flatten(); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); + + $b = $a->flatten(); - $expected = [22, -17, 12, 4, 11, -2, 20, -6, -9]; + $expected = Vector::quick([22, -17, 12, 4, 11, -2, 20, -6, -9]); - $this->assertInstanceOf(Vector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -394,16 +421,21 @@ public function flatten() : void */ public function transpose() : void { - $z = $this->a->transpose(); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); - $expected = [ + $b = $a->transpose(); + + $expected = Matrix::quick([ [22, 4, 20], [-17, 11, -6], [12, -2, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -411,16 +443,21 @@ public function transpose() : void */ public function inverse() : void { - $z = $this->a->inverse(); + $a = Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]); - $expected = [ + $b = $a->inverse(); + + $expected = Matrix::quick([ [0.02093549603923048, 0.042436816295737464, 0.018483591097698978], [0.0007544322897019996, 0.08261033572236892, -0.017351942663145988], [0.04602036967182196, 0.03923047906450396, -0.05846850245190495], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -429,21 +466,20 @@ public function inverse() : void */ public function pseudoinverse() : void { - $a = Matrix::build([ + $a = Matrix::quick([ [22, -17, 12], [4, 11, -2], ]); - $z = $a->pseudoinverse(); + $b = $a->pseudoinverse(); - $expected = [ + $expected = Matrix::quick([ [0.03147992432205172, 0.05583000490505223], [-0.009144418751313844, 0.0700371382524], [0.012665545511877228, -0.00313572980169575], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -451,8 +487,13 @@ public function pseudoinverse() : void */ public function det() : void { - $this->assertEquals(-5301.999999999999, $this->a->det()); - $this->assertEquals(-544.0, $this->c->det()); + $a = Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]); + + $this->assertEquals(-544.0, $a->det()); } /** @@ -460,24 +501,49 @@ public function det() : void */ public function trace() : void { - $this->assertEquals(24.0, $this->a->trace()); - $this->assertEquals(21.0, $this->c->trace()); + $a = Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]); + + $this->assertEquals(21.0, $a->trace()); } /** * @test + * @dataProvider symmetricProvider + * + * @param \Tensor\Matrix $matrix + * @param bool $expected */ - public function symmetric() : void + public function symmetric(Matrix $matrix, $expected) : void { - $a = Matrix::quick([ - [1, 5, 2], - [5, 1, 3], - [2, 3, 1], - ]); + $this->assertEquals($expected, $matrix->symmetric()); + } - $this->assertTrue($a->symmetric()); + /** + * @return \Generator + */ + public function symmetricProvider() : Generator + { + yield [ + Matrix::quick([ + [22, -17, 12], + [4, 11, -2], + [20, -6, -9], + ]), + false, + ]; - $this->assertFalse($this->a->symmetric()); + yield [ + Matrix::quick([ + [1, 5, 2], + [5, 1, 3], + [2, 3, 1], + ]), + true, + ]; } /** @@ -485,8 +551,13 @@ public function symmetric() : void */ public function rank() : void { - $this->assertEquals(3, $this->a->rank()); - $this->assertEquals(3, $this->c->rank()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertEquals(3, $a->rank()); } /** @@ -494,8 +565,13 @@ public function rank() : void */ public function fullRank() : void { - $this->assertTrue($this->a->fullRank()); - $this->assertTrue($this->c->fullRank()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertTrue($a->fullRank()); } /** @@ -503,16 +579,21 @@ public function fullRank() : void */ public function reciprocal() : void { - $z = $this->a->reciprocal(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->reciprocal(); + + $expected = Matrix::quick([ [0.045454545454545456, -0.058823529411764705, 0.08333333333333333], [0.25, 0.09090909090909091, -0.5], [0.05, -0.16666666666666666, -0.1111111111111111], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -520,18 +601,25 @@ public function reciprocal() : void */ public function map() : void { - $z = $this->a->map(function ($value) { - return $value > 0. ? 1 : -1; - }); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $sign = function ($value) { + return $value >= 0.0 ? 1 : -1; + }; + + $b = $a->map($sign); + + $expected = Matrix::quick([ [1, -1, 1], [1, 1, -1], [1, -1, -1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -539,20 +627,23 @@ public function map() : void */ public function ref() : void { - $ref = $this->a->ref(); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(REF::class, $ref); + $ref = $matrix->ref(); - $expected = [ + $a = Matrix::quick([ [22, -17, 12], [0, 14.09090909090909, -4.181818181818182], [0, 0, -17.10322580645161], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $ref->a()); - $this->assertEqualsWithDelta($expected, $ref->a()->asArray(), 1e-4); + $expected = new REF($a, 0); - $this->assertEquals(0, $ref->swaps()); + $this->assertEqualsWithDelta($expected, $ref, 1e-8); } /** @@ -560,18 +651,23 @@ public function ref() : void */ public function rref() : void { - $rref = $this->a->rref(); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(RREF::class, $rref); + $rref = $matrix->rref(); - $expected = [ + $a = Matrix::quick([ [1, 0, 0], [0, 1, 0], [0, 0, 1], - ]; + ]); + + $expected = new RREF($a); - $this->assertInstanceOf(Matrix::class, $rref->a()); - $this->assertEqualsWithDelta($expected, $rref->a()->asArray(), 1e-4); + $this->assertEqualsWithDelta($expected, $rref, 1e-8); } /** @@ -579,30 +675,35 @@ public function rref() : void */ public function lu() : void { - $lu = $this->a->lu(); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(LU::class, $lu); + $lu = $matrix->lu(); - $lHat = [ + $l = Matrix::quick([ [1.0, 0, 0], [0.18181818181818182, 1.0, 0], [0.9090909090909091, 0.6709677419354838, 1.0], - ]; + ]); - $uHat = [ + $u = Matrix::quick([ [22, -17, 12], [0, 14.09090909090909, -4.181818181818182], [0, 0, -17.10322580645161], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $lu->l()); - $this->assertInstanceOf(Matrix::class, $lu->u()); + $p = Matrix::quick([ + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + ]); - $this->assertCount(9, $lu->l()); - $this->assertCount(9, $lu->u()); + $expected = new LU($l, $u, $p); - $this->assertEqualsWithDelta($lHat, $lu->l()->asArray(), 1e-8); - $this->assertEqualsWithDelta($uHat, $lu->u()->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $lu, 1e-8); } /** @@ -610,25 +711,23 @@ public function lu() : void */ public function cholesky() : void { - $a = Matrix::quick([ + $matrix = Matrix::quick([ [2, -1, 0], [-1, 2, -1], [0, -1, 2], ]); - $cholesky = $a->cholesky(); + $cholesky = $matrix->cholesky(); - $this->assertInstanceOf(Cholesky::class, $cholesky); - - $expected = [ + $l = Matrix::quick([ [1.4142135623730951, 0, 0], [-0.7071067811865475, 1.224744871391589, 0], [0, -0.8164965809277261, 1.1547005383792515], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $cholesky->l()); - $this->assertCount(9, $cholesky->l()); - $this->assertEquals($expected, $cholesky->l()->asArray()); + $expected = new Cholesky($l); + + $this->assertEquals($expected, $cholesky); } /** @@ -637,24 +736,25 @@ public function cholesky() : void */ public function eig() : void { - $eig = $this->a->eig(false); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(Eigen::class, $eig); + $eig = $matrix->eig(false); $values = [-15.096331148319537, 25.108706520450326, 13.9876246278692]; - $vectors = [ + $vectors = Matrix::quick([ [0.25848694820886425, -0.11314537870318066, -0.9593657388523845], [-0.8622719261400653, -0.17721179605718698, -0.47442924101375483], [-0.6684472200177011, -0.6126879076802705, -0.42165369894378907], - ]; - - $this->assertInstanceOf(Matrix::class, $eig->eigenvectors()); + ]); - $eigenvalues = $eig->eigenvalues(); + $expected = new Eigen($values, $vectors); - $this->assertEqualsWithDelta($values, $eigenvalues, 1e-8); - $this->assertEqualsWithDelta($vectors, $eig->eigenvectors()->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $eig, 1e-8); } /** @@ -663,24 +763,25 @@ public function eig() : void */ public function eigSymmetric() : void { - $eig = $this->a->matmul($this->a)->eig(true); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(Eigen::class, $eig); + $eig = $matrix->matmul($matrix)->eig(true); $values = [-366.30071669298195, 335.92000012383926, 1084.3807165691428]; - $vectors = [ + $vectors = Matrix::quick([ [0.5423765325213931, 0.8162941265260668, -0.19872492538460218], [-0.04667292577741032, 0.26544998308386847, 0.9629942598375911], [-0.8388380862654284, 0.5130304137961217, -0.1820726765627782], - ]; - - $this->assertInstanceOf(Matrix::class, $eig->eigenvectors()); + ]); - $eigenvalues = $eig->eigenvalues(); + $expected = new Eigen($values, $vectors); - $this->assertEqualsWithDelta($values, $eigenvalues, 1e-8); - $this->assertEqualsWithDelta($vectors, $eig->eigenvectors()->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $eig, 1e-8); } /** @@ -689,30 +790,33 @@ public function eigSymmetric() : void */ public function svd() : void { - $svd = $this->a->svd(); + $matrix = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $uHat = [ + $svd = $matrix->svd(); + + $u = Matrix::quick([ [-0.8436018806559158, 0.4252547343454771, -0.3278631999333884], [0.08179499775610413, -0.5016868397437385, -0.8611735557772425], [-0.5307027843302525, -0.7533052009276842, 0.38844025146657923], - ]; + ]); $singularValues = [ 34.66917512262571, 17.12630582468919, 8.929610580306822, ]; - $vtHat = [ + $vT = Matrix::quick([ [-0.8320393250771425, 0.531457514846513, -0.15894486917903863], [-0.4506078135544562, -0.48043370238236727, 0.7524201326246152], [-0.3235168618307952, -0.6976649392999047, -0.6392186422366096], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $svd->u()); - $this->assertInstanceOf(Matrix::class, $svd->vT()); + $expected = new SVD($u, $singularValues, $vT); - $this->assertEqualsWithDelta($uHat, $svd->u()->asArray(), 1e-8); - $this->assertEqualsWithDelta($singularValues, $svd->singularValues(), 1e-8); - $this->assertEqualsWithDelta($vtHat, $svd->vT()->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $svd, 1e-8); } /** @@ -720,25 +824,45 @@ public function svd() : void */ public function matmul() : void { - $z = $this->a->matmul($this->b); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = Matrix::quick([ + [13], + [11], + [9], + ]); + + $c = $a->matmul($b); + + $expected = Matrix::quick([ [207], [155], [113], - ]; + ]); + + $this->assertEquals($expected, $c); + } + + /** + * @test + */ + public function dotVector() : void + { + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $b = Vector::quick([2, 10, -1]); - $z = $this->a->matmul($this->c); + $c = $a->dot($b); - $expected = [ - [-49.0, 69.0, -181.0], - [47.0, 59.0, -21.0], - [164.0, 111.0, -396.0], - ]; + $expected = ColumnVector::quick([-138, 120, -11]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** @@ -746,7 +870,7 @@ public function matmul() : void */ public function convolve() : void { - $input = Matrix::quick([ + $a = Matrix::quick([ [3, 27, 66, 29, 42, 5], [5, 9, 15, 42, 45, 16], [1, 5, 10, 22, 66, 5], @@ -755,837 +879,982 @@ public function convolve() : void [0, 0, 0, 5, 2, 33, 35], ]); - $kernel = Matrix::quick([ + $b = Matrix::quick([ [0, 0, 1], [0, 1, 0], [1, 0, 0], ]); - $z = $input->convolve($kernel, 1); + $c = $a->convolve($b, 1); - $expected = [ + $expected = Matrix::quick([ [3, 32, 75, 44, 84, 50], [32, 76, 49, 94, 72, 82], [10, 20, 53, 71, 91, 15], [5, 11, 26, 78, 34, 43], [1, 4, 12, 29, 48, 27], [0, 3, 19, 26, 27, 33], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** * @test + * @dataProvider multiplyProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function multiplyMatrix() : void + public function multiply(Matrix $a, $b, $expected) : void { - $z = $this->a->multiply($this->c); - - $expected = [ - [88, -102, -144], - [4, 33, -10], - [-200, 6, -126], - ]; + $c = $a->multiply($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function divideMatrix() : void + public function multiplyProvider() : Generator { - $z = $this->a->divide($this->c); - - $expected = [ - [5.5, -2.8333333333333335, -1], - [4, 3.6666666666666665, -0.4], - [-2, 6, -0.6428571428571429], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [88, -102, -144], + [4, 33, -10], + [-200, 6, -126], + ]), ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function addMatrix() : void - { - $z = $this->a->add($this->c); - - $expected = [ - [26, -11, 0], - [5, 14, 3], - [10, -7, 5], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function subtractMatrix() : void - { - $z = $this->a->subtract($this->c); - - $expected = [ - [18, -23, 24], - [3, 8, -7], - [30, -5, -23], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function powMatrix() : void - { - $z = $this->a->pow($this->c); - - $expected = [ - [234256, 24137569, 1.1215665478461509E-13], - [4, 1331, -32], - [9.765625E-14, -0.16666666666666666, 22876792454961], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function modMatrix() : void - { - $z = $this->a->mod($this->c); - - $expected = [ - [2, -5, 0], - [0, 2, -2], - [0, 0, -9], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function equalMatrix() : void - { - $z = $this->a->equal($this->c); - - $expected = [ - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [44, -170, -12], + [8, 110, 2], + [40, -60, 9], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function notEqualMatrix() : void - { - $z = $this->a->notEqual($this->c); - - $expected = [ - [1, 1, 1], - [1, 1, 1], - [1, 1, 1], + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [55.0, -42.5, 30.], + [-4, -11, 2], + [96.0, -28.799999999999997, -43.199999999999996], + ]), ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function greaterMatrix() : void - { - $z = $this->a->greater($this->c); - - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 2.5, + Matrix::quick([ + [55, -42.5, 30], + [10.0, 27.5, -5.], + [50, -15, -22.5], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider divideProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function greaterEqualMatrix() : void + public function divide(Matrix $a, $b, $expected) : void { - $z = $this->a->greaterEqual($this->c); + $c = $a->divide($b); - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function lessMatrix() : void - { - $z = $this->a->less($this->c); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEqualsWithDelta($expected, $c, 1e-8); } /** - * @test + * @return \Generator */ - public function lessEqualMatrix() : void + public function divideProvider() : Generator { - $z = $this->a->lessEqual($this->c); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [5.5, -2.8333333333333335, -1], + [4, 3.6666666666666665, -0.4], + [-2, 6, -0.6428571428571429], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [11, -1.7, -12], + [2, 1.1, 2], + [10, -0.6, 9], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [8.8, -6.8, 4.8], + [-4, -11, 2], + [4.166666666666667, -1.25, -1.875], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 2.0, + Matrix::quick([ + [11.0, -8.5, 6.], + [2.0, 5.5, -1.], + [10.0, -3.0, -4.5], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider addProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function dotVector() : void + public function add(Matrix $a, $b, $expected) : void { - $z = $this->a->dot($this->d); + $c = $a->add($b); - $expected = [-138, 120, -11]; - - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function multiplyVector() : void + public function addProvider() : Generator { - $z = $this->a->multiply($this->d); - - $expected = [ - [44, -170, -12], - [8, 110, 2], - [40, -60, 9], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [26, -11, 0], + [5, 14, 3], + [10, -7, 5], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [24, -7, 11], + [6, 21, -3], + [22, 4, -10], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [24.5, -14.5, 14.5], + [3, 10, -3], + [24.8, -1.2000000000000002, -4.2], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 1.0, + Matrix::quick([ + [23, -16, 13], + [5, 12, -1], + [21, -5, -8], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider subtractProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function divideVector() : void + public function subtract(Matrix $a, $b, $expected) : void { - $z = $this->a->divide($this->d); - - $expected = [ - [11, -1.7, -12], - [2, 1.1, 2], - [10, -0.6, 9], - ]; + $c = $a->subtract($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function addVector() : void + public function subtractProvider() : Generator { - $z = $this->a->add($this->d); - - $expected = [ - [24, -7, 11], - [6, 21, -3], - [22, 4, -10], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [18, -23, 24], + [3, 8, -7], + [30, -5, -23], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [20, -27, 13], + [2, 1, -1], + [18, -16, -8], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [19.5, -19.5, 9.5], + [5, 12, -1], + [15.2, -10.8, -13.8], + ]), ]; - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function subtractVector() : void - { - $z = $this->a->subtract($this->d); - - $expected = [ - [20, -27, 13], - [2, 1, -1], - [18, -16, -8], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 10.0, + Matrix::quick([ + [12, -27, 2], + [-6, 1, -12], + [10, -16, -19], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider powProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function powVector() : void + public function pow(Matrix $a, $b, $expected) : void { - $z = $this->a->pow($this->d); - - $expected = [ - [484, 2015993900449, 0.08333333333333333], - [16, 25937424601, -0.5], - [400, 60466176, -0.1111111111111111], - ]; + $c = $a->pow($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function modVector() : void + public function powProvider() : Generator { - $z = $this->a->mod($this->d); - - $expected = [ - [0, -7, 0], - [0, 1, 0], - [0, -6, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [234256, 24137569, 1.1215665478461509E-13], + [4, 1331, -32], + [9.765625E-14, -0.16666666666666666, 22876792454961], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [484, 2015993900449, 0.08333333333333333], + [16, 25937424601, -0.5], + [400, 60466176, -0.1111111111111111], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 3.0, + Matrix::quick([ + [10648, -4913, 1728], + [64, 1331, -8], + [8000, -216, -729], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider modProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function equalVector() : void + public function mod(Matrix $a, $b, $expected) : void { - $z = $this->a->equal($this->d); + $c = $a->mod($b); - $expected = [ - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function notEqualVector() : void + public function modProvider() : Generator { - $z = $this->a->notEqual($this->d); - - $expected = [ - [1, 1, 1], - [1, 1, 1], - [1, 1, 1], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [2, -5, 0], + [0, 2, -2], + [0, 0, -9], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [0, -7, 0], + [0, 1, 0], + [0, -6, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [0, -1, 0], + [0, 0, 0], + [0, -2, -1] + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 10.0, + Matrix::quick([ + [2, -7, 2], + [4, 1, -2], + [0, -6, -9], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider equalProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function greaterVector() : void + public function equal(Matrix $a, $b, $expected) : void { - $z = $this->a->greater($this->d); - - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], - ]; + $c = $a->equal($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function greaterEqualVector() : void + public function equalProvider() : Generator { - $z = $this->a->greaterEqual($this->d); - - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 4.0, + Matrix::quick([ + [0, 0, 0], + [1, 0, 0], + [0, 0, 0], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider notEqualProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function lessVector() : void + public function notEqual(Matrix $a, $b, $expected) : void { - $z = $this->a->less($this->d); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], - ]; + $c = $a->notEqual($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function lessEqualVector() : void + public function notEqualProvider() : Generator { - $z = $this->a->less($this->d); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [1, 1, 1], + [1, 1, 1], + [1, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [1, 1, 1], + [1, 1, 1], + [1, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [1, 1, 1], + [1, 1, 1], + [1, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 4.0, + Matrix::quick([ + [1, 1, 1], + [0, 1, 1], + [1, 1, 1], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider greaterProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function multiplyColumnVector() : void + public function greater(Matrix $a, $b, $expected) : void { - $z = $this->a->multiply($this->e); + $c = $a->greater($b); - $expected = [ - [55.0, -42.5, 30.], - [-4, -11, 2], - [96.0, -28.799999999999997, -43.199999999999996], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function divideColumnVector() : void + public function greaterProvider() : Generator { - $z = $this->a->divide($this->e); - - $expected = [ - [8.8, -6.8, 4.8], - [-4, -11, 2], - [4.166666666666667, -1.25, -1.875], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 4.0, + Matrix::quick([ + [1, 0, 1], + [0, 1, 0], + [1, 0, 0], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); } /** * @test + * @dataProvider greaterEqualProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function addColumnVector() : void + public function greaterEqual(Matrix $a, $b, $expected) : void { - $z = $this->a->add($this->e); + $c = $a->greaterEqual($b); - $expected = [ - [24.5, -14.5, 14.5], - [3, 10, -3], - [24.8, -1.2000000000000002, -4.2], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function subtractColumnVector() : void + public function greaterEqualProvider() : Generator { - $z = $this->a->subtract($this->e); - - $expected = [ - [19.5, -19.5, 9.5], - [5, 12, -1], - [15.2, -10.8, -13.8], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 4.0, + Matrix::quick([ + [1, 0, 1], + [1, 1, 0], + [1, 0, 0], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider lessProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function modColumnVector() : void + public function less(Matrix $a, $b, $expected) : void { - $z = $this->a->mod($this->e); - - $expected = [ - [0, -1, 0], - [0, 0, 0], - [0, -2, -1] - ]; + $c = $a->less($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function equalColumnVector() : void + public function lessProvider() : Generator { - $z = $this->a->equal($this->e); - - $expected = [ - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 10.0, + Matrix::quick([ + [0, 1, 0], + [1, 0, 1], + [0, 1, 1], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** * @test + * @dataProvider lessEqualProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function notEqualColumnVector() : void + public function lessEqual(Matrix $a, $b, $expected) : void { - $z = $this->a->notEqual($this->e); - - $expected = [ - [1, 1, 1], - [1, 1, 1], - [1, 1, 1], - ]; + $c = $a->lessEqual($b); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** - * @test + * @return \Generator */ - public function greaterColumnVector() : void + public function lessEqualProvider() : Generator { - $z = $this->a->greater($this->e); + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + Vector::quick([2, 10, -1]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; + + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + ColumnVector::quick([2.5, -1, 4.8]), + Matrix::quick([ + [0, 1, 0], + [0, 0, 1], + [0, 1, 1], + ]), + ]; - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + 10.0, + Matrix::quick([ + [0, 1, 0], + [1, 0, 1], + [0, 1, 1], + ]), ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function greaterEqualColumnVector() : void - { - $z = $this->a->greaterEqual($this->e); - - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function lessColumnVector() : void - { - $z = $this->a->less($this->e); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function lessEqualColumnVector() : void - { - $z = $this->a->lessEqual($this->e); - - $expected = [ - [0, 1, 0], - [0, 0, 1], - [0, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function multiplyScalar() : void - { - $z = $this->a->multiply(2.5); - - $expected = [ - [55, -42.5, 30], - [10.0, 27.5, -5.], - [50, -15, -22.5], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function divideScalar() : void - { - $z = $this->a->divide(2.); - - $expected = [ - [11.0, -8.5, 6.], - [2.0, 5.5, -1.], - [10.0, -3.0, -4.5], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function addScalar() : void - { - $z = $this->a->add(1); - - $expected = [ - [23, -16, 13], - [5, 12, -1], - [21, -5, -8], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function subtractScalar() : void - { - $z = $this->a->subtract(10); - - $expected = [ - [12, -27, 2], - [-6, 1, -12], - [10, -16, -19], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function powScalar() : void - { - $z = $this->a->pow(3); - - $expected = [ - [10648, -4913, 1728], - [64, 1331, -8], - [8000, -216, -729], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function modScalar() : void - { - $z = $this->a->mod(10); - - $expected = [ - [2, -7, 2], - [4, 1, -2], - [0, -6, -9], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function equalScalar() : void - { - $z = $this->a->equal(4); - - $expected = [ - [0, 0, 0], - [1, 0, 0], - [0, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function notEqualScalar() : void - { - $z = $this->a->notEqual(4); - - $expected = [ - [1, 1, 1], - [0, 1, 1], - [1, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function greaterScalar() : void - { - $z = $this->a->greater(4); - - $expected = [ - [1, 0, 1], - [0, 1, 0], - [1, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function greaterEqualScalar() : void - { - $z = $this->a->greaterEqual(4); - - $expected = [ - [1, 0, 1], - [1, 1, 0], - [1, 0, 0], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function lessScalar() : void - { - $z = $this->a->less(10); - - $expected = [ - [0, 1, 0], - [1, 0, 1], - [0, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); - } - - /** - * @test - */ - public function lessEqualScalar() : void - { - $z = $this->a->lessEqual(10); - - $expected = [ - [0, 1, 0], - [1, 0, 1], - [0, 1, 1], - ]; - - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); } /** @@ -1593,16 +1862,21 @@ public function lessEqualScalar() : void */ public function abs() : void { - $z = $this->a->abs(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->abs(); + + $expected = Matrix::quick([ [22, 17, 12], [4, 11, 2], [20, 6, 9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1610,33 +1884,43 @@ public function abs() : void */ public function square() : void { - $z = $this->a->square(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->square(); + + $expected = Matrix::quick([ [484, 289, 144], [16, 121, 4], [400, 36, 81], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } - /** - * @test - */ - public function sqrt() : void - { - $z = $this->b->sqrt(); + /** + * @test + */ + public function sqrt() : void + { + $a = Matrix::quick([ + [13], + [11], + [9], + ]); + + $b = $a->sqrt(); - $expected = [ + $expected = Matrix::quick([ [3.605551275463989], [3.3166247903554], [3], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1644,16 +1928,22 @@ public function sqrt() : void */ public function exp() : void { - $z = $this->b->exp(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->exp(); + + $expected = Matrix::quick([ [442413.3920089205], [59874.14171519778], [8103.08392757538], - ]; + ]); + - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1661,16 +1951,21 @@ public function exp() : void */ public function expm1() : void { - $z = $this->b->expm1(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->expm1(); + + $expected = Matrix::quick([ [442412.3920089205], [59873.14171519782], [8102.083927575384], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1678,16 +1973,21 @@ public function expm1() : void */ public function log() : void { - $z = $this->b->log(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->log(); + + $expected = Matrix::quick([ [2.5649493574615367], [2.3978952727983707], [2.1972245773362196], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1695,16 +1995,21 @@ public function log() : void */ public function log1p() : void { - $z = $this->b->log1p(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->log1p(); + + $expected = Matrix::quick([ [2.6390573296152584], [2.4849066497880004], [2.302585092994046], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1712,16 +2017,21 @@ public function log1p() : void */ public function sin() : void { - $z = $this->b->sin(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->sin(); + + $expected = Matrix::quick([ [0.4201670368266409], [-0.9999902065507035], [0.4121184852417566], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1729,20 +2039,21 @@ public function sin() : void */ public function asin() : void { - $z = Matrix::quick([ + $a = Matrix::quick([ [0.32], [-0.5], [0.01], - ])->asin(); + ]); - $expected = [ + $b = $a->asin(); + + $expected = Matrix::quick([ [0.3257294872946302], [-0.5235987755982989], [0.010000166674167114], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1750,16 +2061,21 @@ public function asin() : void */ public function cos() : void { - $z = $this->b->cos(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->cos(); + + $expected = Matrix::quick([ [0.9074467814501962], [0.004425697988050785], [-0.9111302618846769], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1767,20 +2083,21 @@ public function cos() : void */ public function acos() : void { - $z = Matrix::quick([ + $a = Matrix::quick([ [0.32], [-0.5], [0.01], - ])->acos(); + ]); + + $b = $a->acos(); - $expected = [ + $expected = Matrix::quick([ [1.2450668395002664], [2.0943951023931957], [1.5607961601207294], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1788,16 +2105,21 @@ public function acos() : void */ public function tan() : void { - $z = $this->b->tan(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->tan(); + + $expected = Matrix::quick([ [0.4630211329364896], [-225.95084645419513], [-0.45231565944180985], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1805,16 +2127,21 @@ public function tan() : void */ public function atan() : void { - $z = $this->b->atan(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->atan(); + + $expected = Matrix::quick([ [1.4940244355251187], [1.4801364395941514], [1.460139105621001], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1822,16 +2149,21 @@ public function atan() : void */ public function rad2deg() : void { - $z = $this->b->rad2deg(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->rad2deg(); + + $expected = Matrix::quick([ [744.8451336700701], [630.2535746439056], [515.6620156177408], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1839,16 +2171,21 @@ public function rad2deg() : void */ public function deg2rad() : void { - $z = $this->b->deg2rad(); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->deg2rad(); + + $expected = Matrix::quick([ [0.22689280275926282], [0.19198621771937624], [0.15707963267948966], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1856,12 +2193,17 @@ public function deg2rad() : void */ public function sum() : void { - $z = $this->a->sum(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [17, 13, 5]; + $b = $a->sum(); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = ColumnVector::quick([17, 13, 5]); + + $this->assertEquals($expected, $b); } /** @@ -1869,12 +2211,17 @@ public function sum() : void */ public function product() : void { - $z = $this->a->product(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $b = $a->product(); - $expected = [-4488.0, -88.0, 1080.]; + $expected = ColumnVector::quick([-4488.0, -88.0, 1080.0]); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1882,12 +2229,17 @@ public function product() : void */ public function min() : void { - $z = $this->a->min(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [-17, -2, -9]; + $b = $a->min(); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = ColumnVector::quick([-17, -2, -9]); + + $this->assertEquals($expected, $b); } /** @@ -1895,12 +2247,17 @@ public function min() : void */ public function max() : void { - $z = $this->a->max(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $b = $a->max(); - $expected = [22, 11, 20]; + $expected = ColumnVector::quick([22, 11, 20]); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1908,12 +2265,17 @@ public function max() : void */ public function mean() : void { - $z = $this->a->mean(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [5.666666666666667, 4.333333333333333, 1.6666666666666667]; + $b = $a->mean(); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $expected = ColumnVector::quick([5.666666666666667, 4.333333333333333, 1.6666666666666667]); + + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1921,12 +2283,17 @@ public function mean() : void */ public function median() : void { - $z = $this->a->median(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $b = $a->median(); - $expected = [12, 4, -6]; + $expected = ColumnVector::quick([12, 4, -6]); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1934,12 +2301,17 @@ public function median() : void */ public function quantile() : void { - $z = $this->a->quantile(0.4); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [6.200000000000003, 2.8000000000000007, -6.6]; + $b = $a->quantile(0.4); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEquals($expected, $z->asArray()); + $expected = ColumnVector::quick([6.200000000000003, 2.8000000000000007, -6.6]); + + $this->assertEquals($expected, $b); } /** @@ -1947,12 +2319,17 @@ public function quantile() : void */ public function variance() : void { - $z = $this->a->variance(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $b = $a->variance(); - $expected = [273.55555555555554, 28.222222222222225, 169.55555555555554]; + $expected = ColumnVector::quick([273.55555555555554, 28.222222222222225, 169.55555555555554]); - $this->assertInstanceOf(ColumnVector::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1960,16 +2337,21 @@ public function variance() : void */ public function covariance() : void { - $z = $this->a->covariance(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->covariance(); + + $expected = Matrix::quick([ [273.55555555555554, -65.55555555555556, 135.2222222222222], [-65.55555555555556, 28.222222222222225, 3.4444444444444406], [135.2222222222222, 3.4444444444444406, 169.55555555555554], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEqualsWithDelta($expected, $z->asArray(), 1e-8); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1977,16 +2359,21 @@ public function covariance() : void */ public function round() : void { - $z = $this->a->round(2); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->round(2); + + $expected = Matrix::quick([ [22, -17, 12], [4, 11, -2], [20, -6, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -1994,16 +2381,21 @@ public function round() : void */ public function floor() : void { - $z = $this->a->floor(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->floor(); + + $expected = Matrix::quick([ [22, -17, 12], [4, 11, -2], [20, -6, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2011,16 +2403,21 @@ public function floor() : void */ public function ceil() : void { - $z = $this->a->ceil(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->ceil(); + + $expected = Matrix::quick([ [22, -17, 12], [4, 11, -2], [20, -6, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2028,9 +2425,19 @@ public function ceil() : void */ public function l1Norm() : void { - $this->assertEquals(46.0, $this->a->l1Norm()); - $this->assertEquals(33.0, $this->b->l1Norm()); - $this->assertEquals(31.0, $this->c->l1Norm()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertEquals(46.0, $a->l1Norm()); } /** @@ -2038,9 +2445,13 @@ public function l1Norm() : void */ public function l2Norm() : void { - $this->assertEquals(39.68626966596886, $this->a->l2Norm()); - $this->assertEquals(19.261360284258224, $this->b->l2Norm()); - $this->assertEquals(22.978250586152114, $this->c->l2Norm()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertEquals(39.68626966596886, $a->l2Norm()); } /** @@ -2048,9 +2459,13 @@ public function l2Norm() : void */ public function infinityNorm() : void { - $this->assertEquals(51.0, $this->a->infinityNorm()); - $this->assertEquals(13.0, $this->b->infinityNorm()); - $this->assertEquals(25.0, $this->c->infinityNorm()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertEquals(51.0, $a->infinityNorm()); } /** @@ -2058,9 +2473,13 @@ public function infinityNorm() : void */ public function maxNorm() : void { - $this->assertEquals(22.0, $this->a->maxNorm()); - $this->assertEquals(13.0, $this->b->maxNorm()); - $this->assertEquals(14.0, $this->c->maxNorm()); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); + + $this->assertEquals(22.0, $a->maxNorm()); } /** @@ -2068,16 +2487,21 @@ public function maxNorm() : void */ public function clip() : void { - $z = $this->a->clip(0.0, INF); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->clip(0.0, INF); + + $expected = Matrix::quick([ [22, 0.0, 12], [4, 11, 0.], [20, 0.0, 0.], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2085,16 +2509,21 @@ public function clip() : void */ public function clipLower() : void { - $z = $this->a->clipLower(5.); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->clipLower(5.); + + $expected = Matrix::quick([ [22, 5.0, 12], [5.0, 11, 5.], [20, 5.0, 5.], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2102,16 +2531,21 @@ public function clipLower() : void */ public function clipUpper() : void { - $z = $this->a->clipUpper(16.); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->clipUpper(16.0); + + $expected = Matrix::quick([ [16.0, -17.0, 12], - [4, 11, -2.], - [16, -6.0, -9.], - ]; + [4, 11, -2.0], + [16, -6.0, -9.0], + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2119,16 +2553,21 @@ public function clipUpper() : void */ public function sign() : void { - $z = $this->a->sign(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->sign(); + + $expected = Matrix::quick([ [1, -1, 1], [1, 1, -1], [1, -1, -1], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2136,16 +2575,21 @@ public function sign() : void */ public function negate() : void { - $z = $this->a->negate(); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = $a->negate(); + + $expected = Matrix::quick([ [-22, 17, -12], [-4, -11, 2], [-20, 6, 9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } /** @@ -2153,19 +2597,30 @@ public function negate() : void */ public function augmentAbove() : void { - $z = $this->a->augmentAbove($this->c); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]); + + $c = $a->augmentAbove($b); + + $expected = Matrix::quick([ [4, 6, -12], [1, 3, 5], [-10, -1, 14], [22, -17, 12], [4, 11, -2], [20, -6, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** @@ -2173,19 +2628,30 @@ public function augmentAbove() : void */ public function augmentBelow() : void { - $z = $this->a->augmentBelow($this->c); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = Matrix::quick([ + [4, 6, -12], + [1, 3, 5], + [-10, -1, 14], + ]); + + $c = $a->augmentBelow($b); + + $expected = Matrix::quick([ [22, -17, 12], [4, 11, -2], [20, -6, -9], [4, 6, -12], [1, 3, 5], [-10, -1, 14], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** @@ -2193,16 +2659,27 @@ public function augmentBelow() : void */ public function augmentLeft() : void { - $z = $this->a->augmentLeft($this->b); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = Matrix::quick([ + [13], + [11], + [9], + ]); + + $c = $a->augmentLeft($b); + + $expected = Matrix::quick([ [13, 22, -17, 12], [11, 4, 11, -2], [9, 20, -6, -9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** @@ -2210,16 +2687,27 @@ public function augmentLeft() : void */ public function augmentRight() : void { - $z = $this->a->augmentRight($this->b); + $a = Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]); - $expected = [ + $b = Matrix::quick([ + [13], + [11], + [9], + ]); + + $c = $a->augmentRight($b); + + $expected = Matrix::quick([ [22, -17, 12, 13], [4, 11, -2, 11], [20, -6, -9, 9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $c); } /** @@ -2227,18 +2715,23 @@ public function augmentRight() : void */ public function repeat() : void { - $z = $this->b->repeat(1, 3); + $a = Matrix::quick([ + [13], + [11], + [9], + ]); - $expected = [ + $b = $a->repeat(1, 3); + + $expected = Matrix::quick([ [13, 13, 13, 13], [11, 11, 11, 11], [9, 9, 9, 9], [13, 13, 13, 13], [11, 11, 11, 11], [9, 9, 9, 9], - ]; + ]); - $this->assertInstanceOf(Matrix::class, $z); - $this->assertEquals($expected, $z->asArray()); + $this->assertEquals($expected, $b); } } From adb0a109f4a16683a73c7c59fb0eeed2c56f1e8a Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Mon, 18 Sep 2023 23:57:33 -0500 Subject: [PATCH 05/10] Clean up --- tests/MatrixTest.php | 45 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php index f0a80de..1bf635d 100644 --- a/tests/MatrixTest.php +++ b/tests/MatrixTest.php @@ -733,30 +733,41 @@ public function cholesky() : void /** * @test * @requires extension tensor + * @dataProvider eigProvider + * + * @param \Tensor\Matrix $a + * @param \Tensor\Tensor|float $b + * @param \Tensor\Tensor|float $expected */ - public function eig() : void + public function eig(Matrix $matrix, $expected) : void { - $matrix = Matrix::quick([ - [22.0, -17.0, 12.0], - [4.0, 11.0, -2.0], - [20.0, -6.0, -9.0], - ]); - $eig = $matrix->eig(false); - $values = [-15.096331148319537, 25.108706520450326, 13.9876246278692]; - - $vectors = Matrix::quick([ - [0.25848694820886425, -0.11314537870318066, -0.9593657388523845], - [-0.8622719261400653, -0.17721179605718698, -0.47442924101375483], - [-0.6684472200177011, -0.6126879076802705, -0.42165369894378907], - ]); - - $expected = new Eigen($values, $vectors); - $this->assertEqualsWithDelta($expected, $eig, 1e-8); } + /** + * @return \Generator + */ + public function eigProvider() : Generator + { + yield [ + Matrix::quick([ + [22.0, -17.0, 12.0], + [4.0, 11.0, -2.0], + [20.0, -6.0, -9.0], + ]), + new Eigen([ + -15.096331148319537, 25.108706520450326, 13.9876246278692, + ], Matrix::quick([ + [0.25848694820886425, -0.11314537870318066, -0.9593657388523845], + [-0.8622719261400653, -0.17721179605718698, -0.47442924101375483], + [-0.6684472200177011, -0.6126879076802705, -0.42165369894378907], + ]) + ), + ]; + } + /** * @test * @requires extension tensor From 373844f5c8f1963743c0f3ecfbe432e657537537 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Tue, 19 Sep 2023 00:05:44 -0500 Subject: [PATCH 06/10] Recompile --- ext/php_tensor.h | 4 -- ext/tensor/columnvector.zep.c | 26 +++---- ext/tensor/decompositions/cholesky.zep.c | 4 +- ext/tensor/decompositions/eigen.zep.c | 4 +- ext/tensor/decompositions/lu.zep.c | 8 +-- ext/tensor/decompositions/svd.zep.c | 6 +- ext/tensor/matrix.zep.c | 88 ++++++++++++------------ ext/tensor/reductions/ref.zep.c | 4 +- ext/tensor/reductions/rref.zep.c | 4 +- ext/tensor/vector.zep.c | 58 ++++++++-------- 10 files changed, 101 insertions(+), 105 deletions(-) diff --git a/ext/php_tensor.h b/ext/php_tensor.h index baaf1c4..2d7a15e 100644 --- a/ext/php_tensor.h +++ b/ext/php_tensor.h @@ -14,11 +14,7 @@ #define PHP_TENSOR_VERSION "3.0.3" #define PHP_TENSOR_EXTNAME "tensor" #define PHP_TENSOR_AUTHOR "The Rubix ML Community" -<<<<<<< HEAD #define PHP_TENSOR_ZEPVERSION "0.17.0-$Id$" -======= -#define PHP_TENSOR_ZEPVERSION "0.17.0-9f99da6" ->>>>>>> master #define PHP_TENSOR_DESCRIPTION "A library and extension that provides objects for scientific computing in PHP." diff --git a/ext/tensor/columnvector.zep.c b/ext/tensor/columnvector.zep.c index 6f7d0af..66bf31f 100644 --- a/ext/tensor/columnvector.zep.c +++ b/ext/tensor/columnvector.zep.c @@ -191,7 +191,7 @@ PHP_METHOD(Tensor_ColumnVector, matmul) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -254,7 +254,7 @@ PHP_METHOD(Tensor_ColumnVector, multiplyMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -443,7 +443,7 @@ PHP_METHOD(Tensor_ColumnVector, divideMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -632,7 +632,7 @@ PHP_METHOD(Tensor_ColumnVector, addMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -821,7 +821,7 @@ PHP_METHOD(Tensor_ColumnVector, subtractMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1010,7 +1010,7 @@ PHP_METHOD(Tensor_ColumnVector, powMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1199,7 +1199,7 @@ PHP_METHOD(Tensor_ColumnVector, modMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1388,7 +1388,7 @@ PHP_METHOD(Tensor_ColumnVector, equalMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1601,7 +1601,7 @@ PHP_METHOD(Tensor_ColumnVector, notEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1814,7 +1814,7 @@ PHP_METHOD(Tensor_ColumnVector, greaterMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -2027,7 +2027,7 @@ PHP_METHOD(Tensor_ColumnVector, greaterEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -2240,7 +2240,7 @@ PHP_METHOD(Tensor_ColumnVector, lessMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -2453,7 +2453,7 @@ PHP_METHOD(Tensor_ColumnVector, lessEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/decompositions/cholesky.zep.c b/ext/tensor/decompositions/cholesky.zep.c index f9dae24..786af50 100644 --- a/ext/tensor/decompositions/cholesky.zep.c +++ b/ext/tensor/decompositions/cholesky.zep.c @@ -70,7 +70,7 @@ PHP_METHOD(Tensor_Decompositions_Cholesky, decompose) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -122,7 +122,7 @@ PHP_METHOD(Tensor_Decompositions_Cholesky, __construct) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(l, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(l, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/decompositions/eigen.zep.c b/ext/tensor/decompositions/eigen.zep.c index 07ce670..5a65036 100644 --- a/ext/tensor/decompositions/eigen.zep.c +++ b/ext/tensor/decompositions/eigen.zep.c @@ -87,7 +87,7 @@ PHP_METHOD(Tensor_Decompositions_Eigen, decompose) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) Z_PARAM_OPTIONAL Z_PARAM_BOOL(symmetric) ZEND_PARSE_PARAMETERS_END(); @@ -167,7 +167,7 @@ PHP_METHOD(Tensor_Decompositions_Eigen, __construct) bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ARRAY(eigenvalues) - Z_PARAM_OBJECT_OF_CLASS(eigenvectors, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(eigenvectors, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/decompositions/lu.zep.c b/ext/tensor/decompositions/lu.zep.c index faab245..319b700 100644 --- a/ext/tensor/decompositions/lu.zep.c +++ b/ext/tensor/decompositions/lu.zep.c @@ -92,7 +92,7 @@ PHP_METHOD(Tensor_Decompositions_Lu, decompose) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -159,9 +159,9 @@ PHP_METHOD(Tensor_Decompositions_Lu, __construct) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(3, 3) - Z_PARAM_OBJECT_OF_CLASS(l, tensor_matrix_ce) - Z_PARAM_OBJECT_OF_CLASS(u, tensor_matrix_ce) - Z_PARAM_OBJECT_OF_CLASS(p, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(l, zephir_get_internal_ce(SL("tensor\\matrix"))) + Z_PARAM_OBJECT_OF_CLASS(u, zephir_get_internal_ce(SL("tensor\\matrix"))) + Z_PARAM_OBJECT_OF_CLASS(p, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/decompositions/svd.zep.c b/ext/tensor/decompositions/svd.zep.c index 0135de1..2a9e82b 100644 --- a/ext/tensor/decompositions/svd.zep.c +++ b/ext/tensor/decompositions/svd.zep.c @@ -82,7 +82,7 @@ PHP_METHOD(Tensor_Decompositions_Svd, decompose) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -135,9 +135,9 @@ PHP_METHOD(Tensor_Decompositions_Svd, __construct) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(3, 3) - Z_PARAM_OBJECT_OF_CLASS(u, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(u, zephir_get_internal_ce(SL("tensor\\matrix"))) Z_PARAM_ARRAY(singularValues) - Z_PARAM_OBJECT_OF_CLASS(vT, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(vT, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/matrix.zep.c b/ext/tensor/matrix.zep.c index feb61a4..742573d 100644 --- a/ext/tensor/matrix.zep.c +++ b/ext/tensor/matrix.zep.c @@ -2321,7 +2321,7 @@ PHP_METHOD(Tensor_Matrix, matmul) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -2390,7 +2390,7 @@ PHP_METHOD(Tensor_Matrix, dot) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -2464,7 +2464,7 @@ PHP_METHOD(Tensor_Matrix, convolve) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) Z_PARAM_OPTIONAL Z_PARAM_LONG(stride) ZEND_PARSE_PARAMETERS_END(); @@ -4723,7 +4723,7 @@ PHP_METHOD(Tensor_Matrix, covariance) bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_OBJECT_OF_CLASS_OR_NULL(mean, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS_OR_NULL(mean, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5828,7 +5828,7 @@ PHP_METHOD(Tensor_Matrix, augmentAbove) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5904,7 +5904,7 @@ PHP_METHOD(Tensor_Matrix, augmentBelow) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5981,7 +5981,7 @@ PHP_METHOD(Tensor_Matrix, augmentLeft) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6060,7 +6060,7 @@ PHP_METHOD(Tensor_Matrix, augmentRight) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6262,7 +6262,7 @@ PHP_METHOD(Tensor_Matrix, multiplyMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6379,7 +6379,7 @@ PHP_METHOD(Tensor_Matrix, divideMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6496,7 +6496,7 @@ PHP_METHOD(Tensor_Matrix, addMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6613,7 +6613,7 @@ PHP_METHOD(Tensor_Matrix, subtractMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6731,7 +6731,7 @@ PHP_METHOD(Tensor_Matrix, powMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6849,7 +6849,7 @@ PHP_METHOD(Tensor_Matrix, modMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6967,7 +6967,7 @@ PHP_METHOD(Tensor_Matrix, equalMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7084,7 +7084,7 @@ PHP_METHOD(Tensor_Matrix, notEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7202,7 +7202,7 @@ PHP_METHOD(Tensor_Matrix, greaterMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7320,7 +7320,7 @@ PHP_METHOD(Tensor_Matrix, greaterEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7437,7 +7437,7 @@ PHP_METHOD(Tensor_Matrix, lessMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7554,7 +7554,7 @@ PHP_METHOD(Tensor_Matrix, lessEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7668,7 +7668,7 @@ PHP_METHOD(Tensor_Matrix, multiplyVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7771,7 +7771,7 @@ PHP_METHOD(Tensor_Matrix, divideVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7874,7 +7874,7 @@ PHP_METHOD(Tensor_Matrix, addVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -7977,7 +7977,7 @@ PHP_METHOD(Tensor_Matrix, subtractVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8080,7 +8080,7 @@ PHP_METHOD(Tensor_Matrix, powVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8183,7 +8183,7 @@ PHP_METHOD(Tensor_Matrix, modVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8287,7 +8287,7 @@ PHP_METHOD(Tensor_Matrix, equalVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8390,7 +8390,7 @@ PHP_METHOD(Tensor_Matrix, notEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8493,7 +8493,7 @@ PHP_METHOD(Tensor_Matrix, greaterVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8596,7 +8596,7 @@ PHP_METHOD(Tensor_Matrix, greaterEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8699,7 +8699,7 @@ PHP_METHOD(Tensor_Matrix, lessVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8803,7 +8803,7 @@ PHP_METHOD(Tensor_Matrix, lessEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -8912,7 +8912,7 @@ PHP_METHOD(Tensor_Matrix, multiplyColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9033,7 +9033,7 @@ PHP_METHOD(Tensor_Matrix, divideColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9154,7 +9154,7 @@ PHP_METHOD(Tensor_Matrix, addColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9275,7 +9275,7 @@ PHP_METHOD(Tensor_Matrix, subtractColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9396,7 +9396,7 @@ PHP_METHOD(Tensor_Matrix, powColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9517,7 +9517,7 @@ PHP_METHOD(Tensor_Matrix, modColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9638,7 +9638,7 @@ PHP_METHOD(Tensor_Matrix, equalColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9759,7 +9759,7 @@ PHP_METHOD(Tensor_Matrix, notEqualColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -9880,7 +9880,7 @@ PHP_METHOD(Tensor_Matrix, greaterColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -10001,7 +10001,7 @@ PHP_METHOD(Tensor_Matrix, greaterEqualColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -10122,7 +10122,7 @@ PHP_METHOD(Tensor_Matrix, lessColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -10243,7 +10243,7 @@ PHP_METHOD(Tensor_Matrix, lessEqualColumnVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_columnvector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\columnvector"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/reductions/ref.zep.c b/ext/tensor/reductions/ref.zep.c index 9bf6e9f..12ba9a7 100644 --- a/ext/tensor/reductions/ref.zep.c +++ b/ext/tensor/reductions/ref.zep.c @@ -77,7 +77,7 @@ PHP_METHOD(Tensor_Reductions_Ref, reduce) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -130,7 +130,7 @@ PHP_METHOD(Tensor_Reductions_Ref, __construct) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) Z_PARAM_LONG(swaps) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/reductions/rref.zep.c b/ext/tensor/reductions/rref.zep.c index 8f9a439..5ef2591 100644 --- a/ext/tensor/reductions/rref.zep.c +++ b/ext/tensor/reductions/rref.zep.c @@ -85,7 +85,7 @@ PHP_METHOD(Tensor_Reductions_Rref, reduce) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -232,7 +232,7 @@ PHP_METHOD(Tensor_Reductions_Rref, __construct) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(a, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(a, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif diff --git a/ext/tensor/vector.zep.c b/ext/tensor/vector.zep.c index 320a7cb..1e72c1b 100644 --- a/ext/tensor/vector.zep.c +++ b/ext/tensor/vector.zep.c @@ -1317,7 +1317,7 @@ PHP_METHOD(Tensor_Vector, dot) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1385,7 +1385,7 @@ PHP_METHOD(Tensor_Vector, convolve) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) Z_PARAM_OPTIONAL Z_PARAM_LONG(stride) ZEND_PARSE_PARAMETERS_END(); @@ -1458,7 +1458,7 @@ PHP_METHOD(Tensor_Vector, matmul) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1491,7 +1491,7 @@ PHP_METHOD(Tensor_Vector, inner) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -1542,7 +1542,7 @@ PHP_METHOD(Tensor_Vector, outer) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4025,7 +4025,7 @@ PHP_METHOD(Tensor_Vector, multiplyMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4131,7 +4131,7 @@ PHP_METHOD(Tensor_Vector, divideMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4237,7 +4237,7 @@ PHP_METHOD(Tensor_Vector, addMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4343,7 +4343,7 @@ PHP_METHOD(Tensor_Vector, subtractMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4449,7 +4449,7 @@ PHP_METHOD(Tensor_Vector, powMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4555,7 +4555,7 @@ PHP_METHOD(Tensor_Vector, modMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4661,7 +4661,7 @@ PHP_METHOD(Tensor_Vector, equalMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4767,7 +4767,7 @@ PHP_METHOD(Tensor_Vector, notEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4873,7 +4873,7 @@ PHP_METHOD(Tensor_Vector, greaterMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -4979,7 +4979,7 @@ PHP_METHOD(Tensor_Vector, greaterEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5085,7 +5085,7 @@ PHP_METHOD(Tensor_Vector, lessMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5191,7 +5191,7 @@ PHP_METHOD(Tensor_Vector, lessEqualMatrix) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_matrix_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\matrix"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5290,7 +5290,7 @@ PHP_METHOD(Tensor_Vector, multiplyVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5359,7 +5359,7 @@ PHP_METHOD(Tensor_Vector, divideVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5428,7 +5428,7 @@ PHP_METHOD(Tensor_Vector, addVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5497,7 +5497,7 @@ PHP_METHOD(Tensor_Vector, subtractVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5566,7 +5566,7 @@ PHP_METHOD(Tensor_Vector, powVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5635,7 +5635,7 @@ PHP_METHOD(Tensor_Vector, modVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5705,7 +5705,7 @@ PHP_METHOD(Tensor_Vector, equalVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5774,7 +5774,7 @@ PHP_METHOD(Tensor_Vector, notEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5843,7 +5843,7 @@ PHP_METHOD(Tensor_Vector, greaterVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5912,7 +5912,7 @@ PHP_METHOD(Tensor_Vector, greaterEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -5981,7 +5981,7 @@ PHP_METHOD(Tensor_Vector, lessVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif @@ -6050,7 +6050,7 @@ PHP_METHOD(Tensor_Vector, lessEqualVector) #if PHP_VERSION_ID >= 80000 bool is_null_true = 1; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_OBJECT_OF_CLASS(b, tensor_vector_ce) + Z_PARAM_OBJECT_OF_CLASS(b, zephir_get_internal_ce(SL("tensor\\vector"))) ZEND_PARSE_PARAMETERS_END(); #endif From d8f283fd30f1089866facda87bea39c0aa50f4b0 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Tue, 19 Sep 2023 00:09:23 -0500 Subject: [PATCH 07/10] Appease the linters --- tests/ColumnVectorTest.php | 1 - tests/MatrixTest.php | 38 +++++++++++++++++++------------------- tests/VectorTest.php | 8 ++++---- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/tests/ColumnVectorTest.php b/tests/ColumnVectorTest.php index b98c7d5..659fd45 100644 --- a/tests/ColumnVectorTest.php +++ b/tests/ColumnVectorTest.php @@ -3,7 +3,6 @@ namespace Tensor\Tests; use Tensor\Tensor; -use Tensor\Vector; use Tensor\Matrix; use Tensor\Special; use Tensor\ArrayLike; diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php index 1bf635d..3eede24 100644 --- a/tests/MatrixTest.php +++ b/tests/MatrixTest.php @@ -735,11 +735,10 @@ public function cholesky() : void * @requires extension tensor * @dataProvider eigProvider * - * @param \Tensor\Matrix $a - * @param \Tensor\Tensor|float $b - * @param \Tensor\Tensor|float $expected + * @param \Tensor\Matrix $matrix + * @param \Tensor\Decompositions\Eigen $expected */ - public function eig(Matrix $matrix, $expected) : void + public function eig(Matrix $matrix, Eigen $expected) : void { $eig = $matrix->eig(false); @@ -757,9 +756,11 @@ public function eigProvider() : Generator [4.0, 11.0, -2.0], [20.0, -6.0, -9.0], ]), - new Eigen([ + new Eigen( + [ -15.096331148319537, 25.108706520450326, 13.9876246278692, - ], Matrix::quick([ + ], + Matrix::quick([ [0.25848694820886425, -0.11314537870318066, -0.9593657388523845], [-0.8622719261400653, -0.17721179605718698, -0.47442924101375483], [-0.6684472200177011, -0.6126879076802705, -0.42165369894378907], @@ -994,7 +995,7 @@ public function multiplyProvider() : Generator /** * @test * @dataProvider divideProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1075,7 +1076,7 @@ public function divideProvider() : Generator /** * @test * @dataProvider addProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1156,7 +1157,7 @@ public function addProvider() : Generator /** * @test * @dataProvider subtractProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1237,7 +1238,7 @@ public function subtractProvider() : Generator /** * @test * @dataProvider powProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1304,7 +1305,7 @@ public function powProvider() : Generator /** * @test * @dataProvider modProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1385,7 +1386,7 @@ public function modProvider() : Generator /** * @test * @dataProvider equalProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1466,7 +1467,7 @@ public function equalProvider() : Generator /** * @test * @dataProvider notEqualProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1547,7 +1548,7 @@ public function notEqualProvider() : Generator /** * @test * @dataProvider greaterProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1628,7 +1629,7 @@ public function greaterProvider() : Generator /** * @test * @dataProvider greaterEqualProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1709,7 +1710,7 @@ public function greaterEqualProvider() : Generator /** * @test * @dataProvider lessProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1790,7 +1791,7 @@ public function lessProvider() : Generator /** * @test * @dataProvider lessEqualProvider - * + * * @param \Tensor\Matrix $a * @param \Tensor\Tensor|float $b * @param \Tensor\Tensor|float $expected @@ -1953,7 +1954,6 @@ public function exp() : void [8103.08392757538], ]); - $this->assertEqualsWithDelta($expected, $b, 1e-8); } @@ -2099,7 +2099,7 @@ public function acos() : void [-0.5], [0.01], ]); - + $b = $a->acos(); $expected = Matrix::quick([ diff --git a/tests/VectorTest.php b/tests/VectorTest.php index f17ef5f..4237335 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -147,7 +147,7 @@ public function linspace() : void * @dataProvider shapeProvider * * @param \Tensor\Vector $vector - * @param array $expected + * @param array $expected */ public function shape(Vector $vector, array $expected) : void { @@ -545,7 +545,7 @@ public function addProvider() : Generator yield [ Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]), - 10.0, + 10.0, Vector::quick([-5.0, 35.0, 45.0, -26.0, -62.0, 99.0, 116.0, 55.0]), ]; } @@ -913,7 +913,7 @@ public function lessEqualProvider() : Generator [0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 1], ]), - + ]; yield [ @@ -982,7 +982,7 @@ public function abs() : void public function square() : void { $a = Vector::quick([-15.0, 25.0, 35.0, -36.0, -72.0, 89.0, 106.0, 45.0]); - + $b = $a->square(); $expected = Vector::quick([225, 625, 1225, 1296, 5184, 7921, 11236, 2025]); From 9cf46d8c1090c49df8fdbf2978d0c8c4eddf5002 Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Tue, 19 Sep 2023 00:11:59 -0500 Subject: [PATCH 08/10] Fix CI badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e8191a5..44cb26b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Tensor: Scientific Computing for PHP -[![PHP from Packagist](https://img.shields.io/packagist/php-v/rubix/tensor.svg?style=flat&colorB=8892BF)](https://www.php.net/) [![Latest Stable Version](https://img.shields.io/packagist/v/rubix/tensor.svg?style=flat&colorB=orange)](https://packagist.org/packages/rubix/tensor) [![PHP Build](https://github.com/RubixML/Tensor/workflows/Build/badge.svg)](https://github.com/RubixML/Tensor/actions) [![Extension Build](https://github.com/RubixML/Tensor/actions/workflows/ci-ext.yml/badge.svg)](https://github.com/RubixML/Tensor/actions/workflows/ci-ext.yml) [![Downloads from Packagist](https://img.shields.io/packagist/dt/rubix/tensor.svg?style=flat&colorB=red)](https://packagist.org/packages/rubix/tensor) [![GitHub](https://img.shields.io/github/license/RubixML/Tensor)](https://github.com/RubixML/Tensor/blob/master/LICENSE.md) +[![PHP from Packagist](https://img.shields.io/packagist/php-v/rubix/tensor.svg?style=flat&colorB=8892BF)](https://www.php.net/) [![Latest Stable Version](https://img.shields.io/packagist/v/rubix/tensor.svg?style=flat&colorB=orange)](https://packagist.org/packages/rubix/tensor) [![Code Checks](https://github.com/RubixML/Tensor/actions/workflows/ci.yml/badge.svg)](https://github.com/RubixML/Tensor/actions/workflows/ci.yml) [![Extension Build](https://github.com/RubixML/Tensor/actions/workflows/ci-ext.yml/badge.svg)](https://github.com/RubixML/Tensor/actions/workflows/ci-ext.yml) [![Downloads from Packagist](https://img.shields.io/packagist/dt/rubix/tensor.svg?style=flat&colorB=red)](https://packagist.org/packages/rubix/tensor) [![GitHub](https://img.shields.io/github/license/RubixML/Tensor)](https://github.com/RubixML/Tensor/blob/master/LICENSE.md) A library and extension that provides objects for scientific computing in [PHP](https://php.net). ## Installation From 1a8dc9872250309ca02c79acd75e13ec1aef287f Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Tue, 19 Sep 2023 00:19:09 -0500 Subject: [PATCH 09/10] Fix bug in polyfill --- config.json | 2 +- ext/config.m4 | 2 +- src/Vector.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config.json b/config.json index 4d6ce22..9af2a2c 100644 --- a/config.json +++ b/config.json @@ -6,7 +6,7 @@ "author": "The Rubix ML Community", "version": "3.0.3", "verbose": true, - "extra-cflags": "-O3 -ffast-math", + "extra-cflags": "-O3", "extra-libs": "-lopenblas -llapacke -lgfortran", "extra-sources": [ "include/arithmetic.c", diff --git a/ext/config.m4 b/ext/config.m4 index 4f8a6e2..40d2d4b 100644 --- a/ext/config.m4 +++ b/ext/config.m4 @@ -35,7 +35,7 @@ if test "$PHP_TENSOR" = "yes"; then include/linear_algebra.c include/signal_processing.c include/settings.c" - PHP_NEW_EXTENSION(tensor, $tensor_sources, $ext_shared,, -O3 -ffast-math) + PHP_NEW_EXTENSION(tensor, $tensor_sources, $ext_shared,, -O3) PHP_ADD_BUILD_DIR([$ext_builddir/kernel/]) for dir in "tensor tensor/decompositions tensor/exceptions tensor/reductions"; do PHP_ADD_BUILD_DIR([$ext_builddir/$dir]) diff --git a/src/Vector.php b/src/Vector.php index cc54b75..773d520 100644 --- a/src/Vector.php +++ b/src/Vector.php @@ -1768,7 +1768,7 @@ public function lessEqualMatrix(Matrix $b) : Matrix $rowC = []; foreach ($this->a as $j => $valueA) { - $rowC[] = $valueA < $rowB[$j] ? 1 : 0; + $rowC[] = $valueA <= $rowB[$j] ? 1 : 0; } $c[] = $rowC; From f3895a4dcc7f4de37a2bcc83024b0df65a93e7be Mon Sep 17 00:00:00 2001 From: Andrew DalPino Date: Tue, 19 Sep 2023 15:32:50 -0500 Subject: [PATCH 10/10] Allow some error --- tests/MatrixTest.php | 8 ++++---- tests/VectorTest.php | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php index 3eede24..4fbc7b0 100644 --- a/tests/MatrixTest.php +++ b/tests/MatrixTest.php @@ -2042,7 +2042,7 @@ public function sin() : void [0.4121184852417566], ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -2064,7 +2064,7 @@ public function asin() : void [0.010000166674167114], ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -2086,7 +2086,7 @@ public function cos() : void [-0.9111302618846769], ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -2130,7 +2130,7 @@ public function tan() : void [-0.45231565944180985], ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** diff --git a/tests/VectorTest.php b/tests/VectorTest.php index 4237335..52dd7f0 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -1069,7 +1069,7 @@ public function sin() : void 0.9129452507276277, 0.5155013718214642, -0.6181371122370333, ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1085,7 +1085,7 @@ public function asin() : void 0.1001674211615598, 0.3046926540153975, -0.5235987755982989, ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1102,7 +1102,7 @@ public function cos() : void 0.40808206181339196, -0.8568887533689473, 0.7860702961410393, ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1118,7 +1118,7 @@ public function acos() : void 1.4706289056333368, 1.2661036727794992, 2.0943951023931957, ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /** @@ -1152,7 +1152,7 @@ public function atan() : void 1.5208379310729538, 1.2036224929766774, 1.486959684726482, ]); - $this->assertEquals($expected, $b); + $this->assertEqualsWithDelta($expected, $b, 1e-8); } /**