# Indefinite integral of $|x|$

Today, one of my student found the following integral and asked me for its solution:

$$\int|x|\; dx$$

I used some formal methods for it, but it seems I need a magic trick. I wish, I am noting a valuable question here for others. Thanks for your time.

-
What is the magic trick? –  Tunococ Nov 27 '12 at 20:19
@Tunococ: I didn't note that I could break the integral respect to $x>0$ and $x<0$ as Chris did below. Shame! I was thinking about a wrong way. Sorry. –  Babak S. Nov 27 '12 at 20:25

For $x>0$, $|x|=x$, so has antiderivative $\frac{x^2}{2}$. For $x<0$, $|x|=-x$, and so has antiderivative $-\frac{x^2}{2}$. Thus we should consider the function $$f(x)=\begin{cases}\frac{x^2}{2} & x>0 \\-\frac{x^2}{2} & x \le 0 \end{cases}$$ It's not hard to check from first principles that $f'(0)=0$, and so $f$ is indeed an antiderivative of $|x|$. Of course, so is $f+c$ for any constant $c$.
For those not fond of "cases" constructs: You may use $\frac{x\cdot|x|}2$ –  Hagen von Eitzen Nov 27 '12 at 20:25
@HagenvonEitzen: Honestly, I wanted to note that, but you did. Yes it is $\frac{x|x|}{2}$. Thanks both of you. –  Babak S. Nov 27 '12 at 20:27