MathPHP\Functions\Piecewise::getFunction PHP Метод

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

Find which subinterval our input value is contained within, and then return the function that corresponds to that subinterval. If no subinterval is found such that our input is contained within it, a false is returned.
public getFunction ( number $x ) : mixed
$x number The value at which we are searching for a subinterval that contains it, and thus has a corresponding function.
Результат mixed Returns the function that contains $x in its domain, or false
    public function getFunction($x)
    {
        foreach ($this->intervals as $i => $interval) {
            $a = $interval[0];
            $b = $interval[1];
            $aOpen = $interval[2] ?? false;
            $bOpen = $interval[3] ?? false;
            // Four permutations: open-open, open-closed, closed-open, closed-closed
            if ($aOpen && $bOpen) {
                if ($x > $a && $x < $b) {
                    return $this->functions[$i];
                }
            } elseif ($aOpen && !$bOpen) {
                if ($x > $a && $x <= $b) {
                    return $this->functions[$i];
                }
            } elseif (!$aOpen && $bOpen) {
                if ($x >= $a && $x < $b) {
                    return $this->functions[$i];
                }
            } elseif (!$aOpen && !$bOpen) {
                if ($x >= $a && $x <= $b) {
                    return $this->functions[$i];
                }
            }
        }
        return false;
    }