語料庫語言學基礎知識:矩陣(Haskell版)

語料庫語言學基礎知識寫作計劃:

  • 矩陣(Matlab, Python, R, Haskell)
  • 概率論(Matlab, Python, R, Haskell)
  • 統計學(Matlab, Python, R, Haskell)
  • 機器學習(Matlab, Python, R, Haskell)

主要參考資料:Michael Artin 的 Algebra

如果尚未安裝 Data.Matrix module,可以在命令行進行安裝:

cabal updatecabal install matrix

一、基本概念

"Prelude:" import Data.Matrix"Prelude Data.Matrix:" x = fromList 2 3 [2, 1, 0, 1, 3, 5] --定義 2*3 的矩陣x :: Num a => Matrix a"Prelude Data.Matrix:" print x( 2 1 0 )( 1 3 5 )it :: ()"Prelude Data.Matrix:" size = (nrows x, ncols x) --測量矩陣的行列數目size :: (Int, Int)"Prelude Data.Matrix:" size(2,3)it :: (Int, Int)"Prelude Data.Matrix:" getElem 1 2 x --矩陣的項X_ij,其中i為行指數,j為列指數1it :: Num a => a"Prelude Data.Matrix:" getElem 2 3 x 5it :: Num a => a"Prelude Data.Matrix:" a = zero 2 2 --建立2*2的方塊矩陣 square matrixa :: Num a => Matrix a"Prelude Data.Matrix:" a( 0 0 )( 0 0 )it :: Num a => Matrix a"Prelude Data.Matrix:" b = zero 2 3 --建立2*3的矩陣b :: Num a => Matrix a"Prelude Data.Matrix:" b( 0 0 0 )( 0 0 0 )it :: Num a => Matrix a"Prelude Data.Matrix:" r = fromList 1 3 [1,2,3] --行向量 row vectorr :: Num a => Matrix a"Prelude Data.Matrix:" r( 1 2 3 )it :: Num a => Matrix a"Prelude Data.Matrix:" c = fromList 3 1 [1,2,3] --列向量 column vectorc :: Num a => Matrix a"Prelude Data.Matrix:" c( 1 )( 2 )( 3 )it :: Num a => Matrix a

二、基本運算

"Prelude Data.Matrix:" elementwise (+) x b -- 矩陣的加法 addition (對應的元素相加,形狀不同則無法相加)( 2 1 0 )( 1 3 5 )it :: Num c => Matrix c"Prelude Data.Matrix:" scaleMatrix 2 x -- 數乘 scalar multiplication( 4 2 0 )( 2 6 10 )it :: Num a => Matrix a"Prelude Data.Matrix:" y = fromList 3 3 [1,2,3,4,5,6,7,8,9]y :: Num a => Matrix a"Prelude Data.Matrix:" multStd x y -- 矩陣乘法 matrix multiplication,m*n 矩陣只能和 n*l矩陣相乘( 6 9 12 )( 48 57 66 )it :: Num a => Matrix a"Prelude Data.Matrix:" elementwise (*) x (multStd x y) -- 元素對應相乘 Hadamard product( 12 9 0 )( 48 171 330 )it :: Num c => Matrix c

線性方程的矩陣表示

a_{11}x_1 + cdots + a_{1n}x_n = b_1\ a_{21}x_1 + cdots + a_{2n}x_n = b_2\ vdots hspace{2.2cm}vdots hspace{1cm}vdots\ a_{m1}x_1 + cdots + a_{mn}x_n = b_m

可以寫成 AX=B ,其中 XB 均為列向量。

矩陣的分配率

A(B+B)=AB + AB

矩陣的乘法結合律

(AB)C=A(BC)

標量位置可變性

c(AB)=(cA)B=A(cB)

矩陣乘法不滿足交換律

exists A,B [AB
eq BA]

