Topic

Piecewise functions

A piecewise function is a list of pairs: a region and a rule. The first region that contains x decides the rule. Regions use a betweenness chain on x:

regionmeaning
2:x:82 ≤ x ≤ 8
_:x:8    2:x:_x ≤ 8    x ≥ 2 (_ is an unbounded side)
2:!x:8    2:x:!82 < x ≤ 8    2 ≤ x < 8 (:! is a strict link)
2n :x:! 2n+12n ≤ x < 2n+1 for some integer n
_:x:!-1 || 1:!x:_a union: x < −1 or x > 1
all, else, otherwiseevery x

A name in a bound that is not a parameter is an integer variable. Bounds must be affine in it (like 2n + 1 or 3 − n/2). For each x the library solves for the integer n that fits, and the rule can use n too: x - n if n:x:!n+1 is the sawtooth. A region can also be a plain function x => boolean; it answers point questions, but ranges, integrals and zeros need breakpoints, which only chains provide.

fn("x^2 if _:x:1; 3 - x if 1:!x:2; 1/2 otherwise");
piecewise([["2n:x:!2n+1", "1"], ["2n+1:x:!2n+2", "-1"]]);   // a square wave
where`${a}:!x:${b}`;                                          // a Region from a template

Breaks

breaks([a, b]) lists each breakpoint with the value there (if any piece contains it), the limits from the left and right rules, and a kind. continuous means all three are proven equal. jump means the two limits are proven different. removable means the limits agree but the value is missing or different. That is what a plot needs to draw filled and open dots.

Worked example: breaks

const f = fn("x^2 if _:x:1; 3 - x if 1:!x:2; 1/2 otherwise");
f.breaks([-1, 3]).exact();
atvalueleftrightkind
11 (from x², since _:x:1 is closed)12jump
21 (from 3 − x, since 1:!x:2 is closed at 2)11/2jump

Each limit is the rule of the neighbouring piece evaluated at the breakpoint, enclosed as an interval. A jump is proven when the left and right enclosures are disjoint; continuity is proven when all three are exact and equal; edge marks a break with a piece on only one side. When the enclosures overlap without being equal (a limit like sin(π)/π that is 0 but is only known to lie in a tiny interval) the kind is unproven, never a guess.

How the integer variable is solved

For a chain such as 2n : x :! 2n+1 the bounds are affine in n, so each inequality is a bound on n: 2n ≤ x gives n ≤ x/2, and x < 2n + 1 gives n > (x − 1)/2. The library takes the floor and ceiling of those rational bounds and checks the integers between them, so it never searches. breakpoints(a, b) works the same way in reverse: it lists the integers n whose bounds 2n and 2n + 1 fall in [a, b], which is how ranges, integrals and zeros know where to split.

Exact answers follow. The sawtooth x - n if n:x:!n+1 has integral 1/2 + 1/2 + 1/8 = 9/8 from 0 to 5/2, and the library returns the point interval 9/8. The square wave above has integral 1 − 1 + 1 − 1/2 = 1/2 from 0 to 7/2, also exactly.

Questions piece by piece

Values, ranges, integrals, zeros, extrema and inflections are answered on each open piece with that piece's rule, then combined. A sign change across a jump is not a zero. The zero finder only looks inside pieces and at points where the value is exactly 0.

const g = fn("x^2 - 2 if _:x:!1; x - 1/2 otherwise");
g.zeros([-3, 3]).exact();
// one record: sign-change in [−1970/1393, −1393/985], unique: the zero −√2.
// x − 1/2 is never 0 on x ≥ 1, and the drop from −1 to 1/2 at x = 1 is a jump, not a zero.

The range of a piecewise function is the hull of the ranges of its pieces and of the values at its breakpoints. A piece that is open at a breakpoint contributes its limit there to the outer bound, so the range of the sawtooth on [0, 3] comes back as [0, 1]: a true enclosure, even though the value 1 is approached and never taken.

Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs