# Linear code

In coding theory, a linear code is an error-correcting code for which any linear combination of codewords is also a codeword. Linear codes are traditionally partitioned into block codes and convolutional codes, although turbo codes can be seen as a hybrid of these two types. Linear codes allow for more efficient encoding and decoding algorithms than other codes (cf. syndrome decoding).
Linear codes are used in forward error correction and are applied in methods for transmitting symbols (e.g., bits) on a communications channel so that, if errors occur in the communication, some errors can be corrected or detected by the recipient of a message block. The codewords in a linear block code are blocks of symbols that are encoded using more symbols than the original value to be sent. A linear code of length n transmits blocks containing n symbols. For example, the [7,4,3] Hamming code is a linear binary code which represents 4-bit messages using 7-bit codewords. Two distinct codewords differ in at least three bits. As a consequence, up to two errors per codeword can be detected while a single error can be corrected. This code contains 2$^4$ = 16 codewords.

## Definition and parameters

A linear code of length n and dimension k is a linear subspace C with dimension k of the vector space $\mathbb {F} _{q}^{n}$ where $\mathbb {F} _{q}$ is the finite field with q elements. Such a code is called a q-ary code. If q = 2 or q = 3, the code is described as a binary code, or a ternary code respectively. The vectors in C are called codewords. The size of a code is the number of codewords and equals q$^k$.
The weight of a codeword is the number of its elements that are nonzero and the distance between two codewords is the Hamming distance between them, that is, the number of elements in which they differ. The distance d of the linear code is the minimum weight of its nonzero codewords, or equivalently, the minimum distance between distinct codewords. A linear code of length n, dimension k, and distance d is called an [n,k,d] code (or, more precisely, $[n,k,d]_{q}$ code).
We want to give $\mathbb {F} _{q}^{n}$ the standard basis because each coordinate represents a "bit" that is transmitted across a "noisy channel" with some small probability of transmission error (a binary symmetric channel). If some other basis is used then this model cannot be used and the Hamming metric does not measure the number of errors in transmission, as we want it to.

## Generator and check matrices

As a linear subspace of $\mathbb {F} _{q}^{n}$, the entire code C (which may be very large) may be represented as the span of a set of $k$ codewords (known as a basis in linear algebra). These basis codewords are often collated in the rows of a matrix G known as a generating matrix for the code C. When G has the block matrix form ${\boldsymbol {G}}=[I_{k}\mid P]$, where $I_{k}$ denotes the $k\times k$ identity matrix and P is some $k\times (n-k)$ matrix, then we say G is in standard form.
A matrix H representing a linear function $\phi :\mathbb {F} _{q}^{n}\to \mathbb {F} _{q}^{n-k}$ whose kernel is C is called a check matrix of C (or sometimes a parity check matrix). Equivalently, H is a matrix whose null space is C. If C is a code with a generating matrix G in standard form, ${\boldsymbol {G}}=[I_{k}\mid P]$, then ${\boldsymbol {H}}=[-P^{T}\mid I_{n-k}]$ is a check matrix for C. The code generated by H is called the dual code of C. It can be verified that G is a $k\times n$ matrix, while H is a $(n-k)\times n$ matrix.
Linearity guarantees that the minimum Hamming distance d between a codeword c$_0$ and any of the other codewords c ≠ c$_0$ is independent of c$_0$. This follows from the property that the difference c − c$_0$ of two codewords in C is also a codeword (i.e., an element of the subspace C), and the property that d(c, c$_0$) = d(c − c$_0$, 0). These properties imply that
    $\min _{c\in C,\ c\neq c_{0}}d(c,c_{0})=\min _{c\in C,\ c\neq c_{0}}d(c-c_{0},0)=\min _{c\in C,\ c\neq 0}d(c,0)=d.$
