Skip to content

Piecewise functions and domain restrictions

Braces hold a list of condition: value cases. The same syntax restricts a curve’s domain when the braces hold conditions and nothing else.

examples/03-piecewise.axis

"Piecewise and restrictions"
// One case per branch, read left to right.
jump(x) = {x < 0: -1, x > 0: 1} @ color: RED, lineWidth: 3
// A final value with no condition is the fallback.
clamp(x) = {x < 0: 0, x > 1: 1, x} @ color: BLUE, lineWidth: 3
// Chained comparisons work inside a case.
pulse(x) = {-1 <= x <= 1: 1, 0} @ color: GREEN, lineWidth: 3
// A brace group holding only conditions restricts the domain instead.
y = x ^ 2 {0 < x < 3} @ color: PURPLE, lineWidth: 3
// Several restrictions are comma separated.
y = sin(x) {x > 0, x < 2pi} @ color: ORANGE, lineWidth: 3
// A long piecewise reads better split over lines - inside the braces a newline
// ends nothing, so the cases may be laid out however reads best.
wave(x) = {
x < -pi: 0,
-pi <= x <= pi: sin(x),
x > pi: 0
} @ color: RED, lineWidth: 3
folder "Absolute value, three ways" {
y = abs(x) @ color: RED
y = {x < 0: -x, x} @ color: BLUE, lineStyle: DASHED
y = sqrt(x ^ 2) @ color: GREEN, lineStyle: DOTTED
}
Open in playground →