Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Phpml\Math\Kernel;
6
 
7
use Phpml\Exception\InvalidArgumentException;
8
use Phpml\Math\Kernel;
9
use Phpml\Math\Product;
10
 
11
class RBF implements Kernel
12
{
13
    /**
14
     * @var float
15
     */
16
    private $gamma;
17
 
18
    public function __construct(float $gamma)
19
    {
20
        $this->gamma = $gamma;
21
    }
22
 
23
    public function compute($a, $b): float
24
    {
25
        if (!is_array($a) || !is_array($b)) {
26
            throw new InvalidArgumentException(sprintf('Arguments of %s must be arrays', __METHOD__));
27
        }
28
 
29
        $score = 2 * Product::scalar($a, $b);
30
        $squares = Product::scalar($a, $a) + Product::scalar($b, $b);
31
 
32
        return exp(-$this->gamma * ($squares - $score));
33
    }
34
}