Skip to content

PiecewiseExpand ​

Rewrite Abs, Sign, Min, Max, UnitStep, Clip, UnitBox, UnitTriangle and, over a bounded variable, Floor, Ceil, Round, IntegerPart, FractionalPart, Mod, Quotient and the waves inside exprexpr into Piecewise, recursively. Provided by @enumeratio/analytic.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguagePiecewiseExpand!
PiecewiseExpand(expr)rewrite using whatever is already assumed (e.g. inside an enclosing Assuming).
PiecewiseExpand(expr, assumptions)assume assumptionsassumptions (scoped, as in Assuming) for the rewrite, then forget it.
Typed signatures
TypePackageApplies
(expression, expression?) -> expressionanalyticalways

Domain: Analytic

Details
  • Abs, Sign and Argument expand only once their argument is known real — on a genuinely complex value they are not piecewise-comparable, so (matching Wolfram's own PiecewiseExpand) an argument whose realness isn't established is left untouched rather than guessed at. RealNumbers as the assumption makes every variable real.
  • Min, Max, UnitStep, Clip, UnitBox and UnitTriangle need no assumption, as in Wolfram: they expand over any symbols. The branches, conditions and default follow Wolfram's: a condition between two unknowns is written x - y >= 0, one against a constant x <= 2, and a zero-valued branch is the default. What the assumptions decide of a condition is not written (UnitBox(x) on 0 < x < 2 is x <= 1/2), a branch they rule out goes, and when the branches cover what is left the last one is the default.
  • Floor, Ceil, Round, IntegerPart, FractionalPart, Mod and Quotient, and SquareWave, TriangleWave and SawtoothWave, expand over an argument bounded on both sides by an assumption, one branch per stretch. The bound is read as an interval: 0 < x < 3, but also x^2 < 3, 0 < 2 x < 5, a constant such as Pi, or x < 3 y with 0 < y < 1. The argument is a symbol, linear in one (Floor(2 x)), or over an interval where it is monotone a square or square root of one (Floor(x^2) is bounded by Sqrt(2), Sqrt(3)). A modulus, or the multiple in a two-argument Floor, is a positive number, a constant, or a symbol: Mod(k, m) with 0 <= k <= 3 m reads the bound as 0 <= k/m <= 3 and writes its conditions on k/m. The values are written as Wolfram's Together writes them, in its order. A span of 100 stretches or more is left alone, as Wolfram leaves it.
  • Rewrites recurse into subexpressions, so PiecewiseExpand(Abs(x) + 1, …) rewrites the Abs inside the sum. A sum, product or power of Piecewise values, or a Max, Min, Clip or step over them, is one Piecewise: a branch for each combination, values that agree sharing their conditions, a zero value (else the last in Wolfram's order) the default. Conditions that are bounds on one unknown merge into intervals (x <= 0 with x >= 0 is x == 0, and two intervals that do not touch stay apart as x > 0 || x < 0). A relation is not entered (Wolfram turns a Piecewise inside one into conditions).

Examples

Basic #

∣x∣→|x| \to a two-branch Piecewise, once xx is known real

live ↗
InPiecewiseExpand(∣x∣,x∈R)\mathrm{PiecewiseExpand}(\vert x\vert, x∈ℝ)OutPiecewise([[−x,x<0]],x)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-x, x\lt0\bigr\rbrack\bigr\rbrack, x)

sign⁡(x)\operatorname{sign}(x) expanded into its three branches

live ↗
InPiecewiseExpand(sgn(x),x∈R)\mathrm{PiecewiseExpand}(\mathrm{sgn}(x), x∈ℝ)OutPiecewise([[−1,x<0],[1,0<x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, x\lt0\bigr\rbrack, \bigl\lbrack1, 0\lt x\bigr\rbrack\bigr\rbrack, 0)

Clip⁡(x,{−1,1})\operatorname{Clip}(x,\{-1,1\}) expanded into its three branches

live ↗
InPiecewiseExpand(Clip(x,[−1,1]),x∈R)\mathrm{PiecewiseExpand}(\mathrm{Clip}(x, \bigl\lbrack-1, 1\bigr\rbrack), x∈ℝ)OutPiecewise([[−1,x<−1],[1,1<x]],x)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, x\lt-1\bigr\rbrack, \bigl\lbrack1, 1\lt x\bigr\rbrack\bigr\rbrack, x)

max⁡(x,y)\max(x,y) as a two-branch Piecewise

live ↗
InPiecewiseExpand(max⁡(x,y),x∈R∧y∈R)\mathrm{PiecewiseExpand}(\max(x, y), x∈ℝ∧y∈ℝ)OutPiecewise([[x,0≤x−y]],y)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, 0≤x-y\bigr\rbrack\bigr\rbrack, y)

A fractional modulus: the values over a common denominator, in Wolfram's order of them

live ↗
InPiecewiseExpand(x mod 32,0≤x≤4)\mathrm{PiecewiseExpand}(x\bmod\frac{3}{2}, 0≤x≤4)OutPiecewise([[x−3,3≤x],[x,x<3/2]],2x−32)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x-3, 3≤x\bigr\rbrack, \bigl\lbrack x, x\lt3/2\bigr\rbrack\bigr\rbrack, \frac{2x-3}{2})

