phpOMS/Math/Numerics/Interpolation/LagrangeInterpolation.php
2021-01-31 16:16:50 +00:00

68 lines
1.5 KiB
PHP

<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package phpOMS\Math\Numerics\Interpolation
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Math\Numerics\Interpolation;
/**
* Lagrange spline interpolation.
*
* @package phpOMS\Math\Numerics\Interpolation
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class LagrangeInterpolation implements InterpolationInterface
{
/**
* Points for spline interpolation
*
* @var array<int, array{x:int|float, y:int|float}>
* @since 1.0.0
*/
private array $points = [];
/**
* Constructor.
*
* @param array<int, array{x:int|float, y:int|float}> $points Points to create the interpolation with
*
* @since 1.0.0
*/
public function __construct(array $points) {
$this->points = $points;
}
/**
* {@inheritdoc}
*/
public function interpolate(int | float $x) : float
{
$n = \count($this->points);
$result = 0.0;
for ($i = 0; $i < $n; ++$i) {
$solve = $this->points[$i]['y'];
for ($j = 0; $j < $n; ++$j) {
if ($j !== $i) {
$solve *= ($x - $this->points[$j]['x']) / ($this->points[$i]['x'] - $this->points[$j]['x']);
}
}
$result += $solve;
}
return $result;
}
}