0
$\begingroup$

Given two real numbers $a$ and $b$, is there any formula which would give $1$ if $a=b$ and $0$ if not ? (I am not talking about conditional expressions). Thanks.

Edit : Everything is allowed.

$\endgroup$
22
  • 4
    $\begingroup$ Depends on the allowed functions: $1-\mathrm{sign}|a-b|$ is one solution. $\endgroup$ Oct 27, 2015 at 13:29
  • 5
    $\begingroup$ It's rather obvious that no continuous function $f(a,b)$ will do (thus a function that uses only polynomials or absolute value can't work), so there must be some hidden conditional in the definition of $f$ or a limiting process. Hence, you should clarify what is allowed and what is not. $\endgroup$ Oct 27, 2015 at 13:39
  • 2
    $\begingroup$ @Weber Trigonometric functions alone can't work, as they are either continuous (sine, cosine) or not defined everyhere (tangent). In the latter case, you will end up with a function that is not defined everywhere either, thus can't work on all cases. In the former, it's continuous, whereas your function is not. Thus, either you hide the conditional (sign) either you "push the discontinuity to infinity" with a limit. $\endgroup$ Oct 27, 2015 at 13:54
  • 4
    $\begingroup$ @Weber: Why do you think explicit conditionals are hurting your performance? An explicit conditional is just as much of a "math formula" as any answer you've received, many of which would be implemented with conditionals anyway. And what language are you working in? In a lot of languages, a == b is already 1 if they're equal and 0 otherwise. $\endgroup$ Oct 27, 2015 at 20:06
  • 4
    $\begingroup$ @Weber: Are you trying to get an expression that you can simplify? It's not clear what you mean by having a big chunk of your algorithm "transformed into a math function", or why that would provide any performance benefit. $\endgroup$ Oct 27, 2015 at 20:15

10 Answers 10

16
$\begingroup$

