mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
fix tests
This commit is contained in:
parent
45bab0c041
commit
1b738dfe5a
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user