public __construct ( array $intervals, array $functions ) | ||
$intervals | array | Array of intervals Example: [[-10, 0, false, true], [0, 2], [3, 10]] |
$functions | array | Array of callback functions |
public function __construct(array $intervals, array $functions)
{
if (count($intervals) !== count($functions)) {
throw new Exception\BadDataException("For a piecewise function you must provide the same number of intervals as functions.");
}
if (count(array_filter($functions, "is_callable")) !== count($intervals)) {
throw new Exception\BadDataException("Not every function provided is valid. Ensure that each function is callable.");
}
$unsortedIntervals = $intervals;
// Sort intervals such that start of intervals is increasing
usort($intervals, function ($a, $b) {
return $a[0] <=> $b[0];
});
foreach ($intervals as $interval) {
// Store values from previous interval
$lastA = $a ?? -INF;
$lastB = $b ?? -INF;
$lastBOpen = $bOpen ?? false;
if (count(array_filter($interval, "is_numeric")) !== 2) {
throw new Exception\BadDataException("Each interval must contain two numbers.");
}
// Fetch values from current interval
$a = $interval[0];
$b = $interval[1];
$aOpen = $interval[2] ?? false;
$bOpen = $interval[3] ?? false;
if ($a === $b and ($aOpen or $bOpen)) {
throw new Exception\BadDataException("Your interval [{$a}, {$b}] is a point and thus needs to be closed at both ends");
}
if ($a > $b) {
throw new Exception\BadDataException("Interval must be increasing. Try again using [{$b}, {$a}] instead of [{$a}, {$b}]");
}
if ($a === $lastB and !$aOpen and !$lastBOpen) {
throw new Exception\BadDataException("The intervals [{$lastA}, {$lastB}] and [{$a}, {$b}] share a point, but both intervals are also closed at that point. For intervals to share a point, one or both sides of that point must be open.");
}
if ($a < $lastB) {
throw new Exception\BadDataException("The intervals [{$lastA}, {$lastB}] and [{$a}, {$b}] overlap. The subintervals of a piecewise functions cannot overlap.");
}
}
$this->intervals = $unsortedIntervals;
$this->functions = $functions;
}