Algosim documentation: piecewise


Denotes a piecewise expression.



If expr1, expr2, ..., is a sequence of expressions and cond1, cond2, ... a list of booleans, then piecewise([expr1, cond1[, expr2, cond2[, ...]]]) returns the value of the first exprk with its corresponding condk equal to true, or the null object if all condk are false. The conditions are evaluated in order, and only if a condition evaluates to true is its corresponding expression evaluated. Furthermore, as soon as this expression has been evaluated, it is returned; consequently, the remaining conditions and expressions will not be evaluated.

The constant otherwise (with value true) can be used for improved readability.


myabs ≔ x ↦ piecewise(x, x ≥ 0, −x, otherwise);
mysinc ≔ x ↦ piecewise(sin(x)/x, x ≠ 0, 1, otherwise);
f ≔ x ↦ piecewise(−1, x ≤ −π/2, sin(x), x < π/2, 1, otherwise);

See also