# 艾佛森括号

${\displaystyle [P]={\begin{cases}1&{\text{If }}P{\text{ is true;}}\\0&{\text{Otherwise.}}\end{cases}}}$

## 用途

${\displaystyle \phi (n)=\sum _{i=1}^{n}[\gcd(i,n)=1],\qquad {\text{for }}n\in \mathbb {N} ^{+}.}$

${\displaystyle \sum _{1\leq i\leq 10}i^{2}=\sum _{i}i^{2}[1\leq i\leq 10].}$

${\displaystyle \sum _{1\leq k\leq n \atop \gcd(k,n)=1}\!\!k={\frac {1}{2}}n\varphi (n)}$

${\displaystyle \sum _{1\leq k\leq n \atop \gcd(k,n)=1}\!\!k={\frac {1}{2}}n(\varphi (n)+[n=1])}$

## 样例

${\displaystyle \operatorname {sgn}(x)=[x>0]-[x<0]}$
${\displaystyle H(x)=[x>0].}$

${\displaystyle \max(x,y)=x[x>y]+y[x\leq y],}$
${\displaystyle \min(x,y)=x[x\leq y]+y[x>y],}$
${\displaystyle |x|=x[x\geq 0]-x[x<0].}$

${\displaystyle \lfloor x\rfloor =\sum _{n=-\infty }^{\infty }n[n\leq x
${\displaystyle \lceil x\rceil =\sum _{n=-\infty }^{\infty }n[n-1

${\displaystyle \{x\}=x\cdot [x\geq 0].}$

${\displaystyle [ab]=1.}$

## 注释

1. ^ Ronald Graham, Donald Knuth, and Oren Patashnik. Concrete Mathematics, Section 2.2: Sums and Recurrences.
2. ^ Knuth 1992.