13

I've been trying to create a function that will return $0$ when $x$ is $0$, and for any other $x$ value it should return $1$. I've searched for a pre-existing function online too and wasn't able to find one.

Do you know of any function that can do this?

  • 15
    This will do: $$f(x)=\begin{cases}0,&x=0\{}1,&x\neq 0\end{cases}$$ – DonAntonio Jun 13 '16 at 21:59
  • 3
    Perhaps $f(x)=|\text{sgn}(x)|$ or $f(x)=\text{sgn}(x)^2$ using the absolute value or square of the sign function – Henry Jun 13 '16 at 22:03
  • @David_Shmij: That is a combination of elementary function, namely the constant function $0$ and the constant function $1$ (which are both pretty elementary)! – hmakholm left over Monica Jun 13 '16 at 22:05
  • 1
    @David_Shmij: Which programming language are you talking about here? Most I know will allow you to write either x==0?0:1 or something like if x=0 then 0 else 1, which both seem pretty easy to me. – hmakholm left over Monica Jun 13 '16 at 22:17
  • @David_Shmij: Wolfram's syntax is slightly bizzarre, but you can write Piecewise[{{0,x=0}},1] and then multiply by $5$ to your heart's content. Example here. – hmakholm left over Monica Jun 13 '16 at 23:05
  • If you're using C, tricks like return x != 0, or even return !!x would work, since in C there is no distinction between ints and booleans, but in other languages that make the distinction, you can go with return x == 0 ? 0 : 1 as Henning Makholm suggested. – Pedro A Jun 13 '16 at 23:46
  • I asked GPT3 and it said x/((x*(1/2+(1/π)*atan(|x|)-1/2))/(atan(x)/π)) which is not true but ironically is a way to write sign() function where output is -1 for all negative X values and +1 for all positive X values and undefined for 0. This is pretty interesting and can likely be utilized. For starters, it can be simplified to atan(x)/atan(|x|)=sgn(x) which is pretty neat, granted, off the top of my head, both atan()s can be removed fully.... lol – Albert Renshaw Dec 05 '22 at 02:57

10 Answers10

13

You've already defined your function (assuming you've also chosen its domain).

One of the main ways to "create" a function is simply by specifying its values at all points, and your description has done so.

Typical notation for a function created by the sort of description you give is a definition by cases:

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

For many applications — most applications, I expect — this is one of the best descriptions of said function. If need be, name it with a letter, and continue on with whatever you're doing.


The complementary function

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

which is related to your function by $f(x) = 1 - g(x)$ comes up often enough in some contexts to have been given a name and notation: e.g.

  • The Kronecker delta. A few different notations exist depending on the setting; e.g. $\delta_x$, $\delta[x]$, or $\delta_{x,0}$.
  • The Iverson bracket. This would be notated $[x = 0]$. This notation is, IMO, indispensable for doing complicated calculations with summations.
  • x == 0 computes this function in C and C++, and many other programming languages allow similar.

