# Identity function that returns $1$ for the input $0$.

I'm looking for a way to write the following function:

$$id(x) = \begin{cases} x & x \neq 0 \\ 1 & x = 0 \end{cases}$$

However, I want to implement it without using conditionals. Any ideas? The simpler, the better.

• Aside from functions like $sgn$, which is itself defined by conditionals, all standard functions are continuous on their domains, as are the usual operations like addition, powers, etc., so all algebraic expressions you can come up with will either be continuous or undefined at some point of the real like. If you're willing to use $sgn$ (the "signum" function, which is -1 on negatives, 1 on positives, and 0 for 0), then $id(x) = 1 - sgn^2(x)$ works... – John Hughes Dec 17 '16 at 23:53
• @JohnHughes That does not seem to match the expected value, for $x\neq 0$ it returns $0$, but it should return $x$. Perhaps something like $1-sgn (x)^2+x sgn (x)^2$ ... – Sil Dec 18 '16 at 0:09
• Good catch - I misread the original. – John Hughes Dec 18 '16 at 0:34
• @JohnHughes An equivalent function to $id$ can be written with arctan and tangent, see my answer below. – Enrico Borba Dec 19 '16 at 4:45

How about $$f(x) = x + 0^{|x|}$$

• That's a wonderful solution. – Aadit M Shah Dec 18 '16 at 0:09
• Unfortunately, $0^0$ is ambiguous: its value depends on context and need not be $1$. Google "0^0" for several discussions. – Ethan Bolker Dec 18 '16 at 0:12
• I agree totally with @EthanBolker: You have absolutely no justification in saying “$0^0=1$” – Lubin Dec 18 '16 at 0:14
• proofwiki.org/wiki/Zero_to_the_Zeroth_Power there's plenty of justification for $0^0=1$, in particular, the binomial theorem fails without it, and the vacuous sum convention is broken. – GFauxPas Dec 18 '16 at 0:16
• @MathematicsStudent1122 I thought that by conditional the OP meant something that can't be "inlined" and requires the explicit distinction between different cases. You're right that $|x|$ acts like a conditional, but you can make a similar argument about functions that aren't defined at particular points, such as $1/x$, that they are sort of conditionals. In any case the $|x|$ can be easily replaced with $x^2$. – benji Dec 18 '16 at 0:52

As @JohnHughes points out, normal algebra can't help you. If you're willing to use the Kronecker delta function then $$f(x) = x + \delta(x,0)$$ does what you want.

• Perhaps you mean the Kroknecker delta, because we try to avoid evaluating d.d. outside of an integral. – GFauxPas Dec 18 '16 at 0:25
• @GFauxPas You're right, thanks. Outside an integral the Dirac delta function must be "$\infty$" at $0$, not $1$. Edited accordingly. – Ethan Bolker Dec 18 '16 at 0:41

This can be achieved merely with the arctan and tangent function. The functions $z_1, z_2, z_3$, return $1$ when $x = 0$, and $0$ when $x \neq 0$. Notice this is different than your $id$ function, but will be used to construct it.

$$z_1(x) = 2^{\lceil |x|\rceil} \text{ mod } 2$$

$$z_2(x) = 1 - |\text{sign}(x)|$$

$$z_3(x) = 1 - \bigg\lceil\frac{|x|}{|x| + 1}\bigg\rceil$$

The absolute values can also be replaced with squares, i.e.

$$z_3(x) = 1 - \bigg\lceil\frac{x^2}{x^2 + 1}\bigg\rceil$$

Source: I spent a lot of time trying to write conditional functions without conditionals.

Note: $$\lfloor x \rfloor = (x - 0.5) - \frac{\arctan(\tan(\pi(x - 0.5)))}{\pi}$$

Then we can write $$\lceil x \rceil = -\lfloor -x \rfloor$$

So, we can write $z_3$ merely from arctan and tan: $$z_3(x) = 1 + \bigg\lfloor - \frac{x^2}{x^2 + 1}\bigg\rfloor = 1 - \frac{x^2}{x^2 + 1} - 0.5 - \frac{\arctan(\tan(\pi(- \frac{x^2}{x^2 + 1} - 0.5)))}{\pi}$$

Then,

$$id(x) = z_3(x) + x\cdot(1 - z_3(x))$$

Not pretty, but is entirely in terms of elementary functions.