Quotient by 3/23/2: a branch per multiple of it

live ↗
InPiecewiseExpand(Quotient(x,32),0≤x≤4)\mathrm{PiecewiseExpand}(\mathrm{Quotient}(x, \frac{3}{2}), 0≤x≤4)OutPiecewise([[1,3/2≤x∧x<3],[2,3≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 3/2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack2, 3≤x\bigr\rbrack\bigr\rbrack, 0)

A constant modulus keeps its multiples in the bounds

live ↗
InPiecewiseExpand(x mod π,0<x<10)\mathrm{PiecewiseExpand}(x\bmod π, 0\lt x\lt10)OutPiecewise([[x,x<π],[x−3π,3π≤x],[x−2π,2π≤x∧x<3π]],x−π)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, x\lt π\bigr\rbrack, \bigl\lbrack x-3π, 3π≤x\bigr\rbrack, \bigl\lbrack x-2π, 2π≤x∧x\lt3π\bigr\rbrack\bigr\rbrack, x-π)

A symbolic modulus: the conditions are on k/mk/m

live ↗
InPiecewiseExpand(k mod m,0≤k≤3m)\mathrm{PiecewiseExpand}(k\bmod m, 0≤k≤3m)OutPiecewise([[k,k/m<1],[k−3m,3≤k/m],[k−2m,2≤k/m∧k/m<3]],k−m)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack k, k/m\lt1\bigr\rbrack, \bigl\lbrack k-3m, 3≤k/m\bigr\rbrack, \bigl\lbrack k-2m, 2≤k/m∧k/m\lt3\bigr\rbrack\bigr\rbrack, k-m)

Quotient by a symbol, over −m≤k≤2m-m \le k \le 2m

live ↗
InPiecewiseExpand(Quotient(k,m),−m≤k≤2m)\mathrm{PiecewiseExpand}(\mathrm{Quotient}(k, m), -m≤k≤2m)OutPiecewise([[−1,k/m<0],[1,1≤k/m∧k/m<2],[2,2≤k/m]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, k/m\lt0\bigr\rbrack, \bigl\lbrack1, 1≤k/m∧k/m\lt2\bigr\rbrack, \bigl\lbrack2, 2≤k/m\bigr\rbrack\bigr\rbrack, 0)

The argument is solved for xx: ⌊2x⌋\lfloor 2x\rfloor steps at the halves

live ↗
InPiecewiseExpand(⌊2x⌋,0<x<2)\mathrm{PiecewiseExpand}(\lfloor2x\rfloor, 0\lt x\lt2)OutPiecewise([[1,1/2≤x∧x<1],[2,1≤x∧x<3/2],[3,3/2≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1/2≤x∧x\lt1\bigr\rbrack, \bigl\lbrack2, 1≤x∧x\lt3/2\bigr\rbrack, \bigl\lbrack3, 3/2≤x\bigr\rbrack\bigr\rbrack, 0)

A falling argument flips which end of each stretch the condition names

live ↗
InPiecewiseExpand(⌊−x⌋,0<x<3)\mathrm{PiecewiseExpand}(\lfloor-x\rfloor, 0\lt x\lt3)OutPiecewise([[−3,2<x],[−2,1<x∧x≤2]],−1)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-3, 2\lt x\bigr\rbrack, \bigl\lbrack-2, 1\lt x∧x≤2\bigr\rbrack\bigr\rbrack, -1)

The values carry their common factor out front, as Wolfram's Together writes them

live ↗
InPiecewiseExpand(FractionalPart(2x),0<x<2)\mathrm{PiecewiseExpand}(\mathrm{FractionalPart}(2x), 0\lt x\lt2)OutPiecewise([[2(x−1),1≤x∧x<3/2],[2x,x<1/2],[2x−3,3/2≤x]],2x−1)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack2(x-1), 1≤x∧x\lt3/2\bigr\rbrack, \bigl\lbrack2x, x\lt1/2\bigr\rbrack, \bigl\lbrack2x-3, 3/2≤x\bigr\rbrack\bigr\rbrack, 2x-1)

A triangle wave of 2x2x over half a unit: four stretches, each rising or falling

live ↗
InPiecewiseExpand(TriangleWave(2x),0≤x≤12)\mathrm{PiecewiseExpand}(\mathrm{TriangleWave}(2x), 0≤x≤\frac{1}{2})OutPiecewise([[8x,x<1/8],[4(2x−1),3/8≤x]],−2(4x−1))\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack8x, x\lt1/8\bigr\rbrack, \bigl\lbrack4(2x-1), 3/8≤x\bigr\rbrack\bigr\rbrack, -2(4x-1))

⌊x2⌋\lfloor x^2\rfloor: the stretches are bounded by square roots

