mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
adjust dot product
This commit is contained in:
parent
b7c9453621
commit
5f8004da25
|
|
@ -84,17 +84,6 @@ final class Algebra
|
|||
$temp += $value1[$i][$c] * $value2[$c];
|
||||
}
|
||||
|
||||
$result[$i] = $temp;
|
||||
}
|
||||
} elseif (!$isMatrix1 && $isMatrix2) {
|
||||
$result = [];
|
||||
for ($i = 0; $i < $m1; ++$i) { // Row of 1
|
||||
$temp = 0;
|
||||
|
||||
for ($c = 0; $c < $m2; ++$c) { // Row of 2
|
||||
$temp += $value2[$i][$c] * $value1[$c];
|
||||
}
|
||||
|
||||
$result[$i] = $temp;
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -699,48 +699,6 @@ class Matrix implements \ArrayAccess, \Iterator
|
|||
return $L->det();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dot product
|
||||
*
|
||||
* @param self $B Matrix
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function dot(self $B) : self
|
||||
{
|
||||
$value1 = $this->matrix;
|
||||
$value2 = $B->toArray();
|
||||
|
||||
$m1 = \count($value1);
|
||||
$n1 = \count($value1[0]);
|
||||
|
||||
$m2 = \count($value2);
|
||||
$n2 = \count($value2[0]);
|
||||
|
||||
$result = null;
|
||||
|
||||
if ($m2 !== $n1) {
|
||||
throw new InvalidDimensionException($m2 . 'x' . $n2 . ' not compatible with ' . $m1 . 'x' . $n1);
|
||||
}
|
||||
|
||||
$result = [[]];
|
||||
for ($i = 0; $i < $m1; ++$i) { // Row of 1
|
||||
for ($c = 0; $c < $n2; ++$c) { // Column of 2
|
||||
$temp = 0;
|
||||
|
||||
for ($j = 0; $j < $m2; ++$j) { // Row of 2
|
||||
$temp += $value1[$i][$j] * $value2[$j][$c];
|
||||
}
|
||||
|
||||
$result[$i][$c] = $temp;
|
||||
}
|
||||
}
|
||||
|
||||
return self::fromArray($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sum the elements in the matrix.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -493,81 +493,6 @@ final class MatrixTest extends \PHPUnit\Framework\TestCase
|
|||
$A->mult($B);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers phpOMS\Math\Matrix\Matrix
|
||||
* @group framework
|
||||
*/
|
||||
public function testDotVectors() : void
|
||||
{
|
||||
$v1 = Vector::fromArray([1, 3, -5])->transpose();
|
||||
|
||||
self::assertEquals(
|
||||
[[3]],
|
||||
$v1->dot(Vector::fromArray([4, -2, -1]))->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers phpOMS\Math\Matrix\Matrix
|
||||
* @group framework
|
||||
*/
|
||||
public function testDotMatrices() : void
|
||||
{
|
||||
$m = Matrix::fromArray([
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]);
|
||||
|
||||
self::assertEquals(
|
||||
[
|
||||
[58, 64],
|
||||
[139, 154],
|
||||
],
|
||||
$m->dot(
|
||||
Matrix::fromArray([
|
||||
[7, 8],
|
||||
[9, 10],
|
||||
[11, 12],
|
||||
])
|
||||
)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers phpOMS\Math\Matrix\Matrix
|
||||
* @group framework
|
||||
*/
|
||||
public function testDotVectorMatrix() : void
|
||||
{
|
||||
$v = Vector::fromArray([3, 4])->transpose();
|
||||
|
||||
self::assertEquals(
|
||||
[11, 39, 53],
|
||||
$v->dot(
|
||||
Matrix::fromArray([
|
||||
[1, 5, 7],
|
||||
[2, 6, 8],
|
||||
])
|
||||
)->toVectorArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function testDotMatrixVector() : void
|
||||
{
|
||||
$m = Matrix::fromArray([
|
||||
[1, 2],
|
||||
[5, 6],
|
||||
[7, 8],
|
||||
]);
|
||||
|
||||
self::assertEquals(
|
||||
[11, 39, 53],
|
||||
$m->dot(
|
||||
Vector::fromArray([3, 4])
|
||||
)->toVectorArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function testSumAll() : void
|
||||
{
|
||||
$m = Matrix::fromArray([
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
<p>[video src="<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley">https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley</a>"]</p>
|
||||
Loading…
Reference in New Issue
Block a user