Skip to content

Matrix - 矩陣

矩陣運算,矩陣的元素的型態為 EF - 擴張體

AQ(s)n×m , sZA \in { \mathbb{Q}(\sqrt{s})^{ n \times m } } ~,~ s \in \mathbb{Z}

就把 float C\mathbb{C} 當作 Q(1)\mathbb{Q}(\sqrt{-1}) 吧 ...,反正基底 s\sqrt{s} 要一樣就是了。

Import

js
import { Matrix } from "ran-math";

Properties & Methods

建構子:Matrix constructor

PropertyTypeDescription
.nnumber ( int >= 1 )矩陣的列數
.mnumber ( int >= 1 )矩陣的行數
.arrArray<Array<EF>>矩陣
MethodDescription
Matrix.isMatrix檢查輸入值是否為 Matrix 的實例
Matrix.createI生成單位矩陣 II
.copy回傳一個相同值的新實例
.toStr轉字串
.swapRow矩陣列運算:交換 i,ji, j 兩列 ( 不回傳新矩陣 )
.scaleRow矩陣列運算:列 ii 乘純量 ss (不回傳新矩陣)
.addRow矩陣列運算:列 ii 乘純量 ss 加到列 jj (不回傳新矩陣)
.trans轉置
.inverse反矩陣
.add矩陣加法
.mul矩陣乘法
.muls純量乘矩陣

Matrix.isMatrix

檢查參數 value 是否為 Matrix 的實例,與 value instanceof Matrix 等價。

js
Matrix.isMatrix(value: any): boolean
ParamTypeDescription
valueany要檢查的值

範例:

js
Matrix.isEF(new Matrix(3, 3, (i, j) => i + j)) // true
Matrix.isEF([[1, 2], [3, 4]])                  // false

Matrix.createI

生成 nn 階單位矩陣 InI_n

js
Matrix.createI(n: number): Matrix
ParamTypeDescription
nnumber ( int >= 1 )nn 必須為正整數

範例:

js
Matrix.createI(3) // 生成 3*3 的單位矩陣
// | 1 | 0 | 0 |
// | 0 | 1 | 0 |
// | 0 | 0 | 1 |

Matrix constructor

類別 Matrix 的建構子。

AQ(s)n×m , sZA \in { \mathbb{Q}(\sqrt{s})^{ n \times m } } ~,~ s \in \mathbb{Z}

js
new Matrix(
	n: number,
	m: number,
	initFunc: (i: number, j: number) => number | Frac | EF
): Matrix
ParamTypeDescription
nnumber ( int >= 1 )矩陣的列數
mnumber ( int >= 1 )矩陣的行數
initFunc(i: number, j: number) => number | Frac | EF回傳矩陣元素 AijA_{ij}

範例:

js
// 生成 3*3 零矩陣
let matrix_zero = new Matrix(3, 3, () => 0);
js
// 生成 R^3 向量 [ 2, -3/4, 2+√2 ]^T
const testVec = [2, F(-3, 4), new EF(2, 1, 2)]
let matrix_vec3 = new Matrix(3, 1, i => testVec[i]);
js
// Array<Array> 轉 Matrix
const testArr = [
	[1, 2, 3],
	[4, 5, 6]
];
let matrix_test = new Matrix(2, 3, (i, j) => testArr[i][j]);

.n .m .arr

js
Matrix#n: number             // 矩陣的列數
Matrix#m: number             // 矩陣的行數
Matrix#arr: Array<Array<EF>> // 矩陣, 元素為 EF 型態

CAUTION

屬性 n m arr 為唯讀 ( read only )。

.copy

回傳一個相同值的新實例。

js
Matrix.prototype.copy(): Matrix

範例:

js
new Matrix(3, 3, (i, j) => i + j).copy()
// | 0 | 1 | 2 |
// | 1 | 2 | 3 |
// | 2 | 3 | 4 |

.toStr

轉 debug 字串。

js
Matrix.prototype.toStr(): string

範例:

js
const matrix = new Matrix(3, 3,
	(i, j) => new EF(F(i-j, i+2), F(i-j+1, j+2), F((i+j+1)*j, 7))
);
console.log(matrix.toStr());
// | 0   | -1/2           | -1 + -1/28 √ 42 |
// | 1/3 | 1/21 √ 21      | -1/3            |
// | 1/2 | 1/4 + 4/21 √ 7 | 1/28 √ 70       |

.swapRow

矩陣列運算:交換 i,ji, j 兩列 ( 不回傳新矩陣 )。

js
Matrix.prototype.swapRow(i: number, j: number): void
ParamTypeDescription
inumber ( int >= 0 )ii
jnumber ( int >= 0 )jj

