MathPHP\Functions\Polynomial::__invoke PHP Метод

__invoke() публичный Метод

When a polynomial is being evaluated at a point x₀, build a callback function and return the value of the callback function at x₀ Example: $polynomial = new Polynomial([1, -8, 12, 3]); echo $polynomial(4); prints -13
public __invoke ( number $x₀ ) : float
$x₀ number The value at which we are evaluting our polynomial
Результат float The result of our polynomial evaluated at $x₀
    public function __invoke($x₀) : float
    {
        // Set object parameters as local variables so they can be used with the use function
        $degree = $this->degree;
        $coefficients = $this->coefficients;
        // Start with the zero polynomial
        $polynomial = function ($x) {
            return 0;
        };
        // Iterate over each coefficient to create a callback function for each term
        for ($i = 0; $i < $degree + 1; $i++) {
            // Create a callback function for the current term
            $term = function ($x) use($degree, $coefficients, $i) {
                return $coefficients[$i] * $x ** ($degree - $i);
            };
            // Add the new term to the polynomial
            $polynomial = Arithmetic::add($polynomial, $term);
        }
        return $polynomial($x₀);
    }