Matrix inverse (source code)

= Matrix inverse
{title2=$A^{-1}$}
{wiki}

= Inverse matrix
{synonym}

For an $n$-by-$n$ <square matrix> $A$ over a <field>, its matrix inverse is an $n$-by-$n$ <matrix> $A^{-1}$ satisfying both identities
$$
AA^{-1}=A^{-1}A=I_n,
$$
where $I_n$ is the <identity matrix>. It exists exactly when $A$ is an <invertible matrix>, equivalently when its <matrix rank> is $n$ or its <determinant> is nonzero. It reverses the <linear map>: applying $A$ and then $A^{-1}$ recovers the input, while applying $A^{-1}$ and then $A$ recovers the output.https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-3-multiplication-and-inverse-matrices/[Strang, MIT 18.06 Lecture 3: the two inverse identities]{ref}

The <matrix inverse> is unique. If $B$ and $C$ both satisfy the identities, then $B=B(AC)=(BA)C=C$. Although the definition is two-sided, for finite <square matrices> either identity alone suffices: $BA=I$ forces a <trivial kernel>, whereas $AB=I$ forces <surjectivity>; either makes $A$ an <invertible matrix> by the <rank-nullity theorem>. Rectangular <matrices> may have only one of these properties.

For a concrete example, take
$$
A=\begin{pmatrix}2&1\\1&1\end{pmatrix},\qquad
\det A=2\cdot1-1\cdot1=1,\qquad
A^{-1}=\begin{pmatrix}1&-1\\-1&2\end{pmatrix}.
$$
Direct <matrix multiplication> gives
$$
AA^{-1}=\begin{pmatrix}2-1&-2+2\\1-1&-1+2\end{pmatrix}=I_2,
\qquad
A^{-1}A=\begin{pmatrix}2-1&1-1\\-2+2&-1+2\end{pmatrix}=I_2.
$$
Consequently the <vector> $v=(1,2)^T$ follows a reversible journey:
$$
\begin{pmatrix}1\\2\end{pmatrix}
\xrightarrow{\ A\ }\begin{pmatrix}4\\3\end{pmatrix}
\xrightarrow{\ A^{-1}\ }\begin{pmatrix}4-3\\-4+6\end{pmatrix}
=\begin{pmatrix}1\\2\end{pmatrix}.
$$
The columns $(2,1)^T$ and $(1,1)^T$ are the images of the <standard basis> vectors. They turn the unit square into a <parallelogram>; the <matrix inverse> restores both the square and its surrounding grid. Since $\det A=1$, this example changes shape while preserving <area>, illustrating that invertibility does not require preserving lengths or angles.

\Image[/matrix-inverse-undoing-a-map.png]
{title=A <matrix inverse> restores a grid and a <vector>; a <singular matrix> merges distinct inputs}

For comparison, the <singular matrix>
$$
S=\begin{pmatrix}1&1\\0&0\end{pmatrix},\qquad
S\begin{pmatrix}1\\0\end{pmatrix}
=S\begin{pmatrix}0\\1\end{pmatrix}
=\begin{pmatrix}1\\0\end{pmatrix}
$$
cannot have a <matrix inverse>: the output $(1,0)^T$ cannot specify which of the two inputs occurred. Their difference $(1,-1)^T$ belongs to its <kernel>. Indeed $S(x,y)^T=(x+y,0)^T$ collapses the <plane> to a <line>, its <matrix rank> is one, and its <determinant> is zero. No subsequent <function> of the output alone can reconstruct every input.

An important use of the <matrix inverse> is describing the unique solution of a <linear system>:
$$
Ax=b\iff x=A^{-1}b.
$$
For $b=(4,3)^T$, the preceding computation solves $2x_1+x_2=4$ and $x_1+x_2=3$, giving $x=(1,2)^T$. In computation, usually solve the <linear system> using <Gaussian elimination> and a factorization such as an <LU decomposition>, rather than forming every entry of the <matrix inverse>. The inverse formula specifies the answer; a factorization provides a reusable way to compute it for each right-hand side.https://www.mit.edu/~18.06/Spring17/final-sol.pdf[MIT 18.06, Spring 2017 final solutions, problem 5(b): solve with triangular factors rather than computing inverses]{ref}

Here the <LU decomposition> is especially simple:
$$
A=LU,\qquad
L=\begin{pmatrix}1&0\\\tfrac12&1\end{pmatrix},\qquad
U=\begin{pmatrix}2&1\\0&\tfrac12\end{pmatrix}.
$$
Solve $Ly=b$ by <forward substitution in a triangular system>: $y_1=4$ and $y_2=3-\tfrac12\cdot4=1$. Then solve $Ux=y$ by <backward substitution in a triangular system>: $x_2=2$ and $x_1=(4-2)/2=1$. The same factors work when $b$ changes. If the whole <matrix inverse> is actually needed, its $j$th column is the solution of $Ax=e_j$, where $e_j$ is the $j$th <standard basis> vector.

Reversing several operations means undoing the last one first. For <invertible matrices> of the same size,
$$
(AB)^{-1}=B^{-1}A^{-1},
$$
because $(AB)(B^{-1}A^{-1})=A(BB^{-1})A^{-1}=I$, and the product in the other order is also $I$. This is why a <change-of-basis matrix> uses its <matrix inverse> to reverse the direction of coordinate conversion.