"Prelude Data.Matrix:" zuo = fromList 2 2 [1,1,0,0]zuo :: Num a => Matrix a"Prelude Data.Matrix:" you = fromList 2 2 [2,0,1,1]you :: Num a => Matrix a"Prelude Data.Matrix:" multStd zuo you( 3 1 )( 0 0 )it :: Num a => Matrix a"Prelude Data.Matrix:" multStd you zuo( 2 2 )( 1 1 )it :: Num a => Matrix a --如果AB=BA,我們稱A和B可交換 commute

三、一些特殊的矩陣

-- 對角線項 diagonal entries; -- 對角矩陣diagonal matrix:所有非零項都是對角線項"Prelude Data.Matrix:" i = identity 2 --單位矩陣 identity matrix:對角線項為1的對角矩陣i :: Num a => Matrix a"Prelude Data.Matrix:" z = multStd x y -- 單位矩陣右可消除z :: Num a => Matrix a"Prelude Data.Matrix:" z( 6 9 12 )( 48 57 66 )it :: Num a => Matrix a"Prelude Data.Matrix:" multStd z (identity 3)( 6 9 12 )( 48 57 66 )it :: Num a => Matrix a"Prelude Data.Matrix:" multStd z (identity 3) == zTrueit :: Bool"Prelude Data.Matrix:" multStd i z == z -- 單位矩陣左可消除Trueit :: Bool"Prelude Data.Matrix:" luDecomp y -- 下三角lower triangular、上三角upper triangular、置換permutation、行列式 determinantJust (( 7.0 8.0 9.0 )( 0.0 0.8571428571428572 1.7142857142857144 )( 0.0 5.551115123125783e-17 1.1102230246251565e-16 ),( 1.0 0.0 0.0 )( 0.14285714285714285 1.0 0.0 )( 0.5714285714285714 0.5000000000000002 1.0 ),( 0.0 0.0 1.0 )( 1.0 0.0 0.0 )( 0.0 1.0 0.0 ),1.0)it :: (Fractional a, Ord a) => Maybe (Matrix a, Matrix a, Matrix a, a)"Prelude Data.Matrix:" Just (uppertri, lowertri, perm, det) = luDecomp ydet :: (Fractional d, Ord d) => dlowertri :: (Fractional d, Ord d) => Matrix dperm :: (Fractional d, Ord d) => Matrix duppertri :: (Fractional d, Ord d) => Matrix d"Prelude Data.Matrix:" uppertri( 7.0 8.0 9.0 )( 0.0 0.8571428571428572 1.7142857142857144 )( 0.0 5.551115123125783e-17 1.1102230246251565e-16 )it :: (Ord d, Fractional d) => Matrix d"Prelude Data.Matrix:" lowertri( 1.0 0.0 0.0 )( 0.14285714285714285 1.0 0.0 )( 0.5714285714285714 0.5000000000000002 1.0 )it :: (Ord d, Fractional d) => Matrix d"Prelude Data.Matrix:" perm( 0.0 0.0 1.0 )( 1.0 0.0 0.0 )( 0.0 1.0 0.0 )it :: (Ord d, Fractional d) => Matrix d"Prelude Data.Matrix:" det1.0it :: (Ord d, Fractional d) => d

四、逆矩陣

"Prelude Data.Matrix:" xin = fromList 3 3 [2,3,4,1,4,2,5,2,1]xin :: Num a => Matrix a"Prelude Data.Matrix:" inverse xinRight ( 1.1102230246251565e-16 -0.11111111111111116 0.2222222222222222 )( -0.2 0.4 0.0 )( 0.39999999999999997 -0.24444444444444446 -0.1111111111111111 )it :: (Eq a, Fractional a) => Either String (Matrix a)

引理1(左右逆等同)

A 為方塊矩陣,且有右逆(right inverse) R ,使得 AR=I ,並有左逆(left inverse) L ,使得 LA=I 。則 L=R 。故 A 可逆且 R 為其逆矩陣。

命題2