(It's taken me long enough to write this ... I bet some of these appear in other answers.)

  • Using the Iverson bracket: $[a=b]$
  • Using the Kronecker delta (not typically used for reals, except when defining discrete distributions): $\delta_{a b}$
  • Using contour integration (sneakily): $ \liminf_{n \rightarrow \infty} \frac{1}{2\pi}\int_{0}^{2\pi} \mathrm{e}^{\mathrm{i}(a-b)\theta/n} \,\mathrm{d}\theta$
  • Using the indicator function: $\mathbf{1}_{\{a\}}(b)$
  • Using a standard not uniformly continuous (on $[0,1]$) sequence of functions: $\lim_{n \rightarrow \infty} (\min\{a/b,b/a\})^n $
  • Using the Heaviside step function (in disguise, using the Fourier result that the value at the jump is the mean of the limits from each side): $\left. 4(\frac{\mathrm{d}}{\mathrm{d}x} \max\{x,0\})(1-\frac{\mathrm{d}}{\mathrm{d}x} \max\{x,0\}) \right|_{x = a-b}$
  • Using the ceiling function: $1 - \lceil \frac{|a-b|}{|a-b|+1} \rceil$

Edit: Because, of course, one has to get the parity of the last one backwards. Fixed.

Edit: Added limit to contour integral because, sadly, not all pairs of reals have integer differences. And more:

  • We can look at the mean of a certain cosine function: $\lim_{A \rightarrow \infty} \frac{1}{2A} \int_{-A}^A \cos((a-b)x) \,\mathrm{d}x$
  • We can use the sine integral function: $1-\lim_{N \rightarrow \infty}\frac{2}{N\pi} \sum_{n=0}^N \mathrm{Si}(n \, |a-b|)$
  • Iteration: $f^{[1]}(x)=\sqrt{x}$ and $ f^{[n]}(x) = f^{[n-1]} \circ f^{[1]}(x)$. Compute $1-\lim_{n \rightarrow \infty} f^{[n]}(|a-b|)$.
$\endgroup$
5
  • 3
    $\begingroup$ +1 for mentioning the Iverson bracket. Those little braces have made many an expression more efficient, more readable and generally beautiful. $\endgroup$
    – Jake
    Oct 27, 2015 at 17:01
  • $\begingroup$ Well done, but some of your suggestions are “conditional expressions.” $\endgroup$ Oct 27, 2015 at 19:04
  • $\begingroup$ It seems to me that the contour integral one only works for integers. $\endgroup$ Oct 27, 2015 at 20:59
  • $\begingroup$ These are all wonderful... it should be noted for OP that in terms of computation, all of these are waaaaay harder for a computer to perform than a single "==" check $\endgroup$
    – ASKASK
    Oct 28, 2015 at 0:39
  • $\begingroup$ @MeniRosenfeld : D'oh. Umm... All reals are integers, right? :-) $\endgroup$ Oct 28, 2015 at 7:28
11
$\begingroup$

The classic one: $0^{|a-b|}$ ...

Edit: In case you consider $|.|$ as conditional choose $$0^\sqrt{(a-b)^2}$$ instead.

$\endgroup$
8
  • $\begingroup$ Could this one be expressed in another form so the zero is not there any more ? $\endgroup$
    – Weber
    Oct 27, 2015 at 15:24
  • 9
    $\begingroup$ @Weber : $\: (1\hspace{-0.05 in}-\hspace{-0.05 in}1)^{\hspace{.02 in}|\hspace{.02 in}a-b|} \;\;\;\;$ $\endgroup$
    – user57159
    Oct 27, 2015 at 16:06
  • 6
    $\begingroup$ Why not just $0^{(a-b)^2}$? The square root is unnecessary. @Weber $\endgroup$ Oct 27, 2015 at 19:14
  • 5
    $\begingroup$ Wait $0^0$ is 1? I thought it's undefined... $\endgroup$ Oct 27, 2015 at 19:44
  • 2
    $\begingroup$ @TomášZato It's technically undefined, but often defined as 1. $\endgroup$
    – Ypnypn
    Oct 27, 2015 at 20:15
6
$\begingroup$

This one would do, if you allow limits:

$$\lim_{n\to\infty} \exp (-n|a-b|)$$

You could also compute

$$2-\mathrm{Card} \;\{a,b\}$$

Or

$$1-\int_{a-b}^{e(a-b)} \frac{\mathrm{d}x}{x}$$

but I don't think it's very clean to rely on $\int_0^0 f(x) \mathrm dx=0$ when $f$ is not defined at $x=0$.

Another limit: taking $\frac{2}{\pi}\arctan (b-a)$ you get a number in $]-1,1[$, hence you can use the limit $\lim_{n\to\infty} (1-x^2)^n$, which is $1$ if $x=0$ and $0$ if $x\in [-1,1]\backslash\{0\}$. All in all:

$$\lim_{n\to\infty} \left[1-\left(\frac{2}{\pi}\arctan (a-b)\right)^2\right]^n$$

$\endgroup$
0
4
$\begingroup$

You can use this function: $||sgn(b-a)|-1|$

$\endgroup$
0
4
$\begingroup$

How about this where $\lfloor x\rfloor$ is the floor function of $x$:

$\min\{\left\lfloor 2^x\right\rfloor, \left\lfloor 2^{-x}\right\rfloor\}$.

You can even get rid of the min function by recasting this as

$\frac{\left\lfloor 2^x\right\rfloor+ \left\lfloor 2^{-x}\right\rfloor-\mid \left\lfloor 2^x\right\rfloor- \left\lfloor 2^{-x}\right\rfloor\mid}{2}$


Edit: As ASKASK correctly pointed out (thanks), the question asked for an indicator of equality between $a$ and $b$ rather than an input of $x$ being $0$. To make the adjustment, $x$ in the post can be replaced by $a-b$.

$\endgroup$
2
  • $\begingroup$ I think you should at least mention that we are plugging $x=a-b$ into this formula as OP seemed to request a formula in terms of $a$ and $b$ $\endgroup$
    – ASKASK
    Oct 27, 2015 at 14:56
  • $\begingroup$ But otherwise, I really like this formula. To me it seems to be the cleanest. $\endgroup$
    – ASKASK
    Oct 27, 2015 at 14:57
4
$\begingroup$

$\left\lfloor\frac{1}{1+(a-b)^2}\right\rfloor$, where $\lfloor x\rfloor$ - floor function and $a$ and $b$ - real numbers.

$\endgroup$
2
$\begingroup$

$$ \left|\{a\}\cap\{b\}\right| $$

Where $|\cdot|$ denotes the order (number of elements) of a set.

One can come up with many similar examples. For example, $\left|\{(a,b)\}\cap\Delta\right|$, where $\Delta=\{(x,x)\;\colon\;x\in\mathbb R\}\subset\mathbb R\times\mathbb R$.

$\endgroup$
2
$\begingroup$

$\operatorname{sinc}(a\pi-b\pi)$ works for when $a$ and $b$ are integers.

$\endgroup$
1
$\begingroup$

I would use this, but it maybe it is already too conditinal for you:

The indicator function is often used in such cases, so the function takes only two values, $0,1$ in such a way that $$ 1_M(x)=\begin{cases}0,x\text{ is not element of } M \\1,x\text{ is element of } M \end{cases} $$ where M is any set which we choose. So in your case we would take $\{a\}:=M\subset\mathbb{R}$ and get $$ 1_{\{a\}}(x)=\begin{cases}0,x\text{ is not element of } \{a\} \\1,x\text{ is element of } \{a\} \end{cases} $$ meaning $1_{\{a\}}(x)$ equals only $1$ if and only if $x=a$ or in you case $1_{\{a\}}(b)=1$ with $b=a$. Although technically it actually says that it is only an element of an $1$-element set.

$\endgroup$
4
  • $\begingroup$ As I have said in my question, I am not talking about conditional expressions. $\endgroup$
    – Weber
    Oct 27, 2015 at 14:32
  • $\begingroup$ @Weber I know you did...but then I wasn't sure exactly what you by this, for example $\operatorname{sgn}(\cdot)$ or floor/ceil function are off the limits as well? $\endgroup$
    – user190080
    Oct 27, 2015 at 14:35
  • $\begingroup$ All functions are allowed, even integer ones. $\endgroup$
    – Weber
    Oct 27, 2015 at 14:41
  • $\begingroup$ @Weber I got this, I just meant that the above function I gave is as conditional as $\operatorname{sgn}(\cdot)$,ceil/floor functions are - so if you are satisfied with those, you might also take the indicator function into consideration $\endgroup$
    – user190080
    Oct 27, 2015 at 14:44
0
$\begingroup$

Just this: $$ \{a=b\} $$

It evaluates to 1 if the condition inside the curly brackets is true and to 0 if it isn't.

$\endgroup$
4
  • $\begingroup$ This is the Iverson bracket; square braces $[\cdot]$ are normally used, though. $\endgroup$ Oct 27, 2015 at 19:16
  • $\begingroup$ @AkivaWeinberger Hmm, I don't know, it may be different in different countries $\endgroup$ Oct 27, 2015 at 19:17
  • $\begingroup$ It's not nearly old enough for that to happen. Iverson died in 2004, and Knuth is still around. (Iverson introduced it in his programming language APL, but with regular parentheses; Knuth started using it outside of programming, and changed it to square brackets to avoid ambiguity with the grouping parentheses already used in logic.) $\endgroup$ Oct 27, 2015 at 19:22
  • $\begingroup$ @AkivaWeinberger It is possible that we're talking about different things here. I originally meant this expression to be the type of the one used in here near the middle of the page, "Cost Function" section $\endgroup$ Oct 27, 2015 at 19:40

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.