# Conversion between Piecewise[] and Abs[] representations [duplicate]

Is there a utility in Mathematica that can convert between two representations of an expression? The simplest example I can think of is

Abs[x]


Which I'd like to convert to something like

Piecewise[{{-x,x<0},{x,x>=0}}]


Another simple example is

f[r_]:= 1/16 ((-3+6r) Abs[1 - 2 r] + (3 - 2 r) Abs[3 - 2 r]
- 3 (1 + 2 r) Abs[1 + 2 r] + (3 + 2 r) Abs[3 + 2 r])

Assuming[r > 1/2, Simplify[f[r]]]
Assuming[-1/2 < r < 1/2, Simplify[f[r]]]
Assuming[r < -1/2, Simplify[f[r]]]
Assuming[-2 < r < 2, Simplify[f[r]]]
Assuming[-1/2 < r < 3/2, Simplify[f[r]]]
Assuming[0 < r < 2, Simplify[f[r]]]


which produces the output (sorry for formatting; I'm just using "copy as LaTeX" from Mathematica):

Out[2]=


$$\begin{array}{cc} \{ & \begin{array}{cc} \frac{1}{8} (3-2 r)^2 & 2 r\leq 3 \\ 0 & \text{True} \\ \end{array} \\ \end{array}$$

 Out[3]=


$$\frac{3}{4}-r^2$$

 Out[4]=


$$\begin{array}{cc} \{ & \begin{array}{cc} \frac{1}{8} (2 r+3)^2 & r\geq -\frac{3}{2} \\ 0 & \text{True} \\ \end{array} \\ \end{array}$$

 Out[5]=1/16 ((-3 + 6 r) Abs[1 - 2 r] + (3 - 2 r) Abs[3 - 2 r]
- 3 (1 + 2 r) Abs[1 + 2 r] + (3 + 2 r) Abs[3 + 2 r])

Out[6]=


$$\begin{array}{cc} \{ & \begin{array}{cc} \frac{3}{4}-r^2 & 2 r\leq 1 \\ \frac{1}{8} (3-2 r)^2 & \text{True} \\ \end{array} \\ \end{array}$$

 Out[7]=1/16 (6 - 8 r^2 + (-3 + 6 r) Abs[1 - 2 r] + (3 - 2 r) Abs[3 -2 r])


So it's able to recognize what the function is over each domain, and if there are only two pieces, it can produce a piecewise expression, but if there are more than two pieces it fails. Is there another way to get piecewise expressions that would be better than Assuming[xmin<x<xmax,Simplify[f[x]]?

For testing purposes, the following function can be used to generate piecewise functions (not explicitly piecewise, but using Abs[] as above). g[n] will produce an n-segment piecewise expression on the interval [-n/2,n/2] with segments of length 1.

g[n_Integer] := Assuming[-n <= r <= n,
Integrate[1/(2 π) (Sin[k/2]/(k/2))^n
(Cos[k r] - I Sin[k r]),
{k, -∞, ∞}]]


version info:

\$Version
"11.0.0 for Linux x86 (64-bit) (July 28, 2016)"


You need PiecewiseExpand, with its second argument being Reals:
PiecewiseExpand[