12
$\begingroup$

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?

$\endgroup$
7
  • 14
    $\begingroup$ This will do: $$f(x)=\begin{cases}0,&x=0\\{}1,&x\neq 0\end{cases}$$ $\endgroup$
    – DonAntonio
    Jun 13, 2016 at 21:59
  • 3
    $\begingroup$ Perhaps $f(x)=|\text{sgn}(x)|$ or $f(x)=\text{sgn}(x)^2$ using the absolute value or square of the sign function $\endgroup$
    – Henry
    Jun 13, 2016 at 22:03
  • $\begingroup$ @David_Shmij: That is a combination of elementary function, namely the constant function $0$ and the constant function $1$ (which are both pretty elementary)! $\endgroup$ Jun 13, 2016 at 22:05
  • 1
    $\begingroup$ @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. $\endgroup$ Jun 13, 2016 at 22:17
  • $\begingroup$ @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. $\endgroup$ Jun 13, 2016 at 23:05

10 Answers 10

13
$\begingroup$

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

$\endgroup$
7
  • 1
    $\begingroup$ In some settings, $0^x$ could be used as another notation for this, but would likely look out of place. $\endgroup$
    – user14972
    Jun 13, 2016 at 22:45
  • 2
    $\begingroup$ $0^x$ would be iffy at best if $x$ can be negative ... $\endgroup$ Jun 13, 2016 at 23:07
  • $\begingroup$ +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. $\endgroup$ Jun 14, 2016 at 4:43
  • $\begingroup$ 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 $\endgroup$ Jun 14, 2016 at 4:44
  • $\begingroup$ @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... $\endgroup$ Jun 15, 2016 at 18:04
8
$\begingroup$

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.

$\endgroup$
8
  • 3
    $\begingroup$ That's pretty unreadable compared to Joanpemo's straightforward definition. $\endgroup$ Jun 13, 2016 at 22:07
  • $\begingroup$ 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. $\endgroup$
    – user64742
    Jun 13, 2016 at 22:14
  • 3
    $\begingroup$ @TheGreatDuck The function is even. So there is no difference between the case $x\le 0$ and the case $x\ge 0$. $\endgroup$
    – user228113
    Jun 13, 2016 at 22:17
  • 2
    $\begingroup$ @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 $\endgroup$
    – user64742
    Jun 13, 2016 at 22:42
  • 1
    $\begingroup$ This is exactly what I needed, thank you! $\endgroup$ Jun 14, 2016 at 4:41
4
$\begingroup$

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)

)

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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? $\endgroup$ Oct 21, 2018 at 4:12
  • 1
    $\begingroup$ 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$ $\endgroup$
    – omer
    Oct 21, 2018 at 16:23
  • $\begingroup$ Oh I'm sorry! I wrote $n$ instead of $x$. I'll edit it. Should make more sense now $\endgroup$
    – omer
    Oct 21, 2018 at 16:35
2
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ This seems enticing; but $0^0$ is undefined! $\endgroup$ Mar 2, 2023 at 7:52
  • 1
    $\begingroup$ @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 :) $\endgroup$ Mar 2, 2023 at 14:28
0
$\begingroup$

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$

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .