# Meet the Triangle Wave - Piecewise in Disguise

## May 24, 2021 | 6 minutes, 34 seconds

# Introduction

In the last post we saw that two seemingly inverse composed operations resulted in some wacky stuff happening - that is, complex numbers being mapped to a single region (if you haven't read it, it's worth a look because it forms a similar motivation for this post).

When we talk functional inverses, we look for well-defined functions that map some subset of our codomain of the original function to some subset of the domain of our original function - but not necessarily the whole of both. So when talking about the pair of inverse functions \(x^2\) and \(\sqrt{x}\), we usually take the functional inverse of \(x^2\) to be \(\sqrt{x}:\mathbb{R}_{\geq 0}\to\mathbb{R}_{\geq 0}\). The consequence of limiting ourselves this way is that when we compose these equations with each other - in whatever order, we either restrict the domain of the composed function, or we create a branch for some domain(s) where we don't have the same values as before (\(|x|\) is the example used thus far).

Turns out there are some more examples of the above latter phenomenon - particularly, with the composition of \(\sin\) and \(\arcsin\), as well as \(\cos\) and \(\arccos\), naturally. The approach we take to show this is much the same as our last post, but the periodicity of each function is something to look out for, which we'll see. As you can probably tell from the title, we can argue that \(\arcsin{\sin(x)}\) and \(\arccos{\cos(x)}\) are piecewise. In this post, I'll derive the former equation - a challenge for you is to derive the equation for the latter (which gives an easier, and arguably nicer, formula).

# Derivation

## Setup

Given the periodicity of the function we'll see later on, there are several stages to this derivation we'll want to setup.

Firstly, we'll let \(f(x) = \arcsin{\sin(x)}\) such that:

\[ f(x) = \begin{cases} \vdots & \vdots \\ f_{-1}(x) & x\in\alpha_{-1} \\ f_{0}(x) & x\in\alpha_{0} \\ f_{1}(x) & x\in\alpha_{1} \\ \vdots & \vdots \end{cases}\]

Then we'll solve for \(k\in\mathbb{Z}\) (as opposed to over \(\mathbb{N}\)) such that \(f_k(x) = \arcsin{\sin(x)} \implies \sin(f_k(x))=\sin(x)\).

After some experimentation, we have the following general solutions (which we can prove by the completeness of the subdomains, given a many-to-one relation, as further below):

- \(\sin(f_k(x))=\sin(x-2m\pi) \implies f_k(x) = x-2m\pi\)
- \(\sin(f_k(x))=\sin(-x+(2m+1)\pi) \implies f_k(x) = -x+(2m+1)\pi\)

Consequently, we'll construct a function \(f_k(x)\) such that

\[ \forall x\in\mathbb{R}\ \exists k\in\mathbb{Z}, f(x) = f_k(x) = \begin{cases} x-2k\pi & x\in\beta_1 \\ -x+(2k+1)\pi & x\in\beta_2 \end{cases}\]

Given that \(\arcsin{\sin(x)}\in\left[-\frac{\pi}{2},\frac{\pi}{2}\right]\) we have \(\beta_1 = \left[\frac{(4k-1)\pi}{2},\frac{(4k+1)\pi}{2}\right]\) and \(\beta_2 = \left[\frac{(4k+1)\pi}{2},\frac{(4k+3)\pi}{2}\right]\). For our current example, it's important we test boundary values. For both cases, and the function itself, considering all cases in \(f\), it's fine to leave the intervals open at the suprema (which will be important later). Consequently, \(\operatorname{dom}{f_k(x)} = \alpha_k = \left[\frac{(4k-1)\pi}{2},\frac{(4k+3)\pi}{2}\right)\), and we can write our k-case function as follows:

\[ f_k(x) = \begin{cases} x-2k\pi & x<(4k+1)\frac{\pi}{2} \\ -x+(2k+1)\pi & x\geq(4k+1)\frac{\pi}{2} \end{cases}\]

And thus we have our initial/basic setup, which describes the triangle wave in terms of infinite pairs of lines along \(x\). Arguably, this is the most intuitive definition, because it describes every line along our function. It, however, doesn't show us where else we can connect this function to (insert foreshadowing here).

## Infinite piecewise form

So far we've established two major ideas: that \(f\) is an infinite piecewise function for real values, and that it is composed of \(f_k\) for \(k\in\mathbb{Z}\), describing pairs of lines. What we now want to do is combine these ideas into a single piecewise equation so we know how to interpret it.

Firstly, we know the domains of each sub function. But we don't know an explicit form for them; we should probably avoid piecewise within piecewise for infinite piecewise here, so let's establish a form for \(f_k(x)\).

