How to calculate $ \left| \sin x \right| $ derivative in a more elegant way? I am trying to calculate the derivative of $\left| \sin x \right| $
Given the graphs, we notice that the derivative of $\left| \sin x \right|$ does not exist for $x= k\pi$.
Graph for $\left|\sin x\right|$:

We can rewrite the function as
$\left| \sin(x) \right| =  \left\{
\begin{array}{ll}
      \sin(x),& 2k\pi < x < (2k+1)\pi \\
      -\sin(x), & \text{elsewhere} \\
\end{array} 
\right.  $
Therefore calculate its derivative as:
$(\left| \sin(x) \right|)^{'} =  \left\{
\begin{array}{ll}
      \cos(x),& 2k\pi < x < (2k+1)\pi \\
      -\cos(x), & \text{elsewhere} \\
\end{array} 
\right.  $
Is there a way to rewrite this derivative, in a more elegant way (as a non-branch function) $(\left| \sin(x) \right|)^{'} = g(x)$?
 A: Apparently other answers missed the following direct approach: we have $$|x|' = \frac{x}{|x|}$$ for $x\ne 0$.
So, for example, VIVID's formula $|f(x)|' = \frac{f(x)}{|f(x)|} f'(x)$ is just the chain rule.
A: I think an even nicer way of thinking about it is in terms of the sign of $\sin(x)$ applied to the derivative of $\sin(x)$. A function $f$ that returns the sign of another function $g$ is: $f(x)=\frac{\lvert(g(x))\rvert}{g(x)}$ .
So quite simply $\lvert \sin(x) \rvert'=\frac{\lvert \sin(x)) \rvert}{\sin(x)}\cdot\cos(x)$
This is essentially just $\cos(x)$ but reflected across the axis at the intervals where $\sin(x)$ is negative, that is: $\cos(x)$ for $x \in [0,\pi]$ and $-\cos(x)$ for $x \in [\pi,2\pi]$ and so on. The reason for this is not difficult to see: when we take the absolute value of a function, then the gradients of the points belonging to the intervals that were flipped, here where $x \in [\pi,2\pi]$, are also flipped. So what ends up happening is that the derivative essentially stays the same, but is flipped on the intervals where the original function was flipped.
A: The better way, for me, is as follows:
$$f(x)=\left|\sin(x)\right|=\sqrt{\sin^2(x)}$$
Now, differentiate both sides to get
$$f'(x)=\frac{1}{2\sqrt{\sin^2(x)}}\cdot2\sin(x)\cos(x)=\frac{\sin(2x)}{2\left|\sin(x)\right|}$$
Therefore,
$$\left(\left|\sin(x)\right|\right)'=\frac{\sin(2x)}{2\left|\sin(x)\right|}, \ \ \ \  x \neq k\pi, k\in \mathbb{Z}$$

Appendum: This approach can easily be extended to a general case of finding $\left(|f(x)|\right)'$.
First, we rewrite $$|f(x)| = \sqrt{f^2(x)}$$
Then, repeating the work above:
$$|f(x)|' = \frac{1}{2\sqrt{f^2(x)}}[2f(x)f'(x)] = \frac{f(x)}{|f(x)|}f'(x)$$
we get
$$\boxed{|f(x)|' = \frac{f(x)}{|f(x)|}f'(x)}$$
