# How to indicate keeping the sign when squaring a number.

I have a vector $x$, which I wish to transform according the the following computer code

xstar = sign(x) * x^2


with $x^*$ preserving the positiveness or negativeness of $x$.

How would I write this in proper mathematics? Obviously $x^2$ is incorrect, and $$x^*=\begin{cases} x^2 & \text{if } x \geq 0 \\ -x^2 & \text{if } x < 0 \end{cases}$$ is very clunky. Surely there must be something...

-
$x^\ast = x\cdot \lvert x\rvert$? –  Daniel Fischer Aug 23 '13 at 19:05
Note that $|x|$ is not differentiable but this $x^*$ is which can provide good exam questions. –  Jp McCarthy Aug 23 '13 at 19:08
While $x \cdot |x|$ certainly works, there's nothing wrong with the cases definition, and it's actually easier to understand. –  Javier Badia Aug 23 '13 at 19:15
If $x$ is a vector, what do you mean by its sign? It seems to be a real, though that could be a vector in $\Bbb R^1$ –  Ross Millikan Aug 23 '13 at 19:26
But then the sign of $\mathbf{x}$ doesn't seem to be well defined. What is the sign of $[0,1]$? of $[1,-1]$? –  Ross Millikan Aug 23 '13 at 19:34

How about $x\cdot|x|$? That avoids the need for piecewise definition. And if $|x|$ is defined piecewise for you, then you can use $x\sqrt{x^2},$ instead.
You could just write $\mathrm{sgn}(x)\cdot x^2$. There's nothing "improper" about it! You could even write $\mathrm{sgn}(x)\cdot |x|^2$, which has the advantage that you can read off the sign and the magnitude individually. This is probably the clearest representation of your design intent, which is important for readability.
(I'm assuming that $x$ is a real number. If $x$ is complex, then the above expressions aren't equivalent, so it depends on your desired behavior. If $x$ is a vector, then again, it depends on what you want to do.)
I hadn't known about the $sgn()$ operator before now. –  gmacfarlane Aug 23 '13 at 19:31