A,B 為可逆 n	imes n 矩陣,則它們的積 AB 亦可逆。 A 的逆矩陣 A^{-1} 亦可逆。且 (AB)^{-1}=B^{-1}A^{-1}(A^{-1})^{-1}=A

五、行列式(I)

"Prelude Data.Matrix:" detLaplace y --Laplace 方法,y的det為0,故不可逆0it :: Num a => a"Prelude Data.Matrix:" detLU y --LUDecomp中提取,即使y 是singular 可以測出6.661338147750939e-16it :: (Fractional a, Ord a) => a-- n 次一般線性群(n-dimensional general linear group)是 n×n 可逆矩陣的集合,-- 和與之一起的普通矩陣乘法運算-- 如果一個矩陣一整行或一整列均為0,那麼它是不可逆的

六、矩陣單位

"Prelude Data.Matrix:" e = zero 3 4e :: Num a => Matrix a"Prelude Data.Matrix:" setElem 1 (2,3) e --矩陣單位matrix unit e_2,3,最簡單的非零矩陣( 0 0 0 0 )( 0 0 1 0 )( 0 0 0 0 )it :: Num a => Matrix a--基basis:矩陣單位的集合--n次實數空間標準基standard basis:長度為n的列向量的矩陣單位的集合

七、初等矩陣

--Elementary matrices 對應著 elementary row operations--操作一:將一行的數量積加到另一行上"Prelude Data.Matrix:" m = fromList 5 5[1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9]m :: Num a => Matrix a"Prelude Data.Matrix:" e1 = identity 5 --生成 5x5 單位矩陣e1 :: Num a => Matrix a"Prelude Data.Matrix:" setElem 0.1 (4,2) e1 --set entry (4,2) to 0.1,形成Type i 初等矩陣-- Type i 初等矩陣是在單位矩陣的基礎上再加上一個非零項( 1.0 0.0 0.0 0.0 0.0 )( 0.0 1.0 0.0 0.0 0.0 )( 0.0 0.0 1.0 0.0 0.0 )( 0.0 0.1 0.0 1.0 0.0 )( 0.0 0.0 0.0 0.0 1.0 )it :: Fractional a => Matrix a"Prelude Data.Matrix:" e1 = setElem 0.1 (4,2) e1e1 :: Fractional a => Matrix a"Prelude Data.Matrix:" multStd e1 m --add to row 4, 0.1 times row 2( 1.0 2.0 3.0 4.0 5.0 )( 2.0 3.0 4.0 5.0 6.0 )( 3.0 4.0 5.0 6.0 7.0 )( 4.2 5.3 6.4 7.5 8.6 )( 5.0 6.0 7.0 8.0 9.0 )it :: Fractional a => Matrix a--操作二:交換兩個行"Prelude Data.Matrix:" e2 = identity 5e2 :: Num a => Matrix a"Prelude Data.Matrix:" e2 = switchRows 2 5 e2 --交換行 2 和 行5,生成Type ii 初等矩陣e2 :: Num a => Matrix a --Type ii 初等矩陣是通過上述操作生成的"Prelude Data.Matrix:" e2( 1 0 0 0 0 )( 0 0 0 0 1 )( 0 0 1 0 0 )( 0 0 0 1 0 )( 0 1 0 0 0 )it :: Num a => Matrix a"Prelude Data.Matrix:" multStd e2 m( 1 2 3 4 5 )( 5 6 7 8 9 )( 3 4 5 6 7 )( 4 5 6 7 8 )( 2 3 4 5 6 )it :: Num a => Matrix a-- 操作3:為一整行乘上非零數量"Prelude Data.Matrix:" e3 = identity 5e3 :: Num a => Matrix a"Prelude Data.Matrix:" e3 = setElem 1.271543 (2,2) e3 --Type iii 初等矩陣是將對角線上的一個項換位非零非一的數值e3 :: Fractional a => Matrix a"Prelude Data.Matrix:" e3( 1.0 0.0 0.0 0.0 0.0 )( 0.0 1.271543 0.0 0.0 0.0 )( 0.0 0.0 1.0 0.0 0.0 )( 0.0 0.0 0.0 1.0 0.0 )( 0.0 0.0 0.0 0.0 1.0 )it :: Fractional a => Matrix a"Prelude Data.Matrix:" multStd e3 m( 1.0 2.0 3.0 4.0 5.0 )( 2.543086 3.814629 5.086172 6.357715000000001 7.629258 )( 3.0 4.0 5.0 6.0 7.0 )( 4.0 5.0 6.0 7.0 8.0 )( 5.0 6.0 7.0 8.0 9.0 )it :: Fractional a => Matrix a-- 初等矩陣可逆,其逆亦初等"Prelude Data.Matrix:" inverse e1Right ( 1.0 0.0 0.0 0.0 0.0 )( 0.0 1.0 0.0 0.0 0.0 )( 0.0 0.0 1.0 0.0 0.0 )( 0.0 -0.1 0.0 1.0 0.0 )( 0.0 0.0 0.0 0.0 1.0 )it :: (Eq a, Fractional a) => Either String (Matrix a)"Prelude Data.Matrix:" inverse e2 -- 需要使用其他演算法修正結果Right ( NaN NaN NaN NaN NaN )( NaN NaN NaN NaN NaN )( NaN NaN NaN NaN NaN )( NaN NaN NaN NaN NaN )( NaN NaN NaN NaN NaN )it :: (Eq a, Fractional a) => Either String (Matrix a)"Prelude Data.Matrix:" inverse e3Right ( 1.0 0.0 0.0 0.0 0.0 )( 0.0 0.7864460737859434 0.0 0.0 0.0 )( 0.0 0.0 1.0 0.0 0.0 )( 0.0 0.0 0.0 1.0 0.0 )( 0.0 0.0 0.0 0.0 1.0 )it :: (Eq a, Fractional a) => Either String (Matrix a)-- row echelon matrix 階梯形矩陣 非唯一;Data.Matrix 有reduced ref 函數,返回唯一值,但需要行數比列數少"Prelude Data.Matrix:" rref zRight ( 1.0 0.0 -1.0 )( 0.0 1.0 2.0 )it :: (Eq a, Fractional a) => Either String (Matrix a)

八、轉置矩陣

"Prelude Data.Matrix:" transpose y -- transpose,將行列互換( 1 4 7 )( 2 5 8 )( 3 6 9 )it :: Num a => Matrix a

九、行列式(II)

det [a] = a

det egin{bmatrix} a & b \ c & d end{bmatrix} = ad - bc

"Prelude Data.Matrix:" a = fromList 1 1 [3]a :: Num a => Matrix a"Prelude Data.Matrix:" a( 3 )it :: Num a => Matrix a"Prelude Data.Matrix:" detLaplace a3it :: Num a => a"Prelude Data.Matrix:" b = fromList 2 2 [1,2,3,4]b :: Num a => Matrix a"Prelude Data.Matrix:" detLaplace b-2it :: Num a => a"Prelude Data.Matrix:" c = fromList 3 3 [1,2,3,4,5,6,7,8,9]c :: Num a => Matrix a"Prelude Data.Matrix:" detLaplace c0it :: Num a => a"Prelude Data.Matrix:" detLU e11.0it :: (Fractional a, Ord a) => a"Prelude Data.Matrix:" detLU e2-1.0it :: (Fractional a, Ord a) => a"Prelude Data.Matrix:" detLU e31.271543it :: (Fractional a, Ord a) => a

十、矩陣置換和余因子矩陣

參見luDecomp 中的P結果。

	ext{cof} (A)_{i,j}=(-1)^{i+j}det A_{ji}

定理3

An	imes n 矩陣,使 C=	ext{cof} Aalpha = det A 。如果 alpha 不為零,那麼 A 是可逆的,且 CA = AC = alpha I


推薦閱讀:

TAG:矩陣 | Haskell | 語料庫 |