mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-24 15:48:24 +00:00
add scalar product function
This commit is contained in:
parent
9330785a6f
commit
34281e40ee
25
src/Phpml/Math/Product.php
Normal file
25
src/Phpml/Math/Product.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Phpml\Math;
|
||||
|
||||
class Product
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $a
|
||||
* @param array $b
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function scalar(array $a, array $b)
|
||||
{
|
||||
$product = 0;
|
||||
foreach ($a as $index => $value) {
|
||||
$product += $value * $b[$index];
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
}
|
20
tests/Phpml/Math/ProductTest.php
Normal file
20
tests/Phpml/Math/ProductTest.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\Math;
|
||||
|
||||
use Phpml\Math\Product;
|
||||
|
||||
class ProductTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testScalarProduct()
|
||||
{
|
||||
$product = new Product();
|
||||
|
||||
$this->assertEquals(10, $product->scalar([2, 3], [-1, 4]));
|
||||
$this->assertEquals(-0.1, $product->scalar([1, 4, 1], [-2, 0.5, -0.1]));
|
||||
$this->assertEquals(8, $product->scalar([2], [4]));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user