版本比较
线性代数学习笔记
正文
增加 13 行删除 13 行
86 行未改动87888990919192939495959697989945 行未改动14514614714814914915015115215348 行未改动20220320420520620620720820921011 行未改动22222322422522622622722822923010 行未改动24124224324424524524624724824914 行未改动26426526626726826826927027127222 行未改动29529629729829929930030130230330330430530630726 行未改动334335336337338338339340341342104 行未改动全部展开44744844945045145145245345445515 行未改动47147247347447547547647747847948048148248248348448548618 行未改动86 行未改动 int n; vector<vector<long long> > m; Mtr () { n = 0; } } Mtr (int x) { n = x; m = vector<vector<long long> >(n, vector<long long>(n, 0)); } }};```
对于矩阵乘法,我们可以通过重载运算符来实现:45 行未改动
比如斐波那契的递推式,正常递推是 $O(n)$ 的:
$$F_i = F_i =\begin{cases}1&i=1,2\\F_{i-1}+F_{i-2}& \text{Otherwise.}\end{cases}48 行未改动F_{i-2}\\F_{i-1}\end{pmatrix}
= =
\begin{pmatrix}F_{i-1}\\F_i11 行未改动
再根据 $F_{i}=F_{i-1}+F_{i-2}$ 就可以得到:
$$A = A =\begin{pmatrix}0&1\\1&1\end{pmatrix}10 行未改动F_{i-2}\\F_{i-1}\end{pmatrix}
= =
\begin{pmatrix}F_{i-1}\\F_i14 行未改动1\\1\end{pmatrix}
= =
\begin{pmatrix}F_{n-1}\\F_n22 行未改动 int n; vector<vector<long long> > m; Mtr () { n = 0; } } Mtr (int x) { n = x; m = vector<vector<long long> >(n, vector<long long>(n, 0)); } }};Mtr operator*(Mtr x, Mtr y) { Mtr ans = Mtr(x.n); for (int i = 0; i < ans.n; i++)26 行未改动 cout << 1 << endl; else cout << (fpow(A, n - 2) * B).m[1][0] << endl; return 0;} }```
# 4.高斯消元
104 行未改动全部展开
```cpp#include <iostream>#include <cstdio>#include <cmath> #include <cmath>#include <vector>#include <cstring>#include <algorithm>using namespace std;15 行未改动vector<double> operator*(vector<double> x, double y) { vector<double> ans((int)x.size()); for (int i = 0; i < (int)ans.size(); i++) ans[i] = x[i] * y; return ans; return ans;}bool Gauss() { for (int i = 0; i < n; i++) { if (abs(a[i][i]) < 1e-9) return false; a[i] = a[i] * (1.0 / a[i][i]); for (int j = 0; j < n; j++) for (int j = 0; j < n; j++) if (i != j) a[j] = a[j] - a[i] * a[j][i]; } return true;18 行未改动