In other words, in order to find out the minimum distance between the codewords of a linear code, one would only need to look at the non-zero codewords. The non-zero codeword with the smallest weight has then the minimum distance to the zero codeword, and hence determines the minimum distance of the code.
The distance d of a linear code C also equals the minimum number of linearly dependent columns of the check matrix H.
Proof: Because ${\boldsymbol {H}}\cdot {\boldsymbol {c}}^{T}={\boldsymbol {0}}$, which is equivalent to $\sum _{i=1}^{n}(c_{i}\cdot {\boldsymbol {H_{i}}})={\boldsymbol {0}}$, where ${\boldsymbol {H_{i}}}$ is the $i^{th}$ column of ${\boldsymbol {H}}$. Remove those items with $c_{i}=0$, those ${\boldsymbol {H_{i}}}$ with $c_{i}\neq 0$ are linearly dependent. Therefore, $d$ is at least the minimum number of linearly dependent columns. On another hand, consider the minimum set of linearly dependent columns $\{{\boldsymbol {H_{j}}}\mid j\in S\}$ where $S$ is the column index set. $\sum _{i=1}^{n}(c_{i}\cdot {\boldsymbol {H_{i}}})=\sum _{j\in S}(c_{j}\cdot {\boldsymbol {H_{j}}})+\sum _{j\notin S}(c_{j}\cdot {\boldsymbol {H_{j}}})={\boldsymbol {0}}$. Now consider the vector ${\boldsymbol {c'}}$ such that $c_{j}'=0$ if $j\notin S$. Note ${\boldsymbol {c'}}\in C$ because ${\boldsymbol {H}}\cdot {\boldsymbol {c'}}^{T}={\boldsymbol {0}}$ . Therefore, we have $d\leq wt({\boldsymbol {c'}})$, which is the minimum number of linearly dependent columns in ${\boldsymbol {H}}$. The claimed property is therefore proven.

## Example: Hamming codes

As the first class of linear codes developed for error correction purpose, Hamming codes have been widely used in digital communication systems. For any positive integer $r\geq 2$, there exists a $[2^{r}-1,2^{r}-r-1,3]_{2}$ Hamming code. Since $d=3$, this Hamming code can correct a 1-bit error.
Example : The linear block code with the following generator matrix and parity check matrix is a $[7,4,3]_{2}$ Hamming code.
    ${\boldsymbol {G}}={\begin{pmatrix}1&0&0&0&1&1&0\\0&1&0&0&0&1&1\\0&0&1&0&1&1&1\\0&0&0&1&1&0&1\end{pmatrix}},$ ${\boldsymbol {H}}={\begin{pmatrix}1&0&1&1&1&0&0\\1&1&\ 1&0&0&1&0\\0&1&1&1&0&0&1\end{pmatrix}}$

## Example: Hadamard codes

Hadamard code is a $[2^{r},r,2^{r-1}]_{2}$ linear code and is capable of correcting many errors. Hadamard code could be constructed column by column : the $i^{th}$ column is the bits of the binary representation of integer $i$, as shown in the following example. Hadamard code has minimum distance $2^{r-1}$ and therefore can correct $2^{r-2}-1$ errors.
Example: The linear block code with the following generator matrix is a $[8,3,4]_{2}$ Hadamard code: ${\boldsymbol {G}}_{\mathrm {Had} }={\begin{pmatrix}0&0&0&0&1&\ 1&1&1\\0&0&1&1&0&0&1&1\\0&1&0&1&0&1&0&1\end{pmatrix}}$.
Hadamard code is a special case of Reed–Muller code. If we take the first column (the all-zero column) out from ${\boldsymbol {G}}_{\mathrm {Had} }$, we get $[7,3,4]_{2}$ simplex code, which is the dual code of Hamming code.

## Nearest neighbor algorithm

The parameter d is closely related to the error correcting ability of the code. The following construction/algorithm illustrates this (called the nearest neighbor decoding algorithm):
Input: A received vector v in $\mathbb {F} _{q}^{n}.$
Output: A codeword $w$ in $C$ closest to $v$, if any.
* Starting with $t=0$, repeat the following two steps.
* Enumerate the elements of the ball of (Hamming) radius $t$ around the received word $v$, denoted $B_{t}(v)$.
** For each $w$ in $B_{t}(v)$, check if $w$ in $C$. If so, return $w$ as the solution.
* Increment $t$. Fail only when $t>(d-1)/2$ so enumeration is complete and no solution has been found.
We say that a linear $C$ is $t$-error correcting if there is at most one codeword in $B_{t}(v)$, for each $v$ in $\mathbb {F} _{q}^{n}$.

## Popular notation

Codes in general are often denoted by the letter C, and a code of length n and of rank k (i.e., having n code words in its basis and k rows in its generating matrix) is generally referred to as an (n, k) code. Linear block codes are frequently denoted as [n, k, d] codes, where d refers to the code's minimum Hamming distance between any two code words.
(The [n, k, d] notation should not be confused with the (n, M, d) notation used to denote a non-linear code of length n, size M (i.e., having M code words), and minimum Hamming distance d.)

## Singleton bound

Lemma (Singleton bound): Every linear [n,k,d] code C satisfies $k+d\leq n+1$.
A code C whose parameters satisfy k +d = n + 1 is called maximum distance separable or MDS. Such codes, when they exist, are in some sense best possible.
If C$_1$ and C$_2$ are two codes of length n and if there is a permutation p in the symmetric group S$_n$ for which (c$_1$,...,c$_n$) in C$_1$ if and only if (c$_{p(1)}$,...,c$_{p(n)}$) in C$_2$, then we say C$_1$ and C$_2$ are permutation equivalent. In more generality, if there is an $n\times n$ monomial matrix $M\colon \mathbb {F} _{q}^{n}\to \mathbb {F} _{q}^{n}$ which sends C$_1$ isomorphically to C$_2$ then we say C$_1$ and C$_2$ are equivalent.
Lemma: Any linear code is permutation equivalent to a code which is in standard form.

## Bonisoli's theorem

A code is defined to be equidistant if and only if there exists some constant d such that the distance between any two of the code's distinct codewords is equal to d. In 1984 Arrigo Bonisoli determined the structure of linear one-weight codes over finite fields and proved that every equidistant linear code is a sequence of dual Hamming codes.

## Examples

Some examples of linear codes include:
* Repetition code
* Parity code
* Cyclic code
* Hamming code
* Golay code, both the binary and ternary versions
* Polynomial codes, of which BCH codes are an example
* Reed–Solomon codes
* Reed–Muller code
* Algebraic geometry code
* Binary Goppa code
* Low-density parity-check codes
* Expander code
* Multidimensional parity-check code
* Toric code
* Turbo code
* Locally recoverable code

## Generalization

Hamming spaces over non-field alphabets have also been considered, especially over finite rings, most notably Galois rings over Z$_4$. This gives rise to modules instead of vector spaces and ring-linear codes (identified with submodules) instead of linear codes. The typical metric used in this case the Lee distance. There exist a Gray isometry between $\mathbb {Z} _{2}^{2m}$ (i.e. GF(2$^{2m}$)) with the Hamming distance and $\mathbb {Z} _{4}^{m}$ (also denoted as GR(4,m)) with the Lee distance; its main attraction is that it establishes a correspondence between some "good" codes that are not linear over $\mathbb {Z} _{2}^{2m}$ as images of ring-linear codes from $\mathbb {Z} _{4}^{m}$.
Some authors have referred to such codes over rings simply as linear codes as well.