NevillesMethod.php 3.22 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
<?php
namespace MathPHP\NumericalAnalysis\Interpolation;

use MathPHP\Functions\Polynomial;

/**
 * Nevilles Method
 *
 * In numerical analysis, Nevilles Method is an interpolation technique that
 * uses Lagrange polynomials of lower powers recursively in order to compute
 * Lagrange polynomials of higher powers.
 *
 * Nevilles Method belongs to a collection of techniques that interpolate a
 * function or a set of values to approximate a function at a target point.
 * We can either directly supply a set of inputs and their corresponding outputs
 * for said function, or if we explicitly know the function, we can define it as
 * a callback function and then generate a set of points by evaluating that
 * function at n points between a start and end point. We then use these values
 * to interpolate Lagrange polynomials recursively at our target point.
 *
 * http://www2.math.ou.edu/~npetrov/neville.pdf
 */
class NevillesMethod extends Interpolation
{
    /**
     * Interpolate
     *
     * @param number         $target  The point at which we are interpolation
     * @param callable|array $source  The source of our approximation. Should be either
     *                                a callback function or a set of arrays. Each array
     *                                (point) contains precisely two numbers, an x and y.
     *                                Example array: [[1,2], [2,3], [3,4]].
     *                                Example callback: function($x) {return $x**2;}
     * @param number   ... $args The arguments of our callback function: start,
     *                           end, and n. Example: approximate($source, 0, 8, 5).
     *                           If $source is a set of points, do not input any
     *                           $args. Example: approximate($source).
     *
     * @return number            The interpolated value at our target
     */
    public static function interpolate($target, $source, ... $args)
    {
        // get an array of points from our $source argument
        $points = self::getPoints($source, $args);

        // Validate input and sort points
        self::validate($points, $degree = 2);
        $sorted = self::sort($points);

        // Descriptive constants
        $x = self::X;
        $y = self::Y;

        // Initialize
        $n   = count($sorted);
        $Q   = [];

        // Build our 0th-degree Lagrange polynomials: Q₍ᵢ₎₍₀₎ = yᵢ for all i < n
        for ($i = 0; $i < $n; $i++) {
            $Q[$i][0] = new Polynomial([$sorted[$i][$y]]); // yᵢ
        }

        // Recursively generate our (n-1)th-degree Lagrange polynomial at $target
        for ($i = 1; $i < $n; $i++) {
            for ($j = 1; $j <= $i; $j++) {
                $xᵢ        = $sorted[$i - $j][$x];
                $xᵢ          = $sorted[$i][$x];
                $Q₎₍₋₁₎   = $Q[$i][$j-1]($target);
                $Q₋₁₎₍₋₁₎ = $Q[$i-1][$j-1]($target);
                $Q[$i][$j]   = LagrangePolynomial::interpolate([[$xᵢ,$Q₋₁₎₍₋₁₎],[$xᵢ,$Q₎₍₋₁₎]]);
            }
        }

        // Return our (n-1)th-degree Lagrange polynomial evaluated at $target
        return $Q[$n-1][$n-1]($target);
    }
}