计算逆矩阵
/*
* 時間復雜度 O(n^3)
* 輸入 a 原矩陣
* c 逆矩陣
* n 矩陣的階數
*
* 函數說明:將原矩陣a和一個單位矩陣E作成一個大矩陣(a,E),
* 用初等變換將大矩陣中的a變成E,則會得到(E,a-1)的形式
*/
vector<double> operator * (vector<double> a, double b){int n = a.size();vector<double>res (n, 0);for (int i = 0; i < n; i++){res[i] = a[i]*b;}return res;
}
vector<double> operator - (vector<double>a, vector<double> b){int n = a.size();vector<double> res(n, 0);for (int i = 0; i < n; i++){res[i] = a[i]-b[i];}return res;
}
void inverse(vector<double> a[], vector<double> c[], int n){for (int i = 0; i < n; i++){c[i] = vector<double>(n, 0);}for (int i = 0; i < n; i++){c[i][i] = 1;}for (int i = 0; i < n; i++){for (int j = i; j < n; j++){if (fabs(a[j][i]) > eps){swap(a[i], a[j]);swap(c[i], c[j]);break;}}c[i] = c[i] * (1.0/a[i][i]);a[i] = a[i] * (1.0/a[i][i]);for (int j = 0; j < n; j++){if (i != j && fabs(a[j][i]) > eps){c[j] = c[j] - c[i] * a[j][i];a[j] = a[j] - a[i] * a[j][i];}}}
}
總結
- 上一篇: confluence的一些环境变量说明
- 下一篇: ftp主动模式和被动模式的区别