跳到正文

算法·理论

线性代数简记

stripe_python约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年6月2日
更新
2026年10月28日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

Guass-Jordan 消元

考虑线性方程组 Ax=bA \mathbf{x} = \mathbf{b},通过如下三种初等变换使得 AA 化为 II:

  • 交换两行;
  • 某一行乘上非零数 kk;
  • 某一行乘上数 kk 加到另一行。

形如 Ix=b′I \mathbf{x}=\mathbf{b'} 的方程显然可解。

矩阵求逆

初等变换本质是对矩阵的线性变换,其可以用初等矩阵 EE 表示,且 EE 可逆。

在 Guass-Jordan 消元的过程中,我们得到 EkEk−1⋯E1A=IE_k E_{k-1} \cdots E_1A = I。

因此有 A−1=EkEk−1⋯E1A^{-1}=E_k E_{k-1} \cdots E_1。称为 AA 的初等矩阵分解。

实际上,在消元过程中记录矩阵 BB,每次初等变换时对 BB 进行同样的操作,最终即有 B=A−1B=A^{-1}。复杂度 O(n3)O(n^3)。

行列式

定义

det⁡(A)=∑(−1)inv⁡(p)∏i=1nAi,pi\det (A)=\sum (-1)^{\operatorname{inv}(p)} \prod_{i=1}^n A_{i,p_i}

其中 pp 是一个 1∼n1 \sim n 的排列,inv⁡(p)\operatorname{inv}(p) 为逆序对数。暴力计算复杂度 O(n×n!)O(n \times n!)。

结论:

  • det⁡(AB)=det⁡(A)×det⁡(B)\det (AB)=\det (A) \times \det (B);
  • 矩阵 AA 可逆当且仅当 det⁡(A)≠0\det (A) \ne 0,此时 det⁡(A−1)=det⁡−1(A)\det(A^{-1})=\det^{-1}(A);
  • 上三角矩阵的行列式等于对角线元素之积。

由如上结论,在 Guass-Jordan 消元时,对于每一次初等变换记录行列式的变化即可计算,复杂度 O(n3)O(n^3)。

积和式

定义

per⁡(A)=∑∏i=1nAi,pi\operatorname{per}(A)=\sum \prod_{i=1}^n A_{i,p_i}

其中 pp 是一个 1∼n1 \sim n 的排列。计算一般矩阵的积和式是 #P-complete 问题。

组合意义是排列计数 / 二分图完美匹配计数。有 per⁡(A)≡det⁡(A)(mod2)\operatorname{per}(A) \equiv \det (A) \pmod 2。

Ryser 公式给出一个 O(n22n)O(n^2 2^n) 的求解方法:

per⁡(A)=(−1)n∑S⊆{1,2,⋯ ,n}(−1)∣S∣∏i=1n∑j∈SAi,j\operatorname{per}(A) =(-1)^n \sum _{S \subseteq \{1,2,\cdots,n\}} (-1)^{|S|} \prod_{i=1}^n \sum_{j \in S} A_{i,j}

Matrix Tree 定理

对于无向图 GG,定义:

  • 度数矩阵 DD:Di,iD_{i,i} 为节点 ii 的度数,其它位置为 00;
  • 邻接矩阵 GG:Di,jD_{i,j} 为 11 当且仅当 i,ji,j 有边,否则为 00。
  • Laplace 矩阵:L=D−GL=D-G。

Matrix Tree 定理指出,图 GG 的生成树个数等于 det⁡(L′)\det (L'),L′L' 是 LL 丢掉任意一行 / 列的矩阵。

如果无向图 GG 的边带权,则 det⁡(L′)=∑T∏e∈Twe\det (L')=\sum \limits_{T} \prod \limits_{e \in T} w_e。此时度数变为出边边权和,邻接矩阵需要记边权。

扩展:如果 GG 是有向图,则 det⁡(L′)=∑T∏e∈Twe\det (L')=\sum \limits_{T} \prod \limits_{e \in T} w_e。设 DD 是入度矩阵,L′L' 是 LL 丢掉 uu 行 uu 列的矩阵,则 L′L' 算的是以 uu 为根的根向树数目。

如果要算的是 ∑T∑e∈Twe\sum \limits_{T} \sum\limits_{e \in T} w_e,则可以令 Gi,j=1+wi,j×xG_{i,j}=1+w_{i,j} \times x,其中 xx 为形式幂级数的占位符,实现化积为和的效果。

板子

代码
C++
template <class mint, int n, int m = n>
struct matrix {
    mint v[n][m];
    matrix() : v{} {}
    matrix(initializer_list<initializer_list<mint>> a) : v{} {
        int i = 0, j = 0;
        for (const auto& r : a) {
            j = 0;
            for (const auto& c : r) {
                if (i < n && j < m) v[i][j] = c;
                j++;
            } i++;
        }
    }
    mint* operator[] (int i) {return v[i];}
    const mint* operator[] (int i) const {return v[i];}
    matrix& operator+= (const matrix& o) {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) v[i][j] += o[i][j];
        return *this;
    }
    matrix& operator-= (const matrix& o) {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) v[i][j] -= o[i][j];
        return *this;
    }
    matrix operator+ (const matrix& o) const {return matrix(*this) += o;}
    matrix operator- (const matrix& o) const {return matrix(*this) -= o;}
    template <int k> matrix<mint, n, k> 
    operator* (const matrix<mint, m, k>& o) const {
        matrix<mint, n, k> r;
        for (int a = 0; a < n; a++)
            for (int c = 0; c < m; c++)
                for (int b = 0; b < k; b++)
                    r[a][b] += v[a][c] * o[c][b];
        return r;
    }
    matrix& operator*= (const matrix& o) {
        static_assert(n == m, "");
        return *this = *this * o;
    }
    static matrix I() {
        static_assert(n == m, "");
        matrix r;
        for (int i = 0; i < n; i++) r[i][i] = 1;
        return r;
    } 
    bool operator== (const matrix& o) const {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) 
                if (v[i][j] != o[i][j]) return false;
        return true;
    }
    bool operator!= (const matrix& o) const {return !(*this == o);}
    pair<bool, matrix> inv(int _n = n) const {
        matrix A = *this, R = I();
        for (int i = 0; i < _n; i++) {
            int p = -1;
            for (int j = i; j < _n; j++) 
                if (A[j][i] != 0) {
                    p = j; break;
                }
            if (p == -1) return {0, R};
            if (i != p) for (int j = 0; j < _n; j++) 
                swap(A[i][j], A[p][j]), swap(R[i][j], R[p][j]);
            mint ip = ~A[i][i];
            for (int j = 0; j < _n; j++) A[i][j] *= ip, R[i][j] *= ip;
            for (int k = 0; k < _n; k++) {
                if (k == i || A[k][i] == 0) continue;
                mint t = A[k][i];
                for (int j = 0; j < _n; j++) A[k][j] -= t * A[i][j], R[k][j] -= t * R[i][j];
            }
        }
        return {1, R};
    }
    mint det(int _n = n) const {
        matrix A = *this; mint res = 1;
        for (int i = 0; i < _n; i++) {
            int p = -1;
            for (int j = i; j < _n; j++) 
                if (A[j][i] != 0) {
                    p = j; break;
                }
            if (p == -1) return 0;
            if (i != p) {
                res = -res;
                for (int j = 0; j < _n; j++) swap(A[i][j], A[p][j]);
            }
            for (int k = i + 1; k < _n; k++) 
                while (A[k][i] != 0) {
                    mint t = A[i][i].val / A[k][i].val;
                    for (int j = i; j < _n; j++) A[i][j] -= t * A[k][j];
                    for (int j = i; j < _n; j++) swap(A[i][j], A[k][j]);
                    res = -res;
                }
            res *= A[i][i];
        }
        return res;
    }
};
原文