範例:

js
new Matrix(3, 3, (i, j) => i + j).swapRow(0, 1)
// | 0 | 1 | 2 |    | 1 | 2 | 3 | <- Swapped
// | 1 | 2 | 3 | -> | 0 | 1 | 2 | <- Swapped
// | 2 | 3 | 4 |    | 2 | 3 | 4 |

.scaleRow

矩陣列運算:列 ii 乘純量 ss (不回傳新矩陣)。

js
Matrix.prototype.scaleRow(i: number, s: number|Frac|EF): void
ParamTypeDescription
inumber ( int >= 0 )ii
snumber | Frac | EF純量 ss

範例:

js
new Matrix(3, 3, (i, j) => i + j).scaleRow(2, F(-1, 2))
// | 0 | 1 | 2 |    | 0  | 1    | 2  |
// | 1 | 2 | 3 | -> | 1  | 2    | 3  |
// | 2 | 3 | 4 |    | -1 | -3/2 | -2 | <- After *(-1/2)

.addRow

矩陣列運算:列 ii 乘純量 ss 加到列 jj (不回傳新矩陣)。

js
Matrix.prototype.addRow(i: number, j: number, s: number|Frac|EF): void
ParamTypeDescription
inumber ( int >= 0 )ii
jnumber ( int >= 0 )jj
snumber | Frac | EF純量 ss

範例:

js
new Matrix(3, 3, (i, j) => i + j).addRow(2, 0, F(-1, 2))
// | 0 | 1 | 2 |    | -1 | -1/2 | 0 | <- Added
// | 1 | 2 | 3 | -> | 1  | 2    | 3 |
// | 2 | 3 | 4 |    | 2  | 3    | 4 |

.trans

將矩陣轉置。

ATA^T

js
Matrix.prototype.trans(): Matrix

範例:

js
const testArr = [
	[5, 3, -7],
	[1, 0, 6],
	[2, 2, 3]
];
new Matrix(3, 3, (i, j) => testArr[i][j]).trans()
// | 5  | 1 | 2 |
// | 3  | 0 | 2 |
// | -7 | 6 | 3 |

.inverse

反矩陣,利用 Gauss-Jordan Elimination

A1;det(A)0A^{-1} \quad;\quad \det(A) \neq 0

js
Matrix.prototype.inverse(): Matrix

範例:

js
const testArr = [
	[5, -2, 3],
	[1, 2, -7],
	[0, -6, 2]
];
new Matrix(3, 3, (i, j) => testArr[i][j]).inverse()
// | 19/102 | 7/102  | -2/51   |
// | 1/102  | -5/102 | -19/102 |
// | 1/34   | -5/34  | -1/17   |

.add

矩陣加法。

A+B;A,BQ(s)n×m , sZA + B \quad;\quad A, B \in { \mathbb{Q}(\sqrt{s})^{ n \times m } } ~,~ s \in \mathbb{Z}

js
Matrix.prototype.add(matrix: Matrix): Matrix
ParamTypeDescription
matrixMatrix矩陣 BB

範例:

js
const matrix = new Matrix(3, 3, (i, j) => i + j);
matrix.add(matrix)
// | 0 | 2 | 4 |
// | 2 | 4 | 6 |
// | 4 | 6 | 8 |

.mul

矩陣乘法。

AB;AQ(s)n×m , BQ(s)m×l , sZAB \quad;\quad A \in { \mathbb{Q}(\sqrt{s})^{ n \times m } } ~,~ B \in { \mathbb{Q}(\sqrt{s})^{ m \times l } } ~,~ s \in \mathbb{Z}

js
Matrix.prototype.mul(matrix: Matrix): Matrix
ParamTypeDescription
matrixMatrix矩陣 BB

範例:

js
const matrix = new Matrix(3, 3, (i, j) => i + j);
matrix.mul(matrix)
// 懶得 copy, 你執行這段 code 就知道了

.muls

純量乘矩陣。

sA;sQ(s) , AQ(s)n×m , sZsA \quad;\quad s \in { \mathbb{Q}(\sqrt{s}) } ~,~ A \in { \mathbb{Q}(\sqrt{s})^{ n \times m } } ~,~ s \in \mathbb{Z}

js
Matrix.prototype.muls(s: number|Frac|EF): Matrix
ParamTypeDescription
snumber | Frac | EF純量 ss

範例:

js
const matrix = new Matrix(3, 3, (i, j) => i + j);
matrix.muls(2)
// | 0 | 2 | 4 |
// | 2 | 4 | 6 |
// | 4 | 6 | 8 |