# 反矩陣
Matrix inverse

This work by Jephian Lin is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).
$\newcommand{\trans}{^\top}
\newcommand{\adj}{^{\rm adj}}
\newcommand{\cof}{^{\rm cof}}
\newcommand{\inp}[2]{\left\langle#1,#2\right\rangle}
\newcommand{\dunion}{\mathbin{\dot\cup}}
\newcommand{\bzero}{\mathbf{0}}
\newcommand{\bone}{\mathbf{1}}
\newcommand{\ba}{\mathbf{a}}
\newcommand{\bb}{\mathbf{b}}
\newcommand{\bc}{\mathbf{c}}
\newcommand{\bd}{\mathbf{d}}
\newcommand{\be}{\mathbf{e}}
\newcommand{\bh}{\mathbf{h}}
\newcommand{\bp}{\mathbf{p}}
\newcommand{\bq}{\mathbf{q}}
\newcommand{\br}{\mathbf{r}}
\newcommand{\bx}{\mathbf{x}}
\newcommand{\by}{\mathbf{y}}
\newcommand{\bz}{\mathbf{z}}
\newcommand{\bu}{\mathbf{u}}
\newcommand{\bv}{\mathbf{v}}
\newcommand{\bw}{\mathbf{w}}
\newcommand{\tr}{\operatorname{tr}}
\newcommand{\nul}{\operatorname{null}}
\newcommand{\rank}{\operatorname{rank}}
%\newcommand{\ker}{\operatorname{ker}}
\newcommand{\range}{\operatorname{range}}
\newcommand{\Col}{\operatorname{Col}}
\newcommand{\Row}{\operatorname{Row}}
\newcommand{\spec}{\operatorname{spec}}
\newcommand{\vspan}{\operatorname{span}}
\newcommand{\Vol}{\operatorname{Vol}}
\newcommand{\sgn}{\operatorname{sgn}}
\newcommand{\idmap}{\operatorname{id}}
\newcommand{\am}{\operatorname{am}}
\newcommand{\gm}{\operatorname{gm}}
\newcommand{\mult}{\operatorname{mult}}
\newcommand{\iner}{\operatorname{iner}}$
```python
from lingeo import random_good_matrix
```
## Main idea
##### Matrix-matrix multipliciation (by column)
Let $A$ be an $m\times n$ matrix.
Let $B$ be an $n\times \ell$ matrix whose columns are $\bu_1,\ldots,\bu_\ell$.
Then the columns of $AB$ are $A\bu_1, \ldots, A\bu_\ell$.
Recall that an $n\times n$ matrix $A$ is invertible if there is a matrix $B$ such that $AB = BA = I_n$.
Indeed, an $n\times n$ matrix is invertible if and only if it is nonsingular.
**invertible $\implies$ nonsingular**
Suppose $A$ and $B$ are $n\times n$ matrices such that $AB = I_n$.
Then both $A$ and $B$ are nonsingular.
**nonsingular $\implies$ invertible**
Suppose $A$ is an $n\times n$ nonsingular matrix.
Let $\be_1,\ldots,\be_n$ be the columns of $I_n$.
Since $A$ is nonsingular and $\Col(A) = \mathbb{R}^n$, the equation $A\bx = \be_i$ has a solution $\bx = \bb_i$ for each $i = 1,\ldots, n$.
Let $B$ be the matrix whose columns are $\bb_1,\ldots,\bb_n$.
Then $AB = I_n$.
Here is a way to calculate $B$ at once:
1. Consider the $n\times 2n$ augmented matrix $\left[\begin{array}{c|c} A & I_n \end{array}\right]$.
2. Since $A$ is nonsingular, necessarily the reduced echelon form of $\left[\begin{array}{c|c} A & I_n \end{array}\right]$ is $\left[\begin{array}{c|c} I_n & B \end{array}\right]$ for some $B$.
3. Thus, $AB = I_n$.
Suppose $A$ and $B$ be $n\times n$ matrices such that $AB = I_n$.
Then $BA = I_n$.
Therefore, if $AB = I_n$, then both $A$ and $B$ are invertible and they are the inverse of each other.
Suppose $A$, $B$, and $C$ are $n\times n$ matrices such that $CA = I_n$ and $AB = I_n$.
Then $C = B$.
Therefore, each matrix only has one inverse.
## Side stories
- `is_invertible`
- `inverse`
- inverse algebra
## Experiments
##### Exercise 1
執行下方程式碼。
矩陣 $\left[\begin{array}{c|c} I & B \end{array}\right]$ 是 $\left[\begin{array}{c|c} A & I \end{array}\right]$ 的最簡階梯形式矩陣。
<!-- eng start -->
Run the code below. Let $\left[\begin{array}{c|c} I & B \end{array}\right]$ be the reduced echelon form of $\left[\begin{array}{c|c} A & I \end{array}\right]$.
<!-- eng end -->
```python
### code
set_random_seed(0)
print_ans = False
A = random_good_matrix(4,4,4)
AI = A.augment(identity_matrix(4), subdivide=True)
IB = AI.rref()
B = IB[:,4:]
print("[ A | I ] =")
show(AI)
print("[ I | B ] =")
show(IB)
```
##### Exercise 1(a)
令 $\bb_i$ 為 $B$ 的第 $i$ 個行向量。
令 $\be_i$ 為 $I$ 的第 $i$ 個行向量。
驗證是否 $A\bb_i = \be_i$。
說明為什麼。
<!-- eng start -->
Let $\bb_i$ be the $i$-th column of $B$ and $\be_i$ the $i$-th column of $I$. Check if $A\bb_i = \be_i$. Can you explain it?
<!-- eng end -->
##### Exercise 1(b)
驗證是否 $AB = I$。
<!-- eng start -->
Verify that $AB = I$.
<!-- eng end -->
## Exercises
##### Exercise 2
令 $A$ 和 $B$ 為 $n\times n$ 矩陣。
驗證以下反矩陣的性質。
<!-- eng start -->
Let $A$ and $B$ be $n\times n$ matrices. Verify the following properties of an inverse matrix.
<!-- eng end -->
##### Exercise 2(a)
若 $A$ 可逆。
如果 $X$ 和 $B$ 是 $n\times m$ 矩陣且 $AX = B$
則 $X = A^{-1}B$。
如果 $X$ 和 $B$ 是 $m\times n$ 矩陣且 $XA = B$
則 $X = BA^{-1}$。
<!-- eng start -->
Suppose $A$ is invertible. If $X$ and $B$ are $n\times m$ matrices and $AX = B$, then $X = A^{-1}B$. If $X$ and $B$ are $m\times n$ matrices and $XA = B$, then $X = BA^{-1}$.
<!-- eng end -->
##### Exercise 2(b)
若 $A$ 和 $B$ 都可逆﹐
則 $AB$ 也可逆且 $(AB)^{-1} = B^{-1}A^{-1}$。
<!-- eng start -->
If $A$ and $B$ are invertible, then $AB$ is also invertible with $(AB)^{-1} = B^{-1}A^{-1}$.
<!-- eng end -->
##### Exercise 2(c)
若 $AB$ 可逆﹐
則 $A$ 和 $B$ 都可逆。
<!-- eng start -->
If $AB$ are invertible, then both $A$ and $B$ are invertible.
<!-- eng end -->
##### Exercise 2(d)
若 $A$ 可逆且 $B = A^{-1}$﹐
則 $B$ 也可逆且 $A = B^{-1}$。
<!-- eng start -->
If $A$ is invertible with $B = A^{-1}$, then $B$ is invertible with $A = B^{-1}$.
<!-- eng end -->
##### Exercise 2(e)
若 $A$ 可逆﹐
則 $A\trans$ 也可逆且 $(A\trans)^{-1} = (A^{-1})\trans$。
<!-- eng start -->
If $A$ is invertible, then $A\trans$ is invertible with $(A\trans)^{-1} = (A^{-1})\trans$.
<!-- eng end -->
##### Exercise 3
令 $A$ 是一個 $m\times n$ 矩陣。
以下討論 $A\trans A$ 是否可逆。
<!-- eng start -->
Let $A$ be an $m\times n$. The following problems are about the invertibility of $A\trans A$.
<!-- eng end -->
##### Exercise 3(a)
證明以下敘述等價:
1. $\ker(A) = \{\bzero\}$(因此必有 $m\geq n$)。
2. $A\trans A$ 可逆。
<!-- eng start -->
Show that the following are equivalent:
1. $\ker(A) = \{\bzero\}$ (so necessarily $m\geq n$).
2. $A\trans A$ is invertible.
<!-- eng end -->
##### Exercise 3(b)
證明若 $m < n$ 則 $A\trans A$ 不可逆。
<!-- eng start -->
Show that if $m < n$ then $A\trans A$ is not invertible.
<!-- eng end -->
##### Exercise 4
令 $A$ 和 $B$ 為 $n\times n$ 矩陣。
依照以下步驟證明
若 $AB = I_n$ 則 $BA = I_n$。
<!-- eng start -->
Let $A$ and $B$ be $n\times n$ matrices. Use the given instructions to prove that $AB = I_n$ if and only if $BA = I_n$.
<!-- eng end -->
##### Exercise 4(a)
若 $AB = I_n$。
證明增廣矩陣 $\left[\begin{array}{c|c} A & I_n \end{array}\right]$ 的最簡階梯形式矩陣
一定是 $\left[\begin{array}{c|c} I_n & B \end{array}\right]$。
<!-- eng start -->
Suppose $AB = I_n$. Show that the reduced echelon form of $\left[\begin{array}{c|c} A & I_n \end{array}\right]$ is $\left[\begin{array}{c|c} I_n & B \end{array}\right]$.
<!-- eng end -->
##### Exercise 4(b)
令 $\be_i$ 為 $I_n$ 的第 $i$ 個行向量。
令 $\ba_i$ 為 $A$ 的第 $i$ 個行向量。
說明對每個 $i = 1,\ldots, n$ 都有
$$
\left[\begin{array}{c|c} A & I_n \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero,
$$
因此也有
$$
\left[\begin{array}{c|c} I_n & B \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero.
$$
<!-- eng start -->
Let $\be_i$ be the $i$-th column of $I_n$ and $\ba_i$ the $i$-th column of $A$.
Show that for each $i = 1,\ldots, n$, we have
$$
\left[\begin{array}{c|c} A & I_n \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero,
$$
and
$$
\left[\begin{array}{c|c} I_n & B \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero.
$$
<!-- eng end -->
##### Exercise 4(c)
因為對每個 $i = 1,\ldots, n$ 都有
$$
\left[\begin{array}{c|c} I_n & B \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero.
$$
說明對每個 $i = 1,\ldots, n$ 都有 $B\ba_i = \be_i$、
因此 $BA = I_n$。
<!-- eng start -->
For each $i = 1,\ldots, n$, we know
$$
\left[\begin{array}{c|c} I_n & B \end{array}\right]
\begin{bmatrix} \be_i \\ -\ba_i \end{bmatrix} = \bzero.
$$
Use this fact to show that $B\ba_i = \be_i$ for each $i = 1,\ldots, n$. Therefore, $BA = I_n$.
<!-- eng end -->
##### Exercise 4(d)
找一組例子使得
$A$ 是 $n\times m$ 矩陣、
$B$ 是 $m\times n$ 矩陣、
$AB = I_n$、
但是 $BA \neq I_m$。
<!-- eng start -->
Find an $n\times m$ matrix $A$ and an $m\times n$ matrix $B$ such that $AB = I_n$ but $BA \neq I_m$.
<!-- eng end -->
**[由鄭宗祐提供]**
**Answer**
Let $A=\begin{bmatrix}1&2&3\\0&4&1\end{bmatrix}$ and $B=\begin{bmatrix}1&-3\\0&0\\0&1\end{bmatrix}.$
Thus, $AB=\begin{bmatrix}1&2&3\\0&4&1\end{bmatrix}\begin{bmatrix}1&-3\\0&0\\0&1\end{bmatrix}=\begin{bmatrix}1&0\\0&1\end{bmatrix}=I_n,$
while $BA=\begin{bmatrix}1&-3\\0&0\\0&1\end{bmatrix}\begin{bmatrix}1&2&3\\0&4&1\end{bmatrix}=\begin{bmatrix}1&-10&0\\0&0&0\\0&4&1\end{bmatrix}\neq I_m.$
##### Exercise 5
令 $A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}$ 且 $\det(A) \neq 0$。
證明 $A^{-1} = \frac{1}{\det(A)}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}$。
<!-- eng start -->
Let $A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}$ with $\det(A) \neq 0$. Show that $A^{-1} = \frac{1}{\det(A)}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}$.
<!-- eng end -->