0

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.

Weber
  • 127
  • 4
    Depends on the allowed functions: $1-\mathrm{sign}|a-b|$ is one solution. – gammatester Oct 27 '15 at 13:29
  • 5
    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. – Jean-Claude Arbaut Oct 27 '15 at 13:39
  • Everything is allowed (Preferably trigonometric functions) – Weber Oct 27 '15 at 13:45
  • 2
    @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. – Jean-Claude Arbaut Oct 27 '15 at 13:54
  • If you dont mind having infinite number of trignometric terms, you can use fourier series representation of the delta function. – Bhaskar Oct 27 '15 at 14:27
  • @Jean-Claude Feel free to suggest anything. – Weber Oct 27 '15 at 14:51
  • Just curious, what is your motivation behind this question? – JiK Oct 27 '15 at 15:49
  • @JiK I am a programmer and I am trying to leverage some heavy algorithms by mathematical shortcut functions – Weber Oct 27 '15 at 16:51
  • @ L16H7 Would you give an example using Fourier series ? – Weber Oct 27 '15 at 16:55
  • @Weber It's really easy to use a Fourier series : take the sign function on [-1,1], complete by periodicity, compute its Fourier series $S(x)$ and compute $1-|S(2/\pi \arctan(b-a))|$. – Jean-Claude Arbaut Oct 27 '15 at 17:33
  • $$\left\lfloor\frac1{(a-b)^2+1}\right\rfloor$$ – Akiva Weinberger Oct 27 '15 at 19:24
  • 1
    Why are explicit conditionals excluded from "everything"? They're an entirely natural way to write things like this, and functions expressed with conditionals aren't somehow "cheating" or "unmathematical". – user2357112 Oct 27 '15 at 19:58
  • @user2357112 As I said before, I am a programmer and explicit conditionals are what I am already using in my code. Now I want to make use of some math formulas to reduce processing time. – Weber Oct 27 '15 at 20:02
  • 4
    @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. – user2357112 Oct 27 '15 at 20:06
  • @user2357112 It's not a == b that I am trying to optimize , but rather a big chunk of my algorithm will be transformed to a math function. The question I asked about the equality is just a step needed to complete the transformation. – Weber Oct 27 '15 at 20:10
  • 4
    @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. – user2357112 Oct 27 '15 at 20:15
  • @user2357112 Here is a simple example : Imagine you want to find the sum of the first n natural numbers, so the naif implementation would be (for(int i =1 ;i<=n; i++) S += i;) now compare that algorithm with the equivalent math formula ( S = (n+n^2)/2) . try to execute both codes for n = 1000000 or more ! – Weber Oct 27 '15 at 20:25
  • @Weber: Now we're getting somewhere. What you want isn't a "math formula"; you're looking for a closed form. Unfortunately, there aren't really any useful transformations you can apply to a == b. All the answers you've gotten are notational differences or ways to complicate things. None of this will help you; you need to analyze your problem from a higher-level perspective. – user2357112 Oct 27 '15 at 20:47
  • @user2357112 You are right about the closed form, I wonder if we can find a systematic method to transform any algorithm to a closed form. – Weber Oct 27 '15 at 20:59
  • 1
    @Weber: Nope. A lot of work has gone into noping that from a variety of directions, such as the halting problem and the time hierarchy theorem. – user2357112 Oct 27 '15 at 21:03
  • Weber, if you are just trying to optimize code, then either use conditionals, or use specific assembly instructions (like sete or cmove on x86). None of the formulas below is likely to be faster. – Jean-Claude Arbaut Oct 28 '15 at 09:41
  • @Jean-ClaudeArbaut A whelk's chance in a supernova. – Daniel Fischer Oct 28 '15 at 14:37

10 Answers10

16

(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|)$.
Eric Towers
  • 67,037
11

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

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

Michael Hoppe
  • 18,103
  • 3
  • 32
  • 49
6

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

4

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

BLAZE
  • 8,458
Soheil
  • 41
4

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

paw88789
  • 40,402
  • 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$ – ASKASK Oct 27 '15 at 14:56
  • But otherwise, I really like this formula. To me it seems to be the cleanest. – ASKASK Oct 27 '15 at 14:57
4

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

Somnium
  • 1,567
  • 1
  • 13
  • 27
2

$$ \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$.

John Gowers
  • 24,959
2

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

1

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.

user190080
  • 3,701
  • As I have said in my question, I am not talking about conditional expressions. – Weber Oct 27 '15 at 14:32
  • @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? – user190080 Oct 27 '15 at 14:35
  • All functions are allowed, even integer ones. – Weber Oct 27 '15 at 14:41
  • @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 – user190080 Oct 27 '15 at 14:44
0

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

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

  • This is the Iverson bracket; square braces $[\cdot]$ are normally used, though. – Akiva Weinberger Oct 27 '15 at 19:16
  • @AkivaWeinberger Hmm, I don't know, it may be different in different countries – Maks Maisak Oct 27 '15 at 19:17
  • 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.) – Akiva Weinberger Oct 27 '15 at 19:22
  • @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 – Maks Maisak Oct 27 '15 at 19:40