Some applications might want to represent such a function in particular ways. For example, if one only cares about the value of $g(x)$ when $x$ is an integer, but strongly prefers to work with analytic functions (e.g. because you're studying a sequence using complex analysis), one has the fact that

$$ g(x) = \mathop{\mathrm{sinc}}(\pi x) $$

holds whenever $x$ is an integer.

(if you're unfamiliar with it, $\mathop{\mathrm{sinc}}(z)$ is the continuous extension of $\sin(z) / z$)

  • 1
    In some settings, $0^x$ could be used as another notation for this, but would likely look out of place. –  Jun 13 '16 at 22:45
  • 2
    $0^x$ would be iffy at best if $x$ can be negative ... – hmakholm left over Monica Jun 13 '16 at 23:07
  • +1 Thanks, this is a resourceful answer because you referenced Kronecker Delta which was great to read about and see some of the sigma-notated and integral-notated versions of it. As for the piece-wise function, unfortunately I'm working on applying this function into a larger function to simplify the super-function, I can't simplify using a piece-wise function because identities don't exist for custom piece-wise functions and therefor it's hard to cancel stuff out. – Albert Renshaw Jun 14 '16 at 04:43
  • Also for the sinc(πx) only wokring when x is an integer, we can just use floor or ceiling function on x to get it working for all values of x, that's a great answer too for what I'm trying to do except when I look at the definition of sinc, it seems to be piece-wise too, doh – Albert Renshaw Jun 14 '16 at 04:44
  • @Henning, since you mention it: have you already seen Knuth's article on notations? There is a mention of the interesting history of $0^x$ in there... – J. M. ain't a mathematician Jun 15 '16 at 18:04
  • @JM: I can't say I have -- but a priori I find it difficult to believe that "Knuth's article on notations" describes a unique paper ... – hmakholm left over Monica Jun 15 '16 at 23:49
  • @Henning, I had this in mind, where he discusses Iverson brackets. – J. M. ain't a mathematician Jun 16 '16 at 00:40
8

How about $f(x)=\left\lceil\frac{x^2}{x^2+1}\right\rceil$

*Works for real numbers, with imaginary numbers you may divide by 0.

paw88789
  • 41,907
  • 3
    That's pretty unreadable compared to Joanpemo's straightforward definition. – hmakholm left over Monica Jun 13 '16 at 22:07
  • The function you gave is the case of when x <= 0! You need to multiply it with another term. Just multiply it with f(-x). In terms of Boolean manipulation, this would be the same as ANDing the set of x <= 0 with the set -x <= 0. That should fix your problem. – user64742 Jun 13 '16 at 22:14
  • 3
    @TheGreatDuck The function is even. So there is no difference between the case $x\le 0$ and the case $x\ge 0$. –  Jun 13 '16 at 22:17
  • 2
    @G. Sassatelli You are correct. I am used to using x on top when defining iverson brackets with floor. I didn't look carefully enough. Oops. :p – user64742 Jun 13 '16 at 22:42
  • @Barry Cipra, Thanks. Yes, you're right. I made the function to be $1$ at $0$ and $0$ elsewhere. I will fix it. – paw88789 Jun 13 '16 at 23:16
  • 1
    This is exactly what I needed, thank you! – Albert Renshaw Jun 14 '16 at 04:41
  • Actually I realized this won't work for all complex values, you will end up dividing by 0 sometimes. I was able to create a function inspired by this that does work for all values of x, here: http://math.stackexchange.com/a/1825355/56935 - Thanks again for all of your help though, this answer helped me find the solution I needed! – Albert Renshaw Jun 14 '16 at 04:54
  • I'm setting this back to my accepted answer as it's more portable; I prefer a closed form but at the end of the day ceiling function actually still works for my use-case. – Albert Renshaw Nov 04 '20 at 23:57
4

How about $f(x)= 1-\delta_{x,0}$ (using the Kronecker Delta function, in Mathematica/WolframAlpha can write the $\delta_{x,0}$ as

kroneckerdelta(x,0)

)

2

Here's one using $\sum$ notation although it only works for the natural numbers:

$f(x) = \sum\limits_{i = 1}^{x}{\frac{1}{x}} $

Due to the empty sum being 0.

It can't be simplified to $f(x) = x \times \frac{1}{x}$ because then f(0) would be undefined.

omer
  • 149
  • 1
    I think this satisfies the 0 when x=0 part but what about the 1 otherwise part? Is there something I do, with x, to f(x), after to get to 1 every time? – Albert Renshaw Oct 21 '18 at 04:12
  • 1
    When $x = 1$ it's just $\frac{1}{1}$, when $x = 2$ it's $\frac{1}{2} + \frac{1}{2}$ which is $\frac{2}{2}$ which is 1, in general it's $\frac{1}{n} + \frac{1}{n} + ... + \frac{1}{n}$ repeated $n$ times, which is simply $\frac{n}{n}$ which is $1$ – omer Oct 21 '18 at 16:23
  • Oh I'm sorry! I wrote $n$ instead of $x$. I'll edit it. Should make more sense now – omer Oct 21 '18 at 16:35
2

You can use the fact that $0^0$ is equal to $1$ and $0$ raised to any other positive power is $0$ itself. So, the function would be:$$f(x)=1-0^{|x|}$$ When x is $0$, $1-0^x = 1-0^0 = 1-1 = 0$ while for any other positive or negative number it would evaluate to $1-0 = 1$. Hope this helps!

  • This seems enticing; but $0^0$ is undefined! – Albert Renshaw Mar 02 '23 at 07:52
  • 1
    @AlbertRenshaw That is a debatable topic. Some assume it is 1 (like Google calculator) while others argue it is undefined. But, if the OP feels $0^0$ is 1, then my answer is valid. Otherwise, others have also given valid answers :) – Shubham Singh Mar 02 '23 at 14:28
0

Paw88789's answer worked great for what I'm trying to do; the only issue was that it didn't work for all "numbers"; some imaginary numbers would cause division by 0. Luckily tonight I was able to create a function that produced the desired result extended to the range of numbers I need to work with

$f(x) = \left \lceil \frac{1}{2\Gamma \left ( \left | x \right | \right )} \right \rceil$

*Note the absolute value inside of Gamma, as it's easy to go unnoticed

0

Without using floor or ceiling it can be done with limits. Start with a function of this form:

$$\lim_{c \to \infty} \left (\frac{y}{\sqrt2}-\frac{(x\cdot c)}{\sqrt2} = \sqrt[3]{1-\left ( \frac{y}{\sqrt2}+\frac{(x\cdot c)}{\sqrt2} \right )^3 } \right )$$

(Note: Using real-valued root not principal root)


This infinitely squashes (along x-axis) a 45º rotated graph of the form $x^3+y^3=1$ which causes the output values to be $0$ everywhere and $\sqrt[6]{2}$ at $0$. This can now easily be worked to achieve the desired affect by dividing by $\sqrt[6]{2}$ and subtracting from $1$

0

The following equation I’ve made works without using ceiling function or limits or infinite sums when plotting the real values. 1 at x=0 and 0 and a complex pair counterpart at all other values for x

$(y-1-\sqrt{x}-\sqrt{-x})\cdot\frac{y}{y-x}=0$

This can then be inverted to a less elegant form to have 0 for x=0 and 1 in all other places.

I like this best because it’s pure closed form expression and doesn’t involve any elements like ceiling function that are hard to work with when using this in other places. However it only works if you ignore complex answers which has problems of its own, but still is noteworthy and has application

0

The following equation I've made is a closed-form expression of KroneckerDelta (j=0 form) which will evaluate y=1 at x=0 and y=0 for x≠0.

$$(\frac{y}{y-x})\cdot((y-1)^2+x^2)=0$$

This function can be subtracted from 1 to achieve the desired result


The function was formed by taking the function for a horizontal line at 0, divided by a 45º line (y=x) through the origin to create a hole in the function at x=0, then multiplied by a circle with radius 0 whose origin is at (0,1) to create a point at (0,1).

0

An approximation of your desired function be of this form: $$f(x) = \sqrt[10^{100}]{\lvert x \rvert}$$ Where $f(1\cdot10^{-50}) = 1$ while $f(0) = 0$.

Timo
  • 101