live ↗
InPiecewiseExpand(⌊x2⌋,0<x<2)\mathrm{PiecewiseExpand}(\lfloor x^2\rfloor, 0\lt x\lt2)OutPiecewise([[1,1≤x∧x<21/2],[2,21/2≤x∧x<31/2],[3,31/2≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2^{1/2}\bigr\rbrack, \bigl\lbrack2, 2^{1/2}≤x∧x\lt3^{1/2}\bigr\rbrack, \bigl\lbrack3, 3^{1/2}≤x\bigr\rbrack\bigr\rbrack, 0)

⌊x⌋\lfloor\sqrt{x}\rfloor is compared as x\sqrt{x}, over its range

live ↗
InPiecewiseExpand(⌊x⌋,0<x<5)\mathrm{PiecewiseExpand}(\lfloor\sqrt{x}\rfloor, 0\lt x\lt5)OutPiecewise([[1,1≤x1/2∧x1/2<2],[2,2≤x1/2]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x^{1/2}∧x^{1/2}\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x^{1/2}\bigr\rbrack\bigr\rbrack, 0)

The bound x2<3x^2 < 3 is −3<x<3-\sqrt{3} < x < \sqrt{3}

live ↗
InPiecewiseExpand(⌊x⌋,x2<3)\mathrm{PiecewiseExpand}(\lfloor x\rfloor, x^2\lt3)OutPiecewise([[−2,x<−1],[−1,−1≤x∧x<0],[1,1≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-2, x\lt-1\bigr\rbrack, \bigl\lbrack-1, -1≤x∧x\lt0\bigr\rbrack, \bigl\lbrack1, 1≤x\bigr\rbrack\bigr\rbrack, 0)

x<3yx < 3y with 0<y<10 < y < 1 bounds xx above by 3

live ↗
InPiecewiseExpand(⌊x⌋,0<x∧x<3y∧0<y<1)\mathrm{PiecewiseExpand}(\lfloor x\rfloor, 0\lt x∧x\lt3y∧0\lt y\lt1)OutPiecewise([[1,1≤x∧x<2],[2,2≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x\bigr\rbrack\bigr\rbrack, 0)

A branch the assumption rules out goes, and the last takes the default

live ↗
InPiecewiseExpand(Clip(x,[0,1]),0<x<3)\mathrm{PiecewiseExpand}(\mathrm{Clip}(x, \bigl\lbrack0, 1\bigr\rbrack), 0\lt x\lt3)OutPiecewise([[1,1<x]],x)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1\lt x\bigr\rbrack\bigr\rbrack, x)

With x>3x > 3 the constant never wins, and 2<x2 < x needs no saying

live ↗
InPiecewiseExpand(max⁡(x,2,y),x>3)\mathrm{PiecewiseExpand}(\max(x, 2, y), x\gt3)OutPiecewise([[x,0≤x−y]],y)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, 0≤x-y\bigr\rbrack\bigr\rbrack, y)

The peak, where both branches give 1, goes to the branch left of it when it is right of 0

live ↗
InPiecewiseExpand(UnitTriangle(x−1))\mathrm{PiecewiseExpand}(\mathrm{UnitTriangle}(x-1))OutPiecewise([[2−x,1<x∧x≤2],[x,0≤x≤1]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack2-x, 1\lt x∧x≤2\bigr\rbrack, \bigl\lbrack x, 0≤x≤1\bigr\rbrack\bigr\rbrack, 0)

A sum of Piecewise values is one Piecewise: a point is x==0x == 0, and a zero value is the default

live ↗
InPiecewiseExpand(max⁡(x,0)+UnitStep(x))\mathrm{PiecewiseExpand}(\max(x, 0)+\mathrm{UnitStep}(x))OutPiecewise([[1,x=0],[x+1,0<x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, x=0\bigr\rbrack, \bigl\lbrack x+1, 0\lt x\bigr\rbrack\bigr\rbrack, 0)

With no zero value the last in order is the default

live ↗
InPiecewiseExpand(∣x∣+1,R)\mathrm{PiecewiseExpand}(\vert x\vert+1, ℝ)OutPiecewise([[1−x,x<0]],x+1)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1-x, x\lt0\bigr\rbrack\bigr\rbrack, x+1)

Branches with the same value share their conditions

live ↗
InPiecewiseExpand(∣x∣∣y∣,R)\mathrm{PiecewiseExpand}(\vert x\vert\vert y\vert, ℝ)OutPiecewise([[−(xy),x<0∧0≤y∨0≤x∧y<0]],xy)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-(xy), x\lt0∧0≤y∨0≤x∧y\lt0\bigr\rbrack\bigr\rbrack, xy)

Independent unknowns: the sum counts how many are non-negative

live ↗
InPiecewiseExpand(UnitStep(x)+UnitStep(y))\mathrm{PiecewiseExpand}(\mathrm{UnitStep}(x)+\mathrm{UnitStep}(y))OutPiecewise([[1,0≤x∧y<0∨x<0∧0≤y],[2,0≤x∧0≤y]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 0≤x∧y\lt0∨x\lt0∧0≤y\bigr\rbrack, \bigl\lbrack2, 0≤x∧0≤y\bigr\rbrack\bigr\rbrack, 0)
Scope #

Four arguments: a branch wins over the earlier ones strictly, over the later ones on a tie

live ↗
InPiecewiseExpand(max⁡(a,b,c,d))\mathrm{PiecewiseExpand}(\max(a, b, c, d))OutPiecewise([[a,0≤a−b∧0≤a−c∧0≤a−d],[b,a−b<0∧0≤b−c∧0≤b−d],[c,a−c<0∧b−c<0∧0≤c−d]],d)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack a, 0≤a-b∧0≤a-c∧0≤a-d\bigr\rbrack, \bigl\lbrack b, a-b\lt0∧0≤b-c∧0≤b-d\bigr\rbrack, \bigl\lbrack c, a-c\lt0∧b-c\lt0∧0≤c-d\bigr\rbrack\bigr\rbrack, d)

Floor on a bounded interval: a branch per integer, the zero stretch as the default

live ↗
InPiecewiseExpand(⌊x⌋,0<x<4)\mathrm{PiecewiseExpand}(\lfloor x\rfloor, 0\lt x\lt4)OutPiecewise([[1,1≤x∧x<2],[2,2≤x∧x<3],[3,3≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack3, 3≤x\bigr\rbrack\bigr\rbrack, 0)

PrimePi on an interval stays

live ↗
InPiecewiseExpand(PrimePi(x),0<x<6)\mathrm{PiecewiseExpand}(\mathrm{PrimePi}(x), 0\lt x\lt6)OutPiecewise([[1,2≤x∧x<3],[2,3≤x∧x<5],[3,5≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack2, 3≤x∧x\lt5\bigr\rbrack, \bigl\lbrack3, 5≤x\bigr\rbrack\bigr\rbrack, 0)

Inequalities with floor and round remain symbolic

live ↗
InPiecewiseExpand(0<⌊x+round(x2)⌋−x<4)\mathrm{PiecewiseExpand}(0\lt\lfloor x+\mathrm{round}(x^2)\rfloor-x\lt4)Out0<⌊x+round(x2)⌋−x<40\lt\lfloor x+\mathrm{round}(x^2)\rfloor-x\lt4

An enclosing Assuming bounds the symbol, as the second argument does

live ↗
InAssuming(0<x<3,PiecewiseExpand(⌊x⌋))\mathrm{Assuming}(0\lt x\lt3, \mathrm{PiecewiseExpand}(\lfloor x\rfloor))OutPiecewise([[1,1≤x∧x<2],[2,2≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x\bigr\rbrack\bigr\rbrack, 0)

A bound on each unknown by its square: the floor and the round together are one Piecewise

live ↗
InAssuming(x2<3,PiecewiseExpand(⌊x⌋+round(y),y2<2))\mathrm{Assuming}(x^2\lt3, \mathrm{PiecewiseExpand}(\lfloor x\rfloor+\mathrm{round}(y), y^2\lt2))OutPiecewise([[−3,x<−1∧y<−1/2],[−2,x<−1∧−1/2≤y≤1/2∨−1≤x∧x<0∧y<−1/2],[−1,x<−1∧1/2<y∨−1≤x∧x<0∧−1/2≤y≤1/2∨0≤x∧x<1∧y<−1/2],[1,1≤x∧−1/2≤y≤1/2∨0≤x∧x<1∧1/2<y],[2,1≤x∧1/2<y]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-3, x\lt-1∧y\lt-1/2\bigr\rbrack, \bigl\lbrack-2, x\lt-1∧-1/2≤y≤1/2∨-1≤x∧x\lt0∧y\lt-1/2\bigr\rbrack, \bigl\lbrack-1, x\lt-1∧1/2\lt y∨-1≤x∧x\lt0∧-1/2≤y≤1/2∨0≤x∧x\lt1∧y\lt-1/2\bigr\rbrack, \bigl\lbrack1, 1≤x∧-1/2≤y≤1/2∨0≤x∧x\lt1∧1/2\lt y\bigr\rbrack, \bigl\lbrack2, 1≤x∧1/2\lt y\bigr\rbrack\bigr\rbrack, 0)

A symbol is left alone

live ↗
InPiecewiseExpand(pw)\mathrm{PiecewiseExpand}(\mathrm{pw})Outpw\mathrm{pw}

A chained inequality with max stays

live ↗
InPiecewiseExpand(−1≤max⁡(x,y)≤1)\mathrm{PiecewiseExpand}(-1≤\max(x, y)≤1)Out−1≤max⁡(x,y)≤1-1≤\max(x, y)≤1

A max of absolute values is one Piecewise, each branch carrying the conditions of the pieces it came from

live ↗
InPiecewiseExpand(max⁡(∣x∣,∣y∣),R)\mathrm{PiecewiseExpand}(\max(\vert x\vert, \vert y\vert), ℝ)OutPiecewise([[−x,x<0∧y<0∧x−y≤0∨x<0∧0≤y∧x+y≤0],[x,0≤x∧y<0∧0≤x+y∨0≤x∧0≤y∧0≤x−y],[−y,x<0∧y<0∧0<x−y∨0≤x∧y<0∧x+y<0]],y)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-x, x\lt0∧y\lt0∧x-y≤0∨x\lt0∧0≤y∧x+y≤0\bigr\rbrack, \bigl\lbrack x, 0≤x∧y\lt0∧0≤x+y∨0≤x∧0≤y∧0≤x-y\bigr\rbrack, \bigl\lbrack-y, x\lt0∧y\lt0∧0\lt x-y∨0≤x∧y\lt0∧x+y\lt0\bigr\rbrack\bigr\rbrack, y)
live ↗
InPiecewiseExpand(if a then b else c)\mathrm{PiecewiseExpand}(\text{if }a\text{ then }b\text{ else }c)OutPiecewise([[b,a]],c)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack b, a\bigr\rbrack\bigr\rbrack, c)

Min and Max expand over any symbols, with no assumption on them

live ↗
InPiecewiseExpand(min⁡(x,y))\mathrm{PiecewiseExpand}(\min(x, y))OutPiecewise([[x,x−y≤0]],y)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, x-y≤0\bigr\rbrack\bigr\rbrack, y)

A constant bound is written as x≤2x \le 2, not as a difference

live ↗
InPiecewiseExpand(max⁡(x,2,y))\mathrm{PiecewiseExpand}(\max(x, 2, y))OutPiecewise([[2,x≤2∧y≤2],[x,2<x∧0≤x−y]],y)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack2, x≤2∧y≤2\bigr\rbrack, \bigl\lbrack x, 2\lt x∧0≤x-y\bigr\rbrack\bigr\rbrack, y)

A zero-valued branch is the Piecewise default

live ↗
InPiecewiseExpand(max⁡(x,y,0))\mathrm{PiecewiseExpand}(\max(x, y, 0))OutPiecewise([[x,0<x∧0≤x−y],[y,0<y∧x−y<0]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, 0\lt x∧0≤x-y\bigr\rbrack, \bigl\lbrack y, 0\lt y∧x-y\lt0\bigr\rbrack\bigr\rbrack, 0)

UnitStep of several arguments is the AND of their steps

live ↗
InPiecewiseExpand(UnitStep(x,y−2))\mathrm{PiecewiseExpand}(\mathrm{UnitStep}(x, y-2))OutPiecewise([[1,0≤x∧2≤y]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 0≤x∧2≤y\bigr\rbrack\bigr\rbrack, 0)

Clip to symbolic limits

live ↗
InPiecewiseExpand(Clip(x,[a,b]))\mathrm{PiecewiseExpand}(\mathrm{Clip}(x, \bigl\lbrack a, b\bigr\rbrack))OutPiecewise([[a,0<a−x],[b,b−x<0∧a−x≤0]],x)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack a, 0\lt a-x\bigr\rbrack, \bigl\lbrack b, b-x\lt0∧a-x≤0\bigr\rbrack\bigr\rbrack, x)

Clip to [0,1][0, 1]: the zero-valued branch is the default

live ↗
InPiecewiseExpand(Clip(x,[0,1]))\mathrm{PiecewiseExpand}(\mathrm{Clip}(x, \bigl\lbrack0, 1\bigr\rbrack))OutPiecewise([[1,1<x],[x,0≤x≤1]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1\lt x\bigr\rbrack, \bigl\lbrack x, 0≤x≤1\bigr\rbrack\bigr\rbrack, 0)

UnitBox of an affine argument solves for the variable

live ↗
InPiecewiseExpand(UnitBox(2x−1))\mathrm{PiecewiseExpand}(\mathrm{UnitBox}(2x-1))OutPiecewise([[1,1/4≤x≤3/4]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1/4≤x≤3/4\bigr\rbrack\bigr\rbrack, 0)

Floor on −2≤x≤2-2 \le x \le 2: the zero-valued stretch is the default

live ↗
InPiecewiseExpand(⌊x⌋,−2≤x≤2)\mathrm{PiecewiseExpand}(\lfloor x\rfloor, -2≤x≤2)OutPiecewise([[−2,x<−1],[−1,−1≤x∧x<0],[1,1≤x∧x<2],[2,2≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-2, x\lt-1\bigr\rbrack, \bigl\lbrack-1, -1≤x∧x\lt0\bigr\rbrack, \bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x\bigr\rbrack\bigr\rbrack, 0)

Ceil on 0<x<30 < x < 3: the last stretch is the default

live ↗
InPiecewiseExpand(⌈x⌉,0<x<3)\mathrm{PiecewiseExpand}(\lceil x\rceil, 0\lt x\lt3)OutPiecewise([[1,x≤1],[2,1<x∧x≤2]],3)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, x≤1\bigr\rbrack, \bigl\lbrack2, 1\lt x∧x≤2\bigr\rbrack\bigr\rbrack, 3)

Round on 0≤x≤30 \le x \le 3: an even integer owns both ends of its stretch

live ↗
InPiecewiseExpand(round(x),0≤x≤3)\mathrm{PiecewiseExpand}(\mathrm{round}(x), 0≤x≤3)OutPiecewise([[1,1/2<x<3/2],[2,3/2≤x≤5/2],[3,5/2<x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1/2\lt x\lt3/2\bigr\rbrack, \bigl\lbrack2, 3/2≤x≤5/2\bigr\rbrack, \bigl\lbrack3, 5/2\lt x\bigr\rbrack\bigr\rbrack, 0)

IntegerPart on −2<x<2-2 < x < 2 truncates toward zero

live ↗
InPiecewiseExpand(IntegerPart(x),−2<x<2)\mathrm{PiecewiseExpand}(\mathrm{IntegerPart}(x), -2\lt x\lt2)OutPiecewise([[−1,x≤−1],[1,1≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, x≤-1\bigr\rbrack, \bigl\lbrack1, 1≤x\bigr\rbrack\bigr\rbrack, 0)

FractionalPart on −3<x<0-3 < x < 0 is negative, with the lowest stretch as the default

live ↗
InPiecewiseExpand(FractionalPart(x),−3<x<0)\mathrm{PiecewiseExpand}(\mathrm{FractionalPart}(x), -3\lt x\lt0)OutPiecewise([[x,−1<x],[x+1,−2<x∧x≤−1]],x+2)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, -1\lt x\bigr\rbrack, \bigl\lbrack x+1, -2\lt x∧x≤-1\bigr\rbrack\bigr\rbrack, x+2)

Mod by 2 on 0≤x≤50 \le x \le 5

live ↗
InPiecewiseExpand(x mod 2,0≤x≤5)\mathrm{PiecewiseExpand}(x\bmod2, 0≤x≤5)OutPiecewise([[x−4,4≤x],[x−2,2≤x∧x<4]],x)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x-4, 4≤x\bigr\rbrack, \bigl\lbrack x-2, 2≤x∧x\lt4\bigr\rbrack\bigr\rbrack, x)

Quotient by 3 on −4≤x≤7-4 \le x \le 7

live ↗
InPiecewiseExpand(Quotient(x,3),−4≤x≤7)\mathrm{PiecewiseExpand}(\mathrm{Quotient}(x, 3), -4≤x≤7)OutPiecewise([[−2,x<−3],[−1,−3≤x∧x<0],[1,3≤x∧x<6],[2,6≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-2, x\lt-3\bigr\rbrack, \bigl\lbrack-1, -3≤x∧x\lt0\bigr\rbrack, \bigl\lbrack1, 3≤x∧x\lt6\bigr\rbrack, \bigl\lbrack2, 6≤x\bigr\rbrack\bigr\rbrack, 0)

SquareWave on 0<x<20 < x < 2: its low half-periods, joined

live ↗
InPiecewiseExpand(SquareWave(x),0<x<2)\mathrm{PiecewiseExpand}(\mathrm{SquareWave}(x), 0\lt x\lt2)OutPiecewise([[−1,1/2≤x∧x<1∨3/2≤x]],1)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, 1/2≤x∧x\lt1∨3/2≤x\bigr\rbrack\bigr\rbrack, 1)

TriangleWave on 0≤x≤10 \le x \le 1: rising stretches first, then the falling one as default

live ↗
InPiecewiseExpand(TriangleWave(x),0≤x≤1)\mathrm{PiecewiseExpand}(\mathrm{TriangleWave}(x), 0≤x≤1)OutPiecewise([[4(x−1),3/4≤x],[4x,x<1/4]],−2(2x−1))\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack4(x-1), 3/4≤x\bigr\rbrack, \bigl\lbrack4x, x\lt1/4\bigr\rbrack\bigr\rbrack, -2(2x-1))

SawtoothWave on −1<x<1-1 < x < 1

live ↗
InPiecewiseExpand(SawtoothWave(x),−1<x<1)\mathrm{PiecewiseExpand}(\mathrm{SawtoothWave}(x), -1\lt x\lt1)OutPiecewise([[x,0≤x]],x+1)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, 0≤x\bigr\rbrack\bigr\rbrack, x+1)

The real cube root is negative below zero

live ↗
InPiecewiseExpand(CubeRoot(x))\mathrm{PiecewiseExpand}(\mathrm{CubeRoot}(x))OutPiecewise([[−(−x)1/3,x<0]],x3)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-(-x)^{1/3}, x\lt0\bigr\rbrack\bigr\rbrack, \sqrt[3]{x})

The conditions compare the cube root as it stands

live ↗
InPiecewiseExpand(⌊CubeRoot(x)⌋,0<x<10)\mathrm{PiecewiseExpand}(\lfloor\mathrm{CubeRoot}(x)\rfloor, 0\lt x\lt10)OutPiecewise([[1,1≤x1/3∧x1/3<2],[2,2≤x1/3]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x^{1/3}∧x^{1/3}\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x^{1/3}\bigr\rbrack\bigr\rbrack, 0)

The prime-counting staircase on a bounded interval

live ↗
InPiecewiseExpand(PrimePi(x),1<x<20)\mathrm{PiecewiseExpand}(\mathrm{PrimePi}(x), 1\lt x\lt20)OutPiecewise([[1,2≤x∧x<3],[2,3≤x∧x<5],[3,5≤x∧x<7],[4,7≤x∧x<11],[5,11≤x∧x<13],[6,13≤x∧x<17],[7,17≤x∧x<19],[8,19≤x]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack2, 3≤x∧x\lt5\bigr\rbrack, \bigl\lbrack3, 5≤x∧x\lt7\bigr\rbrack, \bigl\lbrack4, 7≤x∧x\lt11\bigr\rbrack, \bigl\lbrack5, 11≤x∧x\lt13\bigr\rbrack, \bigl\lbrack6, 13≤x∧x\lt17\bigr\rbrack, \bigl\lbrack7, 17≤x∧x\lt19\bigr\rbrack, \bigl\lbrack8, 19≤x\bigr\rbrack\bigr\rbrack, 0)
Properties #
live ↗
InMap(PiecewiseExpand,[min⁡(x,y),max⁡(x,y,z),UnitStep(x),Clamp(x,a,b)])\mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack\min(x, y), \max(x, y, z), \mathrm{UnitStep}(x), \mathrm{Clamp}(x, a, b)\bigr\rbrack)Out[Piecewise([[x,x−y≤0]],y),Piecewise([[x,0≤x−y∧0≤x−z],[y,x−y<0∧0≤y−z]],z),Piecewise([[1,0≤x]],0),Piecewise([[a,0<a−x],[b,b−x<0∧a−x≤0]],x)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, x-y≤0\bigr\rbrack\bigr\rbrack, y), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x, 0≤x-y∧0≤x-z\bigr\rbrack, \bigl\lbrack y, x-y\lt0∧0≤y-z\bigr\rbrack\bigr\rbrack, z), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 0≤x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack a, 0\lt a-x\bigr\rbrack, \bigl\lbrack b, b-x\lt0∧a-x≤0\bigr\rbrack\bigr\rbrack, x)\bigr\rbrack
live ↗
InAssuming([0≤x≤3],Map(PiecewiseExpand,[⌊x⌋,⌈x⌉,round(x),IntegerPart(x),FractionalPart(x)]))\mathrm{Assuming}(\bigl\lbrack0≤x≤3\bigr\rbrack, \mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack\lfloor x\rfloor, \lceil x\rceil, \mathrm{round}(x), \mathrm{IntegerPart}(x), \mathrm{FractionalPart}(x)\bigr\rbrack))Out[Piecewise([[1,1≤x∧x<2],[2,2≤x∧x<3],[3,3≤x]],0),Piecewise([[1,0<x∧x≤1],[2,1<x∧x≤2],[3,2<x]],0),Piecewise([[1,1/2<x<3/2],[2,3/2≤x≤5/2],[3,5/2<x]],0),Piecewise([[1,1≤x∧x<2],[2,2≤x∧x<3],[3,3≤x]],0),Piecewise([[x−3,3≤x],[x−2,2≤x∧x<3],[x−1,1≤x∧x<2]],x)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack3, 3≤x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 0\lt x∧x≤1\bigr\rbrack, \bigl\lbrack2, 1\lt x∧x≤2\bigr\rbrack, \bigl\lbrack3, 2\lt x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1/2\lt x\lt3/2\bigr\rbrack, \bigl\lbrack2, 3/2≤x≤5/2\bigr\rbrack, \bigl\lbrack3, 5/2\lt x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤x∧x\lt2\bigr\rbrack, \bigl\lbrack2, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack3, 3≤x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x-3, 3≤x\bigr\rbrack, \bigl\lbrack x-2, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack x-1, 1≤x∧x\lt2\bigr\rbrack\bigr\rbrack, x)\bigr\rbrack
live ↗
InMap(PiecewiseExpand,[UnitBox(x),UnitTriangle(x)])\mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack\mathrm{UnitBox}(x), \mathrm{UnitTriangle}(x)\bigr\rbrack)Out[Piecewise([[1,−1/2≤x≤1/2]],0),Piecewise([[1−x,0≤x≤1],[x+1,−1≤x∧x<0]],0)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, -1/2≤x≤1/2\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1-x, 0≤x≤1\bigr\rbrack, \bigl\lbrack x+1, -1≤x∧x\lt0\bigr\rbrack\bigr\rbrack, 0)\bigr\rbrack
live ↗
InAssuming([0≤x≤3],Map(PiecewiseExpand,[SquareWave(x),TriangleWave(x),SawtoothWave(x)]))\mathrm{Assuming}(\bigl\lbrack0≤x≤3\bigr\rbrack, \mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack\mathrm{SquareWave}(x), \mathrm{TriangleWave}(x), \mathrm{SawtoothWave}(x)\bigr\rbrack))Out[Piecewise([[−1,1/2≤x∧x<1∨3/2≤x∧x<2∨5/2≤x∧x<3]],1),Piecewise([[4(x−3),11/4≤x],[4(x−2),7/4≤x∧x<9/4],[4(x−1),3/4≤x∧x<5/4],[4x,x<1/4],[−2(2x−5),9/4≤x∧x<11/4],[−2(2x−3),5/4≤x∧x<7/4]],−2(2x−1)),Piecewise([[x−3,3≤x],[x−2,2≤x∧x<3],[x−1,1≤x∧x<2]],x)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, 1/2≤x∧x\lt1∨3/2≤x∧x\lt2∨5/2≤x∧x\lt3\bigr\rbrack\bigr\rbrack, 1), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack4(x-3), 11/4≤x\bigr\rbrack, \bigl\lbrack4(x-2), 7/4≤x∧x\lt9/4\bigr\rbrack, \bigl\lbrack4(x-1), 3/4≤x∧x\lt5/4\bigr\rbrack, \bigl\lbrack4x, x\lt1/4\bigr\rbrack, \bigl\lbrack-2(2x-5), 9/4≤x∧x\lt11/4\bigr\rbrack, \bigl\lbrack-2(2x-3), 5/4≤x∧x\lt7/4\bigr\rbrack\bigr\rbrack, -2(2x-1)), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack x-3, 3≤x\bigr\rbrack, \bigl\lbrack x-2, 2≤x∧x\lt3\bigr\rbrack, \bigl\lbrack x-1, 1≤x∧x\lt2\bigr\rbrack\bigr\rbrack, x)\bigr\rbrack

A symbolic modulus: the bound 0≤k≤3m0 \le k \le 3m is read as 0≤k/m≤30 \le k/m \le 3

live ↗
InAssuming(0≤k≤3m,Map(PiecewiseExpand,[k mod m,Quotient(k,m)]))\mathrm{Assuming}(0≤k≤3m, \mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack k\bmod m, \mathrm{Quotient}(k, m)\bigr\rbrack))Out[Piecewise([[k,k/m<1],[k−3m,3≤k/m],[k−2m,2≤k/m∧k/m<3]],k−m),Piecewise([[1,1≤k/m∧k/m<2],[2,2≤k/m∧k/m<3],[3,3≤k/m]],0)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack k, k/m\lt1\bigr\rbrack, \bigl\lbrack k-3m, 3≤k/m\bigr\rbrack, \bigl\lbrack k-2m, 2≤k/m∧k/m\lt3\bigr\rbrack\bigr\rbrack, k-m), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, 1≤k/m∧k/m\lt2\bigr\rbrack, \bigl\lbrack2, 2≤k/m∧k/m\lt3\bigr\rbrack, \bigl\lbrack3, 3≤k/m\bigr\rbrack\bigr\rbrack, 0)\bigr\rbrack
live ↗
InAssuming(x∈R,Map(PiecewiseExpand,[∣x∣,sgn(x),arg⁡(x)]))\mathrm{Assuming}(x∈ℝ, \mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack\vert x\vert, \mathrm{sgn}(x), \arg(x)\bigr\rbrack))Out[Piecewise([[−x,x<0]],x),Piecewise([[−1,x<0],[1,0<x]],0),Piecewise([[π,x<0]],0)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-x, x\lt0\bigr\rbrack\bigr\rbrack, x), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack-1, x\lt0\bigr\rbrack, \bigl\lbrack1, 0\lt x\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack π, x\lt0\bigr\rbrack\bigr\rbrack, 0)\bigr\rbrack
live ↗
InMap(PiecewiseExpand,[δxy,DiscreteDelta(x,y)])\mathrm{Map}(\mathrm{PiecewiseExpand}, \bigl\lbrack δ_{xy}, \mathrm{DiscreteDelta}(x, y)\bigr\rbrack)Out[Piecewise([[1,x−y=0]],0),Piecewise([[1,x=0∧y=0]],0)]\bigl\lbrack\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, x-y=0\bigr\rbrack\bigr\rbrack, 0), \mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, x=0∧y=0\bigr\rbrack\bigr\rbrack, 0)\bigr\rbrack
live ↗
InPiecewiseExpand([a])\mathrm{PiecewiseExpand}(\lbrack a\rbrack)OutPiecewise([[1,a]],0)\mathrm{Piecewise}(\bigl\lbrack\bigl\lbrack1, a\bigr\rbrack\bigr\rbrack, 0)
Possible issues #

with no assumption that xx is real, Abs is left alone — same as Wolfram

live ↗
InPiecewiseExpand(∣x∣)\mathrm{PiecewiseExpand}(\vert x\vert)Out∣x∣\vert x\vert

From a span of 100, Floor is left as it is, as in Wolfram

live ↗
InPiecewiseExpand(⌊x⌋,0<x<100)\mathrm{PiecewiseExpand}(\lfloor x\rfloor, 0\lt x\lt100)Out⌊x⌋\lfloor x\rfloor
About
Examples64
Checked againstWolfram Language 60 agree2 differ
CrosswalkWolfram Language