Tutorials › Linear Algebra › Linear Codes as Subspaces

Linear Algebra Beyond the Reals · Tutorial 973 of 1000

Linear Codes as Subspaces

Represent binary words as vectors and use generator matrices to find a code’s dimension, construct its codewords, and tell when messages have unique representations.

Intermediate 10 min read

What You'll Learn

  • Define binary words and linear codes as vectors and subspaces over the field with two elements.
  • Build a linear code by taking all linear combinations of specified columns.
  • Compute a code’s dimension from a basis or a generator matrix.
  • List every codeword by multiplying the generator matrix by all possible messages.
  • Distinguish a basis generator matrix from one with redundant columns.
  • Check whether different messages produce the same codeword.

Binary Words as Vectors

A binary word of length \(n\) is a sequence of \(n\) entries, each equal to \(0\) or \(1\). For linear algebra, we treat that sequence as a column vector in \(\mathbb{F}_2^n\), where \(\mathbb{F}_2=\{0,1\}\) is the field with addition and multiplication performed modulo \(2\). Thus \(1+1=0\), while \(1\cdot1=1\). Addition of binary words is coordinate by coordinate using this arithmetic. For instance, \(\begin{bmatrix}1\\0\\1\end{bmatrix}+\begin{bmatrix}1\\1\\0\end{bmatrix}=\begin{bmatrix}0\\1\\1\end{bmatrix}\).

Definition: A binary linear code of length \(n\) is a subspace \(C\) of \(\mathbb{F}_2^n\). Its elements are called codewords. The dimension of the code is \(\dim C\), the dimension of this vector space over \(\mathbb{F}_2\).

So a linear code is a collection of binary words that contains the zero word and is closed under binary addition and scalar multiplication. In \(\mathbb{F}_2\), scalar multiplication has just two possibilities: multiplying by \(0\) gives the zero vector, and multiplying by \(1\) leaves a vector unchanged. Consequently, for a nonempty collection, closure under addition and scalar multiplication implies that it is a subspace: if \(\mathbf{v}\) belongs to the collection, then \(0\mathbf{v}=\mathbf{0}\) belongs as well. Requiring nonemptiness matters; the empty collection satisfies closure statements vacuously but is not a subspace.

The linear structure gives a compact way to describe a code. If \(\mathbf{g}_1,\dots,\mathbf{g}_k\) form a basis for \(C\), every codeword is a unique linear combination of those vectors. The basis vectors can be arranged as columns of a matrix. Multiplying that matrix by a vector of coefficients then generates a codeword.

Generator Matrices and Code Dimension

Definition: Let \(\mathbf{G}\in\mathbb{F}_2^{n\times m}\) have columns \(\mathbf{g}_1,\dots,\mathbf{g}_m\). The code generated by \(\mathbf{G}\) is \(C=\operatorname{span}\{\mathbf{g}_1,\dots,\mathbf{g}_m\}\). For a message vector \(\mathbf{u}\in\mathbb{F}_2^m\), the generated codeword is \(\mathbf{c}=\mathbf{G}\mathbf{u}\). A matrix whose columns generate \(C\) is called a generator matrix for \(C\).

This column convention matches the convention that vectors are columns. If \(\mathbf{u}=(u_1,\dots,u_m)^{\top}\), then \(\mathbf{G}\mathbf{u}=u_1\mathbf{g}_1+\cdots+u_m\mathbf{g}_m\). The entries \(u_i\) are the coefficients used to form the codeword; they need not be unique if the columns of \(\mathbf{G}\) are dependent.

Why does a generator matrix produce a linear code? The set of all products \(\mathbf{G}\mathbf{u}\) contains \(\mathbf{0}\), since \(\mathbf{G}\mathbf{0}=\mathbf{0}\). If \(\mathbf{c}_1=\mathbf{G}\mathbf{u}_1\) and \(\mathbf{c}_2=\mathbf{G}\mathbf{u}_2\), then \(\mathbf{c}_1+\mathbf{c}_2=\mathbf{G}\mathbf{u}_1+\mathbf{G}\mathbf{u}_2=\mathbf{G}(\mathbf{u}_1+\mathbf{u}_2)\), which is another product of \(\mathbf{G}\). For \(a\in\mathbb{F}_2\), \(a\mathbf{c}_1=a(\mathbf{G}\mathbf{u}_1)=\mathbf{G}(a\mathbf{u}_1)\), also another product. These facts verify the subspace conditions directly.

Theorem: Let \(C\) be generated by the columns of \(\mathbf{G}\in\mathbb{F}_2^{n\times m}\). Then \(\dim C=\operatorname{rank}\mathbf{G}\). If the \(m\) columns are linearly independent, every codeword has a unique message vector \(\mathbf{u}\in\mathbb{F}_2^m\), the dimension is \(m\), and the code has \(2^m\) codewords.

Proof. The columns of \(\mathbf{G}\) span \(C\) by its definition. From a finite spanning list, retain a maximal linearly independent sublist. It still spans \(C\), because any omitted column must be a linear combination of the retained columns; otherwise it could be added to the independent sublist, contradicting maximality. The retained columns are therefore a basis of \(C\). Their number is the rank of \(\mathbf{G}\), so \(\dim C=\operatorname{rank}\mathbf{G}\). If all \(m\) columns are independent, they themselves are a basis. In that case, if \(\mathbf{G}\mathbf{u}=\mathbf{G}\mathbf{v}\), then \(\mathbf{G}(\mathbf{u}-\mathbf{v})=\mathbf{0}\) is a linear combination of the columns equal to zero. Independence forces every coefficient in \(\mathbf{u}-\mathbf{v}\) to be zero, so \(\mathbf{u}=\mathbf{v}\). Thus messages produce distinct codewords. Each of the \(m\) entries of a message has two choices, so there are \(2^m\) messages and, by distinctness, \(2^m\) codewords. \(\square\)

When the columns are dependent, the rank still gives the dimension, but the number of columns is not the dimension. Some messages then produce the same codeword. This distinction is useful when reading a generator matrix: its number of columns records the number of message coordinates supplied, while its rank records how many independent directions its code actually contains.

Generating a Code from Independent Columns

Worked Example: A Two-Dimensional Binary Code

Let \(\mathbf{G}\) have the following two columns in \(\mathbb{F}_2^4\):

$$ \mathbf{G}= \begin{bmatrix} 1&0\\ 0&1\\ 1&1\\ 1&0 \end{bmatrix}. $$

Write its columns as \(\mathbf{g}_1=\begin{bmatrix}1\\0\\1\\1\end{bmatrix}\) and \(\mathbf{g}_2=\begin{bmatrix}0\\1\\1\\0\end{bmatrix}\). They are independent: if \(a\mathbf{g}_1+b\mathbf{g}_2=\mathbf{0}\), the first coordinate gives \(a=0\), and the second gives \(b=0\). Thus they form a basis of the code \(C=\operatorname{span}\{\mathbf{g}_1,\mathbf{g}_2\}\), so \(\dim C=2\).

There are four possible message vectors. Multiplying by \(\mathbf{G}\) gives every codeword:

$$ \begin{aligned} \mathbf{G}\begin{bmatrix}0\\0\end{bmatrix} &=\begin{bmatrix}0\\0\\0\\0\end{bmatrix},& \mathbf{G}\begin{bmatrix}1\\0\end{bmatrix} &=\begin{bmatrix}1\\0\\1\\1\end{bmatrix},\\ \mathbf{G}\begin{bmatrix}0\\1\end{bmatrix} &=\begin{bmatrix}0\\1\\1\\0\end{bmatrix},& \mathbf{G}\begin{bmatrix}1\\1\end{bmatrix} &=\begin{bmatrix}1\\1\\0\\1\end{bmatrix}. \end{aligned} $$

For the last product, the third entry is \(1+1=0\) in \(\mathbb{F}_2\). The list is complete because it uses all four possible messages. It also contains no duplicates, as the columns are independent.

