Sage: Matrices and linear equations
This work by Jephian Lin is licensed under a Creative Commons Attribution 4.0 International License.
$\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
matrix( list of lists )
:把 list of lists
中的每個 list
當作矩陣的列。matrix(r, list)
:把 list
切成 r
個列。identity_matrix(n)
:單位矩陣。zero_matrix(n)
or zero_matrix(m,n)
:全零矩陣。利用 print
(純文字)或 show
(格式化文字)來顯示矩陣。
matrix( list of lists )
: return the matrix whose rows are the lists
in list of lists
.matrix(r, list)
: split list
into r
rows and return the matrix.identity_matrix(n)
: the identity matrix.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.
A = matrix([[1,2,3],
[4,5,6]])
print(A)
A = matrix(2, [1,2,3,4,5,6])
show(A)
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
是一個矩陣。
A[i,j]
:選取第 ij
項。A[list1, list2]
:選取列在 list1
中、行在 list2
中的子矩陣。也可以混合使用﹐如 A[i, list]
或 A[list, j]
。
Suppose A
is a matrix.
A[i,j]
: return the ij
-entry.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]
.
A = matrix(2, [1,2,3,4,5,6])
show(A)
print(A[0,1])
show(A[[0,1],[1,2]])
選取子矩陣中 list
的可以用 a:b
的格式取代。
a:b
:從 a
到 b
(不包含 b
)。a:
:從 a
到底。:b
:從頭到 b
(不包含 b
)。:
:全部。The list
used for selecting the submatrix can be replaced by a:b
.
a:b
: from a
to b
(excluding b
).a:
: from a
to the end.:b
: from the beginning to b
.:
: all.A = matrix(2, [1,2,3,4,5,6])
show(A[:,1:])
可以把選出來的子矩陣設定成給定的矩陣。
You may assign new values to the selected submatrix.
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
為一矩陣﹐可以用
A.right_kernel()
或A.right_kernel().basis_matrix()
來找到零解的基底。
Let A
be a matrix. You may use
A.right_kernel()
orA.right_kernel().basis_matrix()
to find a basis of the homogeneous solutions.
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)
。)
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.)
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()
會回傳其最簡階梯形式矩陣。
Let A
be a matrix. Then A.rref()
returns its reduced echelon form.
A = matrix(3, list(range(12)))
show(A)
show(A.rref())
若 A
為一矩陣而 b
為一向量﹐則 A.augment(b)
會回傳相對應的增廣矩陣。
Let A
be a matrix and b
a vector. Then A.augment(b)
returns their augmenting matrix.
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
執行列運算。
A.swap_rows(i,j)
.A.rescale(i, k)
.A.add_multiple_of_row(i, j, k)
.注意這些函數會直接修改矩陣本身而不會回傳任何矩陣。
Apply the row operation to a matrix (or an augmenting matrix) A
by the following functions.
A.swap_rows(i,j)
.A.rescale(i, k)
.A.add_multiple_of_row(i, j, k)
.Notice that these functions update the matrix itself and do not return any matrix.
A = matrix(3, list(range(12)))
show(A)
print(A.swap_rows(0,1))
show(A)
Inverse matrix
若 A
和 B
都是矩陣﹐則 A.augment(B)
也可以建立其相對應的增廣矩陣。
藉此可以計算反矩陣。
Let A
and B
be matrices. Then A.augment(B)
also returns the corresponding augmenting matrix. We may use it to compute the inverse.
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
是方陣﹐
A.is_invertible()
可以判斷其是否可逆﹐A.inverse()
會求出它的逆矩陣。Suppose A
is a square matrix. Then
A.is_invertible()
determines if A
is invertible, andA.inverse()
returns the inverse of A
.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
裡。
可以用
from lingeo import random_good_matrix
來匯入須要的函數。
lingeo
is a library written for LA-notebook. Its content can be found in lingeo.py
, and you may use the syntax like
from lingeo import random_good_matrix
to import the necessary functions.
from lingeo import random_good_matrix
from lingeo import row_space_matrix, column_space_matrix, kernel_matrix, left_kernel_matrix
若 A
為一矩陣﹐則
row_space_matrix(A)
的列是由 $\beta_R$ 組成、kernel_matrix(A)
的行是由 $\beta_K$ 組成、column_space_matrix(A)
的行是由 $\beta_C$ 組成、left_kernel_matrix(A)
的列是由 $\beta_L$ 組成。Let A
be a matrix. Then
row_space_matrix(A)
is the matrix whose rows are vectors in $\beta_R$,kernel_matrix(A)
is the matrix whose columns are vectors in $\beta_K$,column_space_matrix(A)
is the matrix whose columns are vectors in $\beta_C$, andleft_kernel_matrix(A)
is the matrix whose rows are vectors in $\beta_L$.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))
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))