Matrix rank (source code)

= Matrix rank
{title2=$\operatorname{rank}A$}
{wiki=Rank_(linear_algebra)}

= Rank of a matrix
{synonym}

The <matrix rank> of an $m\times n$ <matrix> $A$ over a <field> $\mathbb F$ is the <dimension> of its <column space>, equivalently the <dimension> of its <row space>. It counts <linearly independent> column directions, with $0\leq\operatorname{rank}A\leq\min(m,n)$. Counting nonzero <matrix entries> gives a different quantity: an <all-ones matrix> can have arbitrarily many nonzero <matrix entries> and still have <matrix rank> one.<Interactive Linear Algebra by Dan Margalit and others/The Rank Theorem>[Interactive Linear Algebra, §2.9, Definition: rank is column-space dimension; the row-space equality is proved below]{ref}

Consider the following <matrix> over the <real numbers>, with columns written as <vectors>:
$$
A=\begin{pmatrix}1&2&3\\2&4&6\\1&1&2\end{pmatrix},\qquad
c_1=\begin{pmatrix}1\\2\\1\end{pmatrix},\quad
c_2=\begin{pmatrix}2\\4\\1\end{pmatrix},\quad
c_3=\begin{pmatrix}3\\6\\2\end{pmatrix}=c_1+c_2.
$$
The <vectors> $c_1,c_2$ are <linearly independent>: $\alpha c_1+\beta c_2=0$ requires both $\alpha+2\beta=0$ and $\alpha+\beta=0$, so $\beta=\alpha=0$. The third column adds no new direction to their <linear span>, hence $\operatorname{rank}A=2$ despite all nine <matrix entries> being nonzero. The second row likewise repeats twice the first row.

The <column space> is a <plane> through the origin:
$$
\operatorname{col}A=\operatorname{span}\{c_1,c_2\}
=\left\{\begin{pmatrix}u\\2u\\v\end{pmatrix}:u,v\in\mathbb R\right\}.
$$
Every output has second <coordinate> twice its first <coordinate>. Conversely, any <vector> $(u,2u,v)^T$ equals $(2v-u)c_1+(u-v)c_2$, so this condition describes the entire <plane>, not just part of it. For example, $b=(1,2,0)^T=-c_1+c_2$ is attainable, whereas $b'=(1,3,0)^T$ lies outside the <plane>.

\Image[/matrix-rank-column-plane.png]
{title=Three column <vectors> span a two-dimensional <plane>: $c_3=c_1+c_2$, $b$ is attainable, and $b'$ is outside the <column space>}

<Elementary row operations> expose the same count algebraically. Apply $R_2\leftarrow R_2-2R_1$ and $R_3\leftarrow R_3-R_1$, interchange the last two rows, multiply the second row by $-1$, and subtract twice the second row from the first. The resulting <reduced row echelon form> is
$$
R=\begin{pmatrix}1&0&1\\0&1&1\\0&0&0\end{pmatrix}.
$$
There are two <pivot columns>, so the <matrix rank> is two. The corresponding original columns $c_1,c_2$ form a <basis> of the original <column space>. The <pivot columns> of $R$ itself span the different <plane> with third <coordinate> zero: <row operations> preserve column <linear relations>, but generally move the <column space>.<Interactive Linear Algebra by Dan Margalit and others/Row Reduction>[Interactive Linear Algebra, §1.2.2, Echelon Forms; §1.2.3, definitions of pivot position and pivot column]{ref}

Solving the <homogeneous linear system> $Rx=0$ gives $x_1+x_3=0$ and $x_2+x_3=0$. Taking the <free variable> $x_3=t$ gives
$$
\ker A=\operatorname{span}\{(-1,-1,1)^T\},\qquad
\operatorname{rank}A+\dim\ker A=2+1=3.
$$
The <kernel> is the input direction that trades one unit of each of the first two columns for one unit of the third. This trade leaves the output unchanged. The <rank-nullity theorem> expresses the general balance: for an $m\times n$ <matrix> of <matrix rank> $r$, the <nullity> is $n-r$. The <column space> describes reachable outputs; the <kernel> describes invisible changes to inputs.<Interactive Linear Algebra by Dan Margalit and others/The Rank Theorem>[Interactive Linear Algebra, §2.9, Rank Theorem: rank plus nullity equals the number of columns]{ref}

For the attainable output $b$, one solution is $x_0=(-1,1,0)^T$. Every solution lies in the <affine solution space of a linear equation>
$$
x=x_0+t(-1,-1,1)^T=\begin{pmatrix}-1-t\\1-t\\t\end{pmatrix},\qquad t\in\mathbb R.
$$
Indeed, $Ax_0=b$ and $A(-1,-1,1)^T=0$. Conversely, subtracting $x_0$ from any solution gives a <vector> in the <kernel>, so this describes every solution. For $b'$, the same <row operations> on the <augmented matrix> expose $0=1$: the second requirement demands $3$ where twice the first requires $2$.

\Image[/matrix-rank-reduction-consistency.png]
{title=Two <pivot columns>, one <kernel> direction, and the same <row operations> distinguishing attainable $b$ from impossible $b'$}

In general, a <linear system> $Ax=b$ is solvable exactly when appending $b$ leaves the <matrix rank> unchanged:
$$
\operatorname{rank}[A\mid b]=\operatorname{rank}A.
$$
Appending a <vector> outside the <column space> adds an independent direction. When a solution $x_0$ exists, every solution is $x_0+\ker A$, because $A(x-x_0)=0$. Thus one count predicts both how many output directions are reachable and, through the <rank-nullity theorem>, how many independent input directions remain free.<Interactive Linear Algebra by Dan Margalit and others/Solution Sets>[Interactive Linear Algebra, §2.4.2, Key Observation and its proof; §2.4.3, Solution Sets and Column Spans]{ref}