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

38 Views Asked by At

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} $$

1

There are 1 best solutions below

4
On BEST ANSWER

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