\[ \begin{align} f_k(x) &= \begin{cases} x-2k\pi & x<(4k+1)\frac{\pi}{2} \\ -x+(2k+1)\pi & x\geq(4k+1)\frac{\pi}{2} \end{cases} \\ &= -x+(2k+1)\pi+2\begin{cases} x-(4k+1)\frac{\pi}{2} & x<(4k+1)\frac{\pi}{2} \\ 0 & x\geq(4k+1)\frac{\pi}{2} \end{cases} \\ &= \operatorname{min}\left(x-2k\pi,-x+(2k+1)\pi\right) \\ &= \frac{\pi}{2}-\left|x-\frac{(4k+1)\pi}{2}\right| \end{align}\]

For the sake of moving on, we'll use the last form we established to construct \(f(x)\):

\[ f(x) = \begin{cases} \vdots & \vdots \\ \frac{\pi}{2}-\left|x-\frac{-3\pi}{2}\right| & x\in\left[\frac{-5\pi}{2},\frac{-\pi}{2}\right) \\ \frac{\pi}{2}-\left|x-\frac{\pi}{2}\right| & x\in\left[\frac{-\pi}{2},\frac{3\pi}{2}\right) \\ \frac{\pi}{2}-\left|x-\frac{5\pi}{2}\right| & x\in\left[\frac{3\pi}{2},\frac{7\pi}{2}\right) \\ \vdots & \vdots \\ \end{cases}\]

However, if you want to see the form in terms of \(\operatorname{min}\):

\[ f(x) = \begin{cases} \vdots & \vdots \\ \operatorname{min}\left(x+2\pi,-x-\pi\right) & x\in\left[\frac{-5\pi}{2},\frac{-\pi}{2}\right) \\ \operatorname{min}\left(x,-x+\pi\right) & x\in\left[\frac{-\pi}{2},\frac{3\pi}{2}\right) \\ \operatorname{min}\left(x-2\pi,-x+3\pi\right) & x\in\left[\frac{3\pi}{2},\frac{7\pi}{2}\right) \\ \vdots & \vdots \\ \end{cases}\]

The former form lends itself rather heavily to compacting - that is, we can see patterns in each case that we can squeeze out and by potentially some miracle, create a closed form for.

## Compacting the equation

Taking our equation without \(\operatorname{min}\), we can manipulate it a little bit to get the following:

\[ f(x) = \frac{\pi}{2}-\left| x-\frac{\pi}{2}\begin{cases} \vdots & \vdots \\ -3 & x\in\left[\frac{-5\pi}{2},\frac{-\pi}{2}\right) \\ 1 & x\in\left[\frac{-\pi}{2},\frac{3\pi}{2}\right) \\ 5 & x\in\left[\frac{3\pi}{2},\frac{7\pi}{2}\right) \\ \vdots & \vdots \\ \end{cases}\right|\]

When I first saw this, my thoughts went something like *oh-my-god-this-is-the-floor-function* and *whoops-this-is-one-ugly-equation*. So what we can do here, is, after a lot of thinking, is 'shift' the cases of the piecewise to match the floor function as well as their respective conditions. After doing so, we're left with:

\[ \begin{align} f(x) &= \frac{\pi}{2}-\left| x-\frac{\pi}{2}\left(1+4\begin{cases} \vdots & \vdots \\ -1 & \frac{1}{2\pi}x+\frac{1}{4}\in\left[-1,0\right) \\ 0 & \frac{1}{2\pi}x+\frac{1}{4}\in\left[0,1\right) \\ 1 & \frac{1}{2\pi}x+\frac{1}{4}\in\left[1,2\right) \\ \vdots & \vdots \\ \end{cases}\right)\right| \\ &= \frac{\pi}{2}-\left| x-\frac{\pi}{2}\left(1+4\left\lfloor \frac{x}{2\pi}+\frac{1}{4} \right\rfloor \right)\right| \end{align}\]

There are a few ways to clean this equation up. But given that this wave is derivative (not calculus derivative) of the sine wave, you can flip and shift in all the same ways (in this case, flip, shift by pi and go from there). And, if you're a believer in modulo non-integers (via definition \(x-n\left\lfloor \frac{x}{n} \right\rfloor\)), you get this *very* cute equation:

\[ \arcsin{\sin(x)} = \left|\bmod\left(x-\frac{\pi}{2},2\pi\right)-\pi\right|-\frac{\pi}{2}\]

Funnily enough, the cosine equivalent of this wave is actually *much* neater. Make sure to do that one yourself, you'll find it quite satisfying!