6
$\begingroup$

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

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

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

$\endgroup$
  • 2
    $\begingroup$ 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... $\endgroup$ – John Hughes Dec 17 '16 at 23:53
  • $\begingroup$ @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$ ... $\endgroup$ – Sil Dec 18 '16 at 0:09
  • $\begingroup$ Good catch - I misread the original. $\endgroup$ – John Hughes Dec 18 '16 at 0:34
  • $\begingroup$ @JohnHughes An equivalent function to $id$ can be written with arctan and tangent, see my answer below. $\endgroup$ – Enrico Borba Dec 19 '16 at 4:45
7
$\begingroup$

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

$\endgroup$
  • 4
    $\begingroup$ That's a wonderful solution. $\endgroup$ – Aadit M Shah Dec 18 '16 at 0:09
  • 6
    $\begingroup$ Unfortunately, $0^0$ is ambiguous: its value depends on context and need not be $1$. Google "0^0" for several discussions. $\endgroup$ – Ethan Bolker Dec 18 '16 at 0:12
  • 1
    $\begingroup$ I agree totally with @EthanBolker: You have absolutely no justification in saying “$0^0=1$” $\endgroup$ – Lubin Dec 18 '16 at 0:14
  • 3
    $\begingroup$ 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. $\endgroup$ – GFauxPas Dec 18 '16 at 0:16
  • 2
    $\begingroup$ @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$. $\endgroup$ – benji Dec 18 '16 at 0:52
5
$\begingroup$

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.

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

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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