# Iverson bracket

In mathematics, the Iverson bracket, named after Kenneth E. Iverson, is a notation that generalises the Kronecker delta, which is the Iverson bracket of the statement x = y. It maps any statement to a function of the free variables in that statement. This function is defined to take the value 1 for the values of the variables for which the statement is true, and takes the value 0 otherwise. It is generally denoted by putting the statement inside square brackets: $[P]={\begin{cases}1&{\text{if }}P{\text{ is true;}}\\0&{\text{otherwise.}}\end{cases}}$ In other words, the Iverson bracket of a statement is the indicator function of the set of values for which the statement is true.
The Iverson bracket allows using capital-sigma notation without restriction on the summation index. That is, for any property $P(k)$ of the integer $k$, one can rewrite the restricted sum $\sum _{k:P(k)}f(k)$ in the unrestricted form $\sum _{k}f(k)\cdot [P(k)]$. With this convention, $f(k)$ does not need to be defined for the values of k for which the Iverson bracket equals 0; that is, a summand $f(k)[{\textbf {false}}]$ must evaluate to 0 regardless of whether $f(k)$ is defined.
The notation was originally introduced by Kenneth E. Iverson in his programming language APL, though restricted to single relational operators enclosed in parentheses, while the generalisation to arbitrary statements, notational restriction to square brackets, and applications to summation, was advocated by Donald Knuth to avoid ambiguity in parenthesized logical expressions.

## Properties

