# 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9868523478507996, "perplexity": 377.6003649968618}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-27/segments/1435375095373.99/warc/CC-MAIN-20150627031815-00217-ip-10-179-60-89.ec2.internal.warc.gz"}