Is it possible to write this piecewise-defined function as a regular function?

83 Views Asked by At

I have this piecewise-defined function: $$ g(a, b, e) = \left\{\begin{array}{ll} 1 - e & \text{if }a > b,\\ 0.5 & \text{if }a = b,\\ e & \text{if }a < b \end{array}\right. $$ $$ e \in \mathopen(0,0.5\mathclose) $$ I need it so that it can be written without the if else. I already figured that it would be possible to have -1 or 1 depending on whether a is greater than b or vice versa: $$ \text{sign} = \frac{a-b}{|a-b|} $$ However, I am stuck here. Is this possible at all?

1

There are 1 best solutions below

4
On BEST ANSWER

Using the sign function you defined, you could take

$$g(x)=e+\frac{1-2e}{2}\left(1+\frac{a-b}{|a-b|}\right)=\frac{1}{2}+\frac{a-b}{|a-b|}\left(\frac{1}{2}-e\right)$$

This if $a>b$, we get $e+(1-2e)=1-e$ and if $a<b$ we get $e+0=e$. We still have zero-division problems when $a=b$, but I believe this will always be the case when trying to write functions that are discontinuous in their parameters as `regular' functions.

EDIT: If you want to code this in Python, you can actually use the cases natively in your formula. If they are true, they will evaluate to $1$, else $0$. Hence, your function could look like

e*(a>b) + 0.5*(a==b) + (1-e)*(a<b)