There is a direct correspondence between arithmetic on Iverson brackets, logic, and set operations. For instance, let A and B be sets and $P(k_{1},\dots )$ any property of integers; then we have
${\begin{aligned}[\,P\land Q\,]~&=~[\,P\,]\,[\,Q\,]~~;\\[1em][\,P\lor Q\,]~&=~[\,P\,]\;+\;[\,Q\,]\;-\;[\,P\,]\,[\,Q\,]~~;\\[1em][\,\neg \,P\,]~&=~1-[\,P\,]~~;\\[1em][\,P{\scriptstyle {\mathsf {\text{ XOR }}}}Q\,]~&=~{\Bigl |}\,[\,P\,]\;-\;[\,Q\,]\,{\Bigr |}~~;\\[1em][\,k\in A\,]\;+\;[\,k\in B\,]~&=~[\,k\in A\cup B\,]\;+\;[\,k\in A\cap B\,]~~;\\[1em][\,x\in A\cap B\,]~&=~[\,x\in A\,]\,[\,x\in B\,]~~;\\[1em][\,\forall \,m\ :\,P(k,m)\,]~&=~\prod _{m}\,[\,P(k,m)\,]~~;\\[1em][\,\exists \,m\ :\,P(k,m)\,]~&=~\min {\Bigl \\;1\,,\,\sum _{m}\,[\,P(k,m)\,]\;{\Bigr \}}=1\;-\;\prod _{m}\,[\,\neg \,P(k,m)\,]~~;\\[1em]\#{\Bigl \\;m\,{\Big |}\,P(k,m)\;{\Bigr \}}~&=~\sum _{m}\,[\,P(k,m)\,]~~.\end{aligned}}$

## Examples

The notation allows moving boundary conditions of summations (or integrals) as a separate factor into the summand, freeing up space around the summation operator, but more importantly allowing it to be manipulated algebraically.

### Double-counting rule

We mechanically derive a well-known sum manipulation rule using Iverson brackets: ${\begin{aligned}\sum _{k\in A}f(k)+\sum _{k\in B}f(k)&=\sum _{k}f(k)\,[k\in A]+\sum _{k}f(k)\,[k\in B]\\&=\sum _{k}f(k)\,([k\in A]+[k\in B])\\&=\sum _{k}f(k)\,([k\in A\cup B]+[k\in A\cap B])\\&=\sum _{k\in A\cup B}f(k)\ +\sum _{k\in A\cap B}f(k).\end{aligned}}$

### Summation interchange

The well-known rule ∑$_{j=1}$$^n$∑$_{k=1}$$^j$f(j, k)=∑$_{k=1}$$^n$∑$_{j=k}$$^n$f(j, k){\textstyle \sum _{j=1}^{n}\sum _{k=1}^{j}f(j, k)=\sum _{k=1}^{n}\sum _{j=k}^{n}f(j, k)} is likewise easily derived: ${\begin{aligned}\sum _{j=1}^{n}\,\sum _{k=1}^{j}f(j,k)&=\sum _{j,k}f(j,k)\,[1\leq j\leq n]\,[1\leq k\leq j]\\&=\sum _{j,k}f(j,k)\,[1\leq k\leq j\leq n]\\&=\sum _{j,k}f(j,k)\,[1\leq k\leq n]\,[k\leq j\leq n]\\&=\sum _{k=1}^{n}\,\sum _{j=k}^{n}f(j,k).\end{aligned}}$

### Counting

For instance, Euler's totient function that counts the number of positive integers up to n which are coprime to n can be expressed by $\varphi (n)=\sum _{i=1}^{n}[\gcd(i,n)=1],\qquad {\text{for }}n\in \mathbb {N} ^{+}.$

### Simplification of special cases

Another use of the Iverson bracket is to simplify equations with special cases. For example, the formula $\sum _{1\leq k\leq n \atop \gcd(k,n)=1}\!\!k={\frac {1}{2}}n\varphi (n)$
is valid for n > 1 but is off by ⁠1/2⁠ for n = 1. To get an identity valid for all positive integers n (i.e., all values for which $\varphi (n)$ is defined), a correction term involving the Iverson bracket may be added: $\sum _{1\leq k\leq n \atop \gcd(k,n)=1}\!\!k={\frac {1}{2}}n{\Big\varphi (n)+[n=1]{\Big )}$

### Common functions

Many common functions, especially those with a natural piecewise definition, may be expressed in terms of the Iverson bracket. The Kronecker delta notation is a specific case of Iverson notation when the condition is equality. That is, $\delta _{ij}=[i=j].$
The indicator function of a set $A$, often denoted $\mathbf {1} _{A}(x)$, $\mathbf {I} _{A}(x)$ or $\chi _{A}(x)$, is an Iverson bracket with set membership as its condition: $\mathbf {I} _{A}(x)=[x\in A].$
The Heaviside step function, sign function, and absolute value function are also easily expressed in this notation: ${\begin{aligned}H(x)&=[x\geq 0],\\\operatorname {sgn}(x)&=[x>0]-[x<0],\end{aligned}}$
and ${\begin{aligned}|x|&=x[x>0]-x[x<0]\\&=x([x>0]-[x<0])\\&=x\cdot \operatorname {sgn}(x).\end{aligned}}$
The comparison functions max and min (returning the larger or smaller of two arguments) may be written as $\max(x,y)=x[x>y]+y[x\leq y]$ and $\min(x,y)=x[x\leq y]+y[x>y].$
The floor and ceiling functions can be expressed as $\lfloor x\rfloor =\sum _{n}n\cdot [n\leq x<n+1]$ and $\lceil x\rceil =\sum _{n}n\cdot [n-1<x\leq n],$ where the index $n$ of summation is understood to range over all the integers.
The ramp function can be expressed $R(x)=x\cdot [x\geq 0].$
The trichotomy of the reals is equivalent to the following identity: $[a<b]+[a=b]+[a>b]=1.$
The Möbius function has the property (and can be defined by recurrence as) $\sum _{d|n}\mu (d)\ =\ [n=1].$

## Formulation in terms of usual functions

In the 1830s, Guglielmo dalla Sommaja used the expression $0^{0^{x}}$ to represent what now would be written $[x>0]$; he also used variants, such as $\left(1-0^{0^{-x}}\right)\left(1-0^{0^{x-a}}\right)$ for $[0\leq x\leq a]$. Following one common convention (that $0^{0}=1$), those quantities are equal where defined: $0^{0^{x}}$ is 1 if x > 0, is 0 if x = 0, and is undefined otherwise.

## Notational variations

In addition to the now-standard square brackets [ · ] , and the original parentheses ( · ) , blackboard bold brackets have also been used, e.g. ⟦ · ⟧ , as well as other unusual forms of bracketing marks available in the publisher's typeface, accompanied by a marginal note.