Check: The last word is the sum of the first two nonzero words: \(\begin{bmatrix}1\\0\\1\\1\end{bmatrix}+\begin{bmatrix}0\\1\\1\\0\end{bmatrix}=\begin{bmatrix}1\\1\\0\\1\end{bmatrix}\). The zero message gives the zero word, and the two columns are independent as shown, confirming the dimension and the four distinct codewords.

Redundant Generators

A generator matrix need not have independent columns. Dependent columns can be useful in a description, but they do not add new codewords or increase the dimension. In this case, row reduction over \(\mathbb{F}_2\) can help expose dependencies. The row operations use arithmetic modulo \(2\), so adding a row to another row is its own inverse: adding the same row again returns the original row because \(1+1=0\).

Worked Example: Three Generators but Dimension Two

Consider the matrix whose columns are \(\mathbf{g}_1,\mathbf{g}_2,\mathbf{g}_3\):

$$ \mathbf{G}= \begin{bmatrix} 1&0&1\\ 0&1&1\\ 1&1&0\\ 0&1&1 \end{bmatrix}. $$

Adding \(R_1\) to \(R_3\), and then \(R_2\) to \(R_4\), gives the row-equivalent matrix below. Adding \(R_2\) to \(R_3\) then clears row 3.

$$ \begin{bmatrix} 1&0&1\\ 0&1&1\\ 1&1&0\\ 0&1&1 \end{bmatrix} \sim \begin{bmatrix} 1&0&1\\ 0&1&1\\ 0&1&1\\ 0&0&0 \end{bmatrix} \sim \begin{bmatrix} 1&0&1\\ 0&1&1\\ 0&0&0\\ 0&0&0 \end{bmatrix}. $$

Here the third column of the original matrix equals the sum of its first two columns: \(\mathbf{g}_3=\mathbf{g}_1+\mathbf{g}_2\). The first two columns are independent, since the first coordinate of \(a\mathbf{g}_1+b\mathbf{g}_2=\mathbf{0}\) forces \(a=0\), and the second forces \(b=0\). They therefore form a basis for the code, which has dimension \(2\), not \(3\).

The messages \(\begin{bmatrix}0\\0\\1\end{bmatrix}\) and \(\begin{bmatrix}1\\1\\0\end{bmatrix}\) give the same word, because \(\mathbf{G}\begin{bmatrix}0\\0\\1\end{bmatrix}=\mathbf{g}_3=\mathbf{g}_1+\mathbf{g}_2=\mathbf{G}\begin{bmatrix}1\\1\\0\end{bmatrix}\). The four codewords, from the combinations of the basis columns, are \(\begin{bmatrix}0\\0\\0\\0\end{bmatrix}\), \(\begin{bmatrix}1\\0\\1\\0\end{bmatrix}\), \(\begin{bmatrix}0\\1\\1\\1\end{bmatrix}\), and \(\begin{bmatrix}1\\1\\0\\1\end{bmatrix}\).

Check: Adding the two nonzero basis words gives \(\begin{bmatrix}1\\0\\1\\0\end{bmatrix}+\begin{bmatrix}0\\1\\1\\1\end{bmatrix}=\begin{bmatrix}1\\1\\0\\1\end{bmatrix}\), since the third coordinate is \(1+1=0\). This is also the third column, as required by \(\mathbf{g}_3=\mathbf{g}_1+\mathbf{g}_2\).

The row-reduction display is consistent with the two independent generators, but the direct column relation is what immediately verifies the redundancy. When checking a particular generator matrix, it is good practice to check column relations or independence using the original columns: row operations change the columns themselves, even though they preserve the rank.

Recovering a Generator Matrix from a Description

A code may be described by parameters rather than by a matrix. To convert such a description into a generator matrix, express a general codeword as a linear combination of the parameters. The coefficient vector of each parameter becomes a column of the generator matrix. If the parameter columns are independent, they form a basis and the number of parameters is the code dimension.

Worked Example: From Parameters to a Generator Matrix

Define \(C\subseteq\mathbb{F}_2^4\) by \(\mathbf{c}=\begin{bmatrix}a+b\\b\\c\\a+c\end{bmatrix}\), where \(a,b,c\in\mathbb{F}_2\). Separate the contribution from each parameter:

$$ \mathbf{c} = a\begin{bmatrix}1\\0\\0\\1\end{bmatrix} +b\begin{bmatrix}1\\1\\0\\0\end{bmatrix} +c\begin{bmatrix}0\\0\\1\\1\end{bmatrix} = \begin{bmatrix} 1&1&0\\ 0&1&0\\ 0&0&1\\ 1&0&1 \end{bmatrix} \begin{bmatrix}a\\b\\c\end{bmatrix}. $$

The three columns are independent. Indeed, if a combination with coefficients \(x,y,z\) is zero, the second coordinate gives \(y=0\), the third gives \(z=0\), and then the first gives \(x=0\). Thus the columns are a basis, \(\dim C=3\), and the eight parameter choices give eight distinct codewords.

For example, with \((a,b,c)=(1,0,1)\), the formula gives \(\mathbf{c}=\begin{bmatrix}1\\0\\1\\0\end{bmatrix}\). Listing all parameter choices gives the codewords \(\begin{bmatrix}0\\0\\0\\0\end{bmatrix}\), \(\begin{bmatrix}1\\0\\0\\1\end{bmatrix}\), \(\begin{bmatrix}1\\1\\0\\0\end{bmatrix}\), \(\begin{bmatrix}0\\1\\0\\1\end{bmatrix}\), \(\begin{bmatrix}0\\0\\1\\1\end{bmatrix}\), \(\begin{bmatrix}1\\0\\1\\0\end{bmatrix}\), \(\begin{bmatrix}1\\1\\1\\1\end{bmatrix}\), and \(\begin{bmatrix}0\\1\\1\\0\end{bmatrix}\).

Check: The example \((a,b,c)=(1,0,1)\) yields \(\begin{bmatrix}1+0\\0\\1\\1+1\end{bmatrix}=\begin{bmatrix}1\\0\\1\\0\end{bmatrix}\), matching the matrix product. The count \(8=2^3\) agrees with three independent binary parameters.

Common Pitfalls and Why the Structure Matters

Do not use ordinary real arithmetic for binary coordinates. A wrong calculation might add the third entries \(1\) and \(1\) as ordinary integers and write \(2\), or reduce the sum to \(0\) but leave other coordinates unchanged. The correction is to perform every coordinate operation in \(\mathbb{F}_2\): for example, \(\begin{bmatrix}1\\1\\0\end{bmatrix}+\begin{bmatrix}1\\0\\1\end{bmatrix}=\begin{bmatrix}0\\1\\1\end{bmatrix}\). The first coordinate is \(1+1=0\), while the other two are \(1+0=1\) and \(0+1=1\).

Closure alone is not a safe substitute for the subspace conditions. The empty collection is vacuously closed under addition and scalar multiplication, yet it contains no zero vector and is not a subspace. Specify that the collection is nonempty, or state that it contains \(\mathbf{0}\) and is closed under both operations.

Generator matrices make the linear structure computational. A message vector supplies coefficients, matrix multiplication combines the generators, and a basis identifies how many independent choices are available. In applications, this provides a systematic way to turn a short binary message into a longer binary word while keeping the set of possible words algebraically organized. At this stage, the key questions are how the code is generated, how many independent generators it has, and whether its messages produce distinct codewords.

Check Your Understanding

Use arithmetic in \(\mathbb{F}_2\) throughout. For matrix questions, treat vectors as columns.

  1. Compute \(\begin{bmatrix}1\\0\\1\\1\end{bmatrix}+\begin{bmatrix}1\\1\\1\\0\end{bmatrix}\).
  2. Let \(\mathbf{G}=\begin{bmatrix}1&0\\1&1\\0&1\end{bmatrix}\). Find all codewords generated by its columns and determine the dimension.
  3. For the matrix in question 2, explain why distinct message vectors give distinct codewords.
  4. If a generator matrix has four columns but rank \(3\), what is the dimension of its code? Can its four message coordinates all be recovered uniquely from the codeword?
  5. Show that a nonempty subset of \(\mathbb{F}_2^n\) closed under addition and scalar multiplication contains \(\mathbf{0}\).

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon