0

Function returning $\left\lvert x \right\rvert$, $0$ or $-\left\lvert x \right\rvert$, depending on input

I have a function $m(x, y)$ that returns
$\left\lvert x \right\rvert$ if $y$ is positive ($y > 0$),
$0$ if $y$ is $0$ ($y = 0$),
or $-\left\lvert x \right\rvert$ if $y$ is negative ($y < 0$). One important rule is that there mustn't be any 'if's (case destinctions) in the function.
This function was originally intended to behave like an activation function in a neural network, but now it's stuck in my mind. I can't find any solution.
If the question turns out to be impossible without an if, is it possible where $m(x, y)$ with negative y returns $1$ instead? So far I have: $$m(x, y) = \left\lvert x \right\rvert \times f(y)\\f(y) = $$ in which $f(y)$ follows:
$f(y) = 1$ where $y > 0$,
$f(y) = 0$ where $y = 0$,
$f(y) = -1$ where $y < 0$.

Compact version of question:

Convert the following function into one without case destinction. $$m(x, y) = \begin{Bmatrix} \left\lvert x \right\rvert : y > 0 \\ 0 : y = 0 \\ -\left\lvert x \right\rvert : y < 0 \\ \end{Bmatrix} $$

Duncan
  • 115
  • Does anyone know more tags I should add? – Duncan May 01 '17 at 10:15
  • How about just $m(x,y)=|x|\cdot \text{sgn}(y)$, where $\text{sgn}$ denotes the sign function (a function that is $-1$ for negative, $1$ for positive values and $0$ at $0$) –  May 01 '17 at 10:19
  • The $sgn$ function uses set-builder notation. – Duncan May 01 '17 at 10:21
  • Not necessarily. I'll post an answer using the $\text{sgn}$ function. –  May 01 '17 at 10:22
  • 1
    Btw, this notation is not set builder notation (as you do not use it to build a set). -- But as you complain that signum requires case distinction, how come you are okay with $|x|$? Doesn't it use case distinction as well? Or if you define it as $|x|:=\max{x,-x}$, doesn't $\max$ use case distinction? – Hagen von Eitzen May 01 '17 at 10:24
  • 1
    Also, since $m(1,y)=\operatorname{sgn}(y)$, your problem is equivalent to finding an acceptable notation for the signum function – Hagen von Eitzen May 01 '17 at 10:30
  • I suspect he's trying to program something in a language that does support a native absolute function but not a sign function –  May 01 '17 at 10:31
  • No, but it's more like a puzzle that I couldn't solve – Duncan May 01 '17 at 11:58

1 Answers1

0

Let

$$\text{sgn}(x)=\left\lfloor\frac{x}{|x|+1}\right\rfloor-\left\lfloor\frac{-x}{|-x|+1}\right\rfloor$$

then $\text{sgn}(x)$ takes on $-1$ when $x<0$, $1$ whenever $x>0$ and $0$ when $x=0$.

Now the function you're looking for is

$$m(x,y)=|x|\cdot \text{sgn}(y)$$