From 1b738dfe5abf7ea8fb7a4314145640a28a83b573 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 23 Oct 2023 23:10:36 +0000 Subject: [PATCH] fix tests --- Math/Functions/Algebra.php | 43 +++++++++++++++++++- Math/Matrix/Vector.php | 59 ++++++++++++++++++++++++++++ tests/Math/Functions/AlgebraTest.php | 16 ++++---- 3 files changed, 108 insertions(+), 10 deletions(-) diff --git a/Math/Functions/Algebra.php b/Math/Functions/Algebra.php index 83245672d..05c7ced27 100644 --- a/Math/Functions/Algebra.php +++ b/Math/Functions/Algebra.php @@ -27,7 +27,7 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException; final class Algebra { /** - * Get the dot product of two arrays + * Get the product of two arrays * * @param array $value1 Value 1 is a matrix or a vector * @param array $value2 Value 2 is a matrix or vector (cannot be a matrix if value1 is a vector) @@ -39,7 +39,7 @@ final class Algebra * * @since 1.0.0 */ - public static function dot(array $value1, array $value2) : int|float|array + public static function mult(array $value1, array $value2) : int|float|array { $m1 = \count($value1); $n1 = ($isMatrix1 = \is_array($value1[0])) ? \count($value1[0]) : 1; @@ -47,6 +47,14 @@ final class Algebra $m2 = \count($value2); $n2 = ($isMatrix2 = \is_array($value2[0])) ? \count($value2[0]) : 1; + if (!$isMatrix1 && $isMatrix2) { + $m1 = \count($value1); + $n1 = ($isMatrix1 = \is_array($value1[0])) ? \count($value1[0]) : 1; + + $m2 = \count($value2); + $n2 = ($isMatrix2 = \is_array($value2[0])) ? \count($value2[0]) : 1; + } + $result = null; if ($isMatrix1 && $isMatrix2) { @@ -93,6 +101,37 @@ final class Algebra return $result; } + /** + * Calculate the eucledian dot product + * + * @param array $value1 Vector 1 + * @param array $value2 Vector 2 + * + * @return float + * + * @since 1.0.0 + */ + public function dot(array $value1, array $value2) : float + { + $length = \count($value1); + $m1 = 0; + $m2 = 0; + $prod = 0; + + for ($i = 0; $i < $length; ++$i) { + $m1 += $value1[$i] * $value1[$i]; + $m2 += $value2[$i] * $value2[$i]; + $prod += $value1[$i] * $value2[$i]; + } + + $m1 = \sqrt($m1); + $m2 = \sqrt($m2); + + $cos = $prod / ($m1 * $m2); + + return $m1 * $m2 * $cos; + } + /** * Calculate the vector corss product * diff --git a/Math/Matrix/Vector.php b/Math/Matrix/Vector.php index 38e01d164..87a6045ef 100755 --- a/Math/Matrix/Vector.php +++ b/Math/Matrix/Vector.php @@ -123,6 +123,65 @@ final class Vector extends Matrix return $dotProduct / ($magnitude1 * $magnitude2); } + + /** + * Calculate the eucledian dot product + * + * @param selft $vector Vector + * + * @return float + * + * @since 1.0.0 + */ + public function dot(self $vector) : float + { + $length = $this->m; + $m1 = 0; + $m2 = 0; + $prod = 0; + + for ($i = 0; $i < $length; ++$i) { + $m1 += $this->matrix[$i][0] * $this->matrix[$i][0]; + $m2 += $vector->matrix[$i][0] * $vector->matrix[$i][0]; + $prod += $this->matrix[$i][0] * $vector->matrix[$i][0]; + } + + $m1 = \sqrt($m1); + $m2 = \sqrt($m2); + + $cos = $prod / ($m1 * $m2); + + return $m1 * $m2 * $cos; + } + + /** + * Calculate the angle between two vectors + * + * @param self $vector Vector + * + * @return float + * + * @since 1.0.0 + */ + public function angle(self $vector) : float + { + $length = $this->m; + $m1 = 0; + $m2 = 0; + $prod = 0; + + for ($i = 0; $i < $length; ++$i) { + $m1 += $this->matrix[$i][0] * $this->matrix[$i][0]; + $m2 += $vector->matrix[$i][0] * $vector->matrix[$i][0]; + $prod += $this->matrix[$i][0] * $vector->matrix[$i][0]; + } + + $m1 = \sqrt($m1); + $m2 = \sqrt($m2); + + return \acos($prod / ($m1 * $m2)); + } + /** * Calculate the cross product * diff --git a/tests/Math/Functions/AlgebraTest.php b/tests/Math/Functions/AlgebraTest.php index a276205b8..7455ea3fe 100644 --- a/tests/Math/Functions/AlgebraTest.php +++ b/tests/Math/Functions/AlgebraTest.php @@ -23,22 +23,22 @@ use phpOMS\Math\Functions\Algebra; */ final class AlgebraTest extends \PHPUnit\Framework\TestCase { - public function testDotVectors() : void + public function testMultVectors() : void { self::assertEquals( 3, - Algebra::dot([1, 3, -5], [4, -2, -1]) + Algebra::mult([1, 3, -5], [4, -2, -1]) ); } - public function testDotMatrices() : void + public function testMultMatrices() : void { self::assertEquals( [ [58, 64], [139, 154], ], - Algebra::dot( + Algebra::mult( [ [1, 2, 3], [4, 5, 6], @@ -52,11 +52,11 @@ final class AlgebraTest extends \PHPUnit\Framework\TestCase ); } - public function testDotVectorMatrix() : void + public function testMultVectorMatrix() : void { self::assertEquals( [11, 39, 53], - Algebra::dot( + Algebra::mult( [3, 4], [ [1, 5, 7], @@ -66,11 +66,11 @@ final class AlgebraTest extends \PHPUnit\Framework\TestCase ); } - public function testDotMatrixVector() : void + public function testMultMatrixVector() : void { self::assertEquals( [11, 39, 53], - Algebra::dot( + Algebra::mult( [ [1, 2], [5, 6],