add scalar product function

This commit is contained in:
Arkadiusz Kondas 2016-04-21 00:23:03 +02:00
parent 9330785a6f
commit 34281e40ee
2 changed files with 45 additions and 0 deletions

View 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;
}
}

View 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]));
}
}