Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|cite|improve this question
$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 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
up vote 8 down vote accepted

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.

share|cite|improve this answer

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.)

share|cite|improve this answer
I hadn't known about the $sgn()$ operator before now. – gregmacfarlane Aug 23 '13 at 19:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.