# Rewriting A Function Without Going Piecewise

date: ‘2015-11-02’ permalink: /rewriting-a-function-without-going-piecewise/ title: Rewriting a function without going piecewise —

You know the ridiculous kind of pseudo-context question that makes you go ‘Why doesn’t Lisa get a *proper* hobby rather than timing her friends doing jigsaw puzzles?’? You could replace pretty much all of them with “A mathematician is trying to be clever by…”.

In this particular case, a mathematician has worked out a function to be: \(f(n) = \\begin{cases} 0 & n \\le 0 \\\\ 2n + 1 & n > 0 \\end{cases}\) Only s/he wants to rewrite it as a non-piecewise function. Preferably continuous over the real numbers. Why? *S/he is trying to be clever.* Cleverness is important to mathematicians.

And, of course, I like both solving puzzles *and* looking clever, so I naturally decided to have a go.

My first thought was, the modulus function is especially good for this sort of thing. If you think about $f(x) = x + \left|x\right|$, you find that your function works out to be $f(x) =2x$ if $x≥0$, and $f(x) = 0$ otherwise. This function is a bit trickier, though, as it’s really in three parts: \(f(x) = \\begin{cases} 0 & x \\le 0 \\\\ 3x & 0 \\lt x \\le 1 \\\\ 2x + 1 & x > 1 \\end{cases}\)

So, my thinking was that I’d need to come up with two functions to add together: one that was zero for negative $x$ but $3x$ elsewhere, and one that was zero for $x \lt 1$ but $-x$ elsewhere – so that when they’re added, the gradient for $x \gt 1$ would be 2.

The first one is easy: if $x + \left|x\right|$ gives gradients of 0 and 2, multiplying it by $\frac 32$ gives gradients of 0 and 3; one part of the function will be $\frac 32 x + \left| \frac 32 x \right|$.

The other is trickier. To get the split at $x=1$, I need to use something involving $\left|x-1\right|$ (or some multiple thereof. $x - 1 + \left|x - 1\right|$ would give me a gradients of 0 and 2, but I want gradients of 0 and -1 – so I need to multiply everything by $-\frac 12$ to get $-\frac12(x - 1) - \frac12\left|x - 1\right|$.

Adding them together gives $f(x) = \frac 32 x + \left| \frac 32 x \right| - \frac12 (x-1) - \frac 12\left|x-1\right|$, which I can simplify to $f(x) = x + \frac 12 + \frac 32 \left|x\right| - \frac 12\left|x-1\right|$.

It’s not a *nice* function, but it is continuous and not piecewise – I’d be curious to know if anyone has a nicer version!

* Edited 2015-11-06 to fix a sign error. Many thanks to @oldandrew for spotting the mistake.