Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| KernelsND | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| gaussianKernel | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Math\Topology |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Math\Topology; |
| 16 | |
| 17 | use phpOMS\Math\Matrix\IdentityMatrix; |
| 18 | use phpOMS\Math\Matrix\Matrix; |
| 19 | |
| 20 | /** |
| 21 | * Kernels. |
| 22 | * |
| 23 | * @package phpOMS\Math\Topology |
| 24 | * @license OMS License 2.0 |
| 25 | * @link https://jingga.app |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | final class KernelsND |
| 29 | { |
| 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @codeCoverageIgnore |
| 35 | */ |
| 36 | private function __construct() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Gaussian kernel |
| 42 | * |
| 43 | * @param array<float|int> $distances Distances |
| 44 | * @param array<float|int> $bandwidths Bandwidths |
| 45 | * |
| 46 | * @return array |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public static function gaussianKernel(array $distances, array $bandwidths) : array |
| 51 | { |
| 52 | $dim = \count($bandwidths); |
| 53 | |
| 54 | $bandwithMatrix = Matrix::fromArray($bandwidths); |
| 55 | $distnaceMatrix = Matrix::fromArray($distances); |
| 56 | $identityMatrix = new IdentityMatrix($dim); |
| 57 | |
| 58 | $cov = $bandwithMatrix->mult($identityMatrix); |
| 59 | |
| 60 | /** @phpstan-ignore-next-line */ |
| 61 | $exponent = $distnaceMatrix->dot($cov->inverse())->mult($distnaceMatrix)->sum(1)->mult(-0.5); |
| 62 | |
| 63 | return $exponent->exp()->mult((1 / \pow(2 * \M_PI, $dim / 2)) * \pow($cov->det(), 0.5))->matrix; |
| 64 | } |
| 65 | } |