版本比较
线性代数
正文
增加 147 行删除 2 行
4 行未改动56789910111213141516370 行未改动全部展开3873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524533914544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355364 行未改动向量线性相关:两条向量在同一直线上,此时变换完的空间会被压少若干维。反之则不是线性相关。
线性变换:经过一个 $A$ 的线性变换后,得到的坐标系仍然平行且等距。
秩(rank):行列式在空间上形成的维度,或是高斯消元时的主元(非 0)个数。秩(rank):行列式在空间上形成的维度,或是高斯消元时的主元(非 0)个数,即他们线性无关。
> 貌似下面有一些地方把 基向量 写成 向量基 了,请仔细甄别。
### 矩阵乘法
若现在有两个矩阵 $A,B$,定义他们的矩阵乘为:
370 行未改动全部展开\end{bmatrix}$$
其中 $a,b,c$ 是上面通过计算已知数。这样不需要回代。
::::info[code]```cpp#include <bits/stdc++.h>#define int long long#define mk make_pair#define fi first#define se second#define pb push_backconst int N = 1e2 + 10, M = 2e2 + 10, inf = 1e18, mod = 2009;const double eps = 1e-6; using namespace std;typedef pair<int, int> pii;int n;double a[N][N];int rnk; // 秩 void gauss(){ vector<int> where(n + 1); for(int col = 1; col <= n; col++) // 枚举的是列 { int p = rnk + 1; while(p <= n && fabs(a[p][col]) < eps) p++; // 找当前列的非 0 元素 if(p > n) continue; swap(a[p], a[++rnk]); where[col] = rnk; double pivot = a[rnk][col]; // 先存下主元的系数,不然一会会被直接除成 0 for(int j = col; j <= n + 1; j++) a[rnk][j] /= pivot; // 这里枚举的是列 for(int i = 1; i <= n; i++) // Jordan 直接全部删完 现在枚举的是行 { if(i == rnk || fabs(a[i][col]) < eps) continue; double t = a[i][col]; for(int j = col; j <= n + 1; j++) a[i][j] -= t * a[rnk][j]; } } for(int i = 1; i <= n; i++) { bool zro = 1; for(int j = 1; j <= n; j++) if(fabs(a[i][j]) > eps) { zro = 0; break; } if(zro && fabs(a[i][n + 1]) > eps) return cout << "-1", void(); } if(rnk != n) return cout << "0\n", void(); for(int i = 1; i <= n; i++) { cout << "x" << i << "=";// cout << where[i] << "\n"; cout << fixed << setprecision(10) << a[where[i]][n + 1] << "\n"; }}signed main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for(int i = 1; i <= n; i++) for(int j = 1; j <= n + 1; j++) cin >> a[i][j]; gauss(); return 0;}/**/```::::
[更高级的模板](https://judge.yosupo.jp/problem/system_of_linear_equations)
计算出特解后还要计算出方程组的基。方程组的基就是对于每个 $x_i$ 求出它为 $1$ 时其他 $x_j$ 用 $a_jx_i$ 来表示时 $a_j$ 的值。要求输出方程组 $A_x=0$ 解空间的维数,一组特解和每个维度上的一个基。
我们之前求出来的秩是线性无关的主元个数,但是要输出的是维度,维度就是 解可以自由移动的空间。自由移动,那自然就是自由元造成的。自由元可以任意取值,能造成解的移动,那么第一问输出 $m-rank$。
一组特解就和上面那个是一样的。每一个维度上的基就是把这一维上的自由元设为 $1$ 后求出其他解。几何意义就是这个空间上的一个基向量。
::::info[code]```cpp#include <bits/stdc++.h>#define int long long#define mk make_pair#define fi first#define se second#define pb push_backconst int N = 1e3 + 10, M = 2e2 + 10, inf = 1e18, mod = 998244353;const double eps = 1e-6; using namespace std;typedef pair<int, int> pii;int n, m, a[N][N];int power(int a, int b){ int ans = 1; while(b) { if(b & 1) ans = ans * a % mod; a = a * a % mod, b >>= 1; } return ans;}int rnk;void gauss(){ vector<int> where(m + 1); for(int col = 1; col <= m && rnk < n; col++) { int p = rnk + 1; while(p <= n && a[p][col] == 0) p++; if(p > n) continue; swap(a[p], a[++rnk]); where[col] = rnk; int inv = power(a[rnk][col], mod - 2); for(int j = col; j <= m + 1; j++) (a[rnk][j] *= inv) %= mod; for(int i = 1; i <= n; i++) { if(i == rnk || a[i][col] == 0) continue; int t = a[i][col]; for(int j = col; j <= m + 1; j++) (a[i][j] = a[i][j] - t * a[rnk][j] % mod + mod) %= mod; } } for(int i = 1; i <= n; i++) { bool zro = 1; for(int j = 1; j <= m; j++) zro &= (a[i][j] == 0); if(zro && a[i][m + 1] != 0) return cout << "-1\n", void(); } cout << m - rnk << "\n"; for(int i = 1; i <= m; i++) cout << a[where[i]][m + 1] << " "; cout << "\n"; for(int i = 1; i <= m; i++) { if(where[i]) continue; for(int j = 1; j <= m; j++) { if(i == j) cout << 1 << " "; else cout << (-a[where[j]][i] + mod) % mod << " "; } }}signed main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> a[i][j]; for(int i = 1; i <= n; i++) cin >> a[i][m + 1]; gauss(); return 0;}/**/```::::
### 线性基