owned this note
owned this note
Published
Linked with GitHub
# Sage: 矩陣、線性方程組
Sage: Matrices and linear equations

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}}$
## 建構矩陣
Construct a matrix
1. `matrix( list of lists )`:把 `list of lists` 中的每個 `list` 當作矩陣的列。
2. `matrix(r, list)`:把 `list` 切成 `r` 個列。
3. `identity_matrix(n)`:單位矩陣。
4. `zero_matrix(n)` or `zero_matrix(m,n)`:全零矩陣。
利用 `print`(純文字)或 `show`(格式化文字)來顯示矩陣。
<!-- eng start -->
1. `matrix( list of lists )`: return the matrix whose rows are the `lists` in `list of lists` .
2. `matrix(r, list)`: split `list` into `r` rows and return the matrix.
3. `identity_matrix(n)`: the identity matrix.
4. `zero_matrix(n)` or `zero_matrix(m,n)`: the zero matrix.
You may use `print` (pure text) or `show` (formatted output) to display the matrix.
<!-- eng end -->
```python
A = matrix([[1,2,3],
[4,5,6]])
print(A)
```
```python
A = matrix(2, [1,2,3,4,5,6])
show(A)
```
```python
A = identity_matrix(3)
# A = zero_matrix(3)
# A = zero_matrix(3,4)
show(A)
```
## 從矩陣中選取各項或子矩陣
Select an entry or a submatrix from a matrix
若 `A` 是一個矩陣。
1. `A[i,j]`:選取第 `ij` 項。
2. `A[list1, list2]`:選取列在 `list1` 中、行在 `list2` 中的子矩陣。
也可以混合使用﹐如 `A[i, list]` 或 `A[list, j]`。
<!-- eng start -->
Suppose `A` is a matrix.
1. `A[i,j]`: return the `ij`-entry.
2. `A[list1, list2]`: return the submatrix induced on rows in `list1` and columns in `list2` .
You may mix the two usages, such as `A[i, list]` or `A[list, j]` .
<!-- eng end -->
```python
A = matrix(2, [1,2,3,4,5,6])
show(A)
print(A[0,1])
show(A[[0,1],[1,2]])
```
選取子矩陣中 `list` 的可以用 `a:b` 的格式取代。
1. `a:b`:從 `a` 到 `b`(不包含 `b`)。
2. `a:`:從 `a` 到底。
3. `:b`:從頭到 `b`(不包含 `b`)。
4. `:`:全部。
<!-- eng start -->
The `list` used for selecting the submatrix can be replaced by `a:b` .
1. `a:b`: from `a` to `b` (excluding `b`).
2. `a:`: from `a` to the end.
3. `:b`: from the beginning to `b`.
4. `:`: all.
<!-- eng end -->
```python
A = matrix(2, [1,2,3,4,5,6])
show(A[:,1:])
```
可以把選出來的子矩陣設定成給定的矩陣。
<!-- eng start -->
You may assign new values to the selected submatrix.
<!-- eng end -->
```python
A = zero_matrix(2,3)
show(A)
A[0,0] = 100
show(A)
A[:,1:] = identity_matrix(2)
show(A)
```
## 求解
Finding the solution(s)
若 `A` 為一矩陣﹐可以用
1. `A.right_kernel()` 或
2. `A.right_kernel().basis_matrix()`
來找到零解的基底。
<!-- eng start -->
Let `A` be a matrix. You may use
1. `A.right_kernel()` or
2. `A.right_kernel().basis_matrix()`
to find a basis of the homogeneous solutions.
<!-- eng end -->
```python
A = matrix(2, [1]*6)
show(A)
print(A.right_kernel())
show(A.right_kernel().basis_matrix())
```
可以用 `vector([list])` 來設定向量。
若 `A` 是矩陣而 `b` 是向量﹐
則 `A \ b` 會給出一組解。
(也可以用 `A.solve_right(b)`。)
<!-- eng start -->
Use `vector([list])` to construct a vector. If `A` is a matrix and `b` is a vector, then `A \ b` returns a solution. (Equivalently, `A.solve_right(b)` has the same effect.)
<!-- eng end -->
```python
A = matrix(2, [1]*6)
b = vector([1,1])
show(A)
print(b)
print(A \ b)
print(A.solve_right(b))
```
## 最簡階梯形式矩陣、增廣矩陣
Reduced echelon form and augmenting matrix
若 `A` 為一矩陣﹐則 `A.rref()` 會回傳其最簡階梯形式矩陣。
<!-- eng start -->
Let `A` be a matrix. Then `A.rref()` returns its reduced echelon form.
<!-- eng end -->
```python
A = matrix(3, list(range(12)))
show(A)
show(A.rref())
```
若 `A` 為一矩陣而 `b` 為一向量﹐則 `A.augment(b)` 會回傳相對應的增廣矩陣。
<!-- eng start -->
Let `A` be a matrix and `b` a vector. Then `A.augment(b)` returns their augmenting matrix.
<!-- eng end -->
```python
A = matrix(3, list(range(12)))
b = vector([1,5,9])
Ab = A.augment(b)
show(Ab)
Ab = A.augment(b, subdivide=True)
show(Ab)
```
可以用以下函數對矩陣(或增廣矩陣)`A` 執行列運算。
1. `A.swap_rows(i,j)` .
2. `A.rescale(i, k)` .
3. `A.add_multiple_of_row(i, j, k)` .
注意這些函數會直接修改矩陣本身而不會回傳任何矩陣。
<!-- eng start -->
Apply the row operation to a matrix (or an augmenting matrix) `A` by the following functions.
1. `A.swap_rows(i,j)` .
2. `A.rescale(i, k)` .
3. `A.add_multiple_of_row(i, j, k)` .
Notice that these functions update the matrix itself and do not return any matrix.
<!-- eng end -->
```python
A = matrix(3, list(range(12)))
show(A)
print(A.swap_rows(0,1))
show(A)
```
## 反矩陣
Inverse matrix
若 `A` 和 `B` 都是矩陣﹐則 `A.augment(B)` 也可以建立其相對應的增廣矩陣。
藉此可以計算反矩陣。
<!-- eng start -->
Let `A` and `B` be matrices. Then `A.augment(B)` also returns the corresponding augmenting matrix. We may use it to compute the inverse.
<!-- eng end -->
```python
A = matrix([[1,1,1],
[1,2,4],
[1,3,9]])
I3 = identity_matrix(3)
AI = A.augment(I3, subdivide=True)
show(AI)
IB = AI.rref()
show(IB)
B = IB[:,3:]
show(A * B)
```
若 `A` 是方陣﹐
1. `A.is_invertible()` 可以判斷其是否可逆﹐
2. `A.inverse()` 會求出它的逆矩陣。
<!-- eng start -->
Suppose `A` is a square matrix. Then
1. `A.is_invertible()` determines if `A` is invertible, and
2. `A.inverse()` returns the inverse of `A` .
<!-- eng end -->
```python
A = matrix([[1,1,1],
[1,2,4],
[1,3,9]])
print(A.is_invertible())
show(A.inverse())
```
## 四大基礎子空間的標準基底
The standard bases of the four fundamental subspaces
`lingeo` 是為這份教材所寫的函式庫。
其內容包含在 `lingeo.py` 裡。
可以用
```python
from lingeo import random_good_matrix
```
來匯入須要的函數。
<!-- eng start -->
`lingeo` is a library written for LA-notebook. Its content can be found in `lingeo.py` , and you may use the syntax like
```python
from lingeo import random_good_matrix
```
to import the necessary functions.
<!-- eng end -->
```python
from lingeo import random_good_matrix
from lingeo import row_space_matrix, column_space_matrix, kernel_matrix, left_kernel_matrix
```
若 `A` 為一矩陣﹐則
1. `row_space_matrix(A)` 的列是由 $\beta_R$ 組成、
2. `kernel_matrix(A)` 的行是由 $\beta_K$ 組成、
3. `column_space_matrix(A)` 的行是由 $\beta_C$ 組成、
4. `left_kernel_matrix(A)` 的列是由 $\beta_L$ 組成。
<!-- eng start -->
Let `A` be a matrix. Then
1. `row_space_matrix(A)` is the matrix whose rows are vectors in $\beta_R$,
2. `kernel_matrix(A)` is the matrix whose columns are vectors in $\beta_K$,
3. `column_space_matrix(A)` is the matrix whose columns are vectors in $\beta_C$, and
4. `left_kernel_matrix(A)` is the matrix whose rows are vectors in $\beta_L$.
<!-- eng end -->
```python
m,n,r = 3,5,2
A = random_good_matrix(m,n,r)
show(A)
print("row space matrix:")
show(row_space_matrix(A))
print("kernel matrix:")
show(kernel_matrix(A))
```
```python
m,n,r = 3,5,2
A = random_good_matrix(m,n,r)
show(A)
print("column space matrix:")
show(column_space_matrix(A))
print("left kernel matrix:")
show(left_kernel_matrix(A))
```