# What mathematical function would do this: if $x = 0$ then $y = 0$ but if $x > 0$ then $y = 1$?

$x = 0$, $f(x) = 0$

$x = 1$, $f(x) = 1$

$x = 2$, $f(x) = 1$

$x = 3$, $f(x) = 1$

...

There have been so many times I could have used this at different programming problems but I always resorted to logical expressions. I feel that there should be a more elegant, purely mathematical, solution.

-
Your description in the title is perfectly mathematical. There is nothing non-mathematical about "f(x) = 0 if x = 0, and f(x) = 1 for x > 0". If you want you can use Iverson notation and write it as $f(x) = [x>0]$, or as an indicator variable $f(x) = 1_{\{x > 0\}}$, but ultimately they're all ways of saying the same thing. (Questions like this have come up before multiple times on this site; maybe someone can find the duplicates.) – ShreevatsaR May 30 '12 at 10:57
See sign function at Wikipedia. – Martin Sleziak May 30 '12 at 11:05
Your description even has a name: Heaviside step function, though you're actually describing $H(n-1)$. – lhf May 30 '12 at 11:18
In C you can just write (x>0) since Boolean values are actually the numeric values 0 and 1. – lhf May 30 '12 at 11:21
Yes Davis here has written a mathematical function here. However, oftentimes people think "function" means "formula" as in an equation (I think historians of mathematics believe that many respected authors thought of functions this way also... including Euler). So, Davis probably wants a formula like those referenced by lhf. – Doug Spoonwood May 30 '12 at 13:26

Unfortunately, checking my typical python and java packages (the only languages I work in now), I exception handle NaN cases. So my original post wouldn't work unless you also handle them.

If you're looking for a good way to code this, then you might try either

if x > 0: return 1 return 0

or, if you're in a language which evaluates True to 1, False to 0, something like

return int(x>0)

But in terms of a mathematical function, your description alone is a mathematical function. There is no ambiguity about $f:[0,\infty] \to \mathbb{R}$, $f(x) = 0$ if $x = 0$, and $f(x) = 1$ else.

-
I don't know a single programming language where 0/0 is evaluated as 0, and this is a question about math, not programming... – Najib Idrissi May 30 '12 at 11:16
@N.I To quote the OP: There have been so many times I could have used this at different programming problems but I always resorted to logical expressions. I feel that there should be a more elegant, purely mathematical, solution. – mixedmath May 30 '12 at 11:18
Can you give an example of a language which evalutes 0/0 as 0? The common ways to express OP's function in programming languages is !!x or int(bool(x)), but the clearest and most readable way is 1 if x>0 else 0. – Samuel May 30 '12 at 11:22
@Samuel: No - you're right. I was thinking of the setup I use in python. It turns out that I have packages that exception handle 0/0 to be 0, and in fact have manually shortcircuited some division myself. Without that, I can't think of anything nicer than what you wrote. – mixedmath May 30 '12 at 11:27
In some languages you can also use the nice and short bool ? iftrue : iffalse construction, i.e. something like return (x > 0) ? 1 : 0. – TMM May 30 '12 at 17:42

Apparently you described the sign function. However, if your problem is to interpolate points, you can use Lagrange polynomials. Given abscissae $(x_1,...,x_n)$ with corresponding ordinates $(y_1,...,y_n)$, the Lagrange polynomial $$L(X) = \sum_{i=1}^n y_i l_i(X)$$ with $$l_i(X) = \prod_{j=1,j\neq i}^n \frac{X-x_j}{x_i-x_j}$$ satisfies $\forall i =1,..,n$ $L(x_i)=y_i$. Such polynomial is unique, given the latter set of abscisase and ordinates.

In your example with points $(0,0), (1,1), (2,1), (3,1)$ this leads to $$L(X) = X\left(\frac{1}{6}X^2 - X + \frac{11}{6}\right)$$

-
I really doubt OP's problem is to interpolate points. – anon May 30 '12 at 17:30
I doubt it too. However he used the interpolation tag and was apparently trying to find another mathematical writting for his function. – vanna May 30 '12 at 17:35