矩阵(线性回归中的应用)
我们在多元线性回归中使用了加减乘除之外的几个定义
上篇,请点手撕多元线性回归
- 零矩阵(null matrix)
- 行列式(determinant)
- 伴随矩阵(conjugate)
- 逆(inverse)
零矩阵(null matrix)
所有元素皆为0的矩阵
我们改造一下之前的类,除了用数组初始化之外,还可以用m和n(行和列)初始化
class Matrix {
constructor(m, n) {
if (Array.isArray(m)) {
// 非数字用作数组
this._arr = m;
} else {
if (m >= 1 && n >= 1) {
// 数字的话必须行列大于等于1
this._arr = new Array(m).fill(0).map(() => new Array(n).fill(0));
} else {
throw(new Error('m & n must le 1'));
}
}
}
}
实际操作一下,创造一个3行2列的零矩阵
// node --experimental-modules mat-advance.mjs
import Matrix from './matrix.mjs';
console.log('零矩阵', new Matrix(3, 2));
看结果
零矩阵 Matrix {
_arr: [
[ 0, 0 ],
[ 0, 0 ],
[ 0, 0 ]
]
}
行列式(determinant)
在 n 维欧几里得空间中,行列式描述的是一个线性变换对“体积”所造成的影响
行列式计算必须是方阵,也就是m等于n的矩阵
表达式
D = |A| = detA = det(aij)
D = ∣ A ∣ = ∣ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a n 1 a n 2 ⋯ a n n ∣ = ∑ ( − 1 ) k a 1 k 1 a 2 k 2 ⋯ a n k n D=\begin{vmatrix}A\end{vmatrix}=\begin{vmatrix} a_{11}&a_{12}&\cdots&a_{1n} \\ a_{21}&a_{22}&\cdots&a_{2n} \\ \vdots&\vdots&\ddots&\vdots \\ a_{n1}&a_{n2}&\cdots&a_{nn} \end{vmatrix} =\sum{(-1)^{k}a_{1k_{1}}a_{2k_{2}}\cdots a_{nk_{n}}} D=∣∣A∣∣=∣∣∣∣∣∣∣∣∣a11a21⋮an1a12a22⋮an2⋯⋯⋱⋯a1na2n⋮ann∣∣∣∣∣∣∣∣∣=∑(−1)ka1k1a2k2⋯ankn
示例
∣ 1 2 3 4 ∣ = 1 × 4 − 2 × 3 = 4 − 6 = − 2 \begin{vmatrix} 1&2 \\ 3&4 \end{vmatrix} =1\times4-2\times3=4-6=-2 ∣∣∣∣1324∣∣∣∣=1×4−2×3=4−6=−2
∣ 1 2 3 4 5 6 7 8 9 ∣ = 1 × 5 × 9 + 2 × 6 × 7 + 3 × 4 × 8 − 1 × 6 × 8 − 2 × 4 × 9 − 3 × 5 × 7 = 45 + 84 + 96 − 48 − 72 − 105 = 0 \begin{vmatrix} 1&2&3 \\ 4&5&6 \\ 7&8&9 \end{vmatrix} =1\times5\times9+2\times6\times7+3\times4\times8-1\times6\times8-2\times4\times9-3\times5\times7=45+84+96-48-72-105=0 ∣</