網站首頁 編程語言 正文
先來一段百度百科上的搜索結果:
伴隨陣法
定理:n階矩陣為可逆的充分必要條件是A非奇異,且:
其中,是|A|中元素的代數余子式;矩陣
稱為矩陣A的伴隨矩陣,記作A*,于是有
用此方法求逆矩陣,對于小型矩陣,特別是二階方陣求逆既方便、快陣,又有規律可循。因為二階可逆矩陣的伴隨矩陣,只需要將主對角線元素的位置互換,次對角線的元素變號即可。
若可逆矩陣是二階或二階以上矩陣,在求逆矩陣的過程中,需要求9個或9個以上代數余子式,還要計算一個三階或三階以上行列式,工作量大且中途難免出現符號及計算的差錯。對于求出的逆炬陣是否正確,一般要通過來檢驗。一旦發現錯誤,必須對每一計算逐一排查。
下面我們來設計一下伴隨陣法矩陣求逆的C++代碼。
首先,需要自定義一個矩陣類型
#include<vector>
typedef vector<double> vec;
typedef vector<vec> mat;
然后,設計矩陣數乘的代碼
mat num_mul(mat A, double num) {
mat B(A.size(), vec(A[0].size()));
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[0].size(); j++)
B[i][j] = A[i][j] * num;
return B;
}
再寫一段計算伴隨矩陣的代碼
mat cutoff(mat A, int i, int j) { //切割,劃去第1行第i列
mat B(A.size() - 1, vec(A.size() - 1));
for(int c = 0; c < B.size(); c++)
for(int r = 0; r < B.size(); r++)
B[c][r] = A[c + (c >= i)][r + (r >= j)];
return B;
}
double det(mat A) {
if(A.size() == 1)
return A[0][0]; //當A為一階矩陣時,直接返回A中唯一的元素
double ans = 0;
for(int j = 0; j < A.size(); j++)
ans += A[0][j] * det(cutoff(A, 0, j)) * (j % 2 ? -1 : 1);
return ans;
}
mat company_mat(mat A) {
mat B(A.size(), vec(A.size()));
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B.size(); j++)
B[j][i] = det(cutoff(A, i, j)) * ((i + j) % 2 ? -1 : 1); //伴隨矩陣與原矩陣存在轉置關系
return B;
}
最后,把我原創的代碼分享給大家
#include<iostream>
#include<vector>
using namespace std;
typedef vector<double> vec;
typedef vector<vec> mat;
mat cutoff(mat A, int i, int j) { //切割,劃去第1行第i列
mat B(A.size() - 1, vec(A.size() - 1));
for(int c = 0; c < B.size(); c++)
for(int r = 0; r < B.size(); r++)
B[c][r] = A[c + (c >= i)][r + (r >= j)];
return B;
}
double det(mat A) {
if(A.size() == 1)
return A[0][0]; //當A為一階矩陣時,直接返回A中唯一的元素
double ans = 0;
for(int j = 0; j < A.size(); j++)
ans += A[0][j] * det(cutoff(A, 0, j)) * (j % 2 ? -1 : 1);
return ans;
}
mat company_mat(mat A) {
mat B(A.size(), vec(A.size()));
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B.size(); j++)
B[j][i] = det(cutoff(A, i, j)) * ((i + j) % 2 ? -1 : 1);
return B;
}
void output(mat A) {
cout << "......\n";
for(int i = 0; i < A.size(); i++) {
for(int j = 0; j < A[0].size(); j++)
printf("%.2lf ", A[i][j]);
cout << '\n';
}
cout << "......\n";
}
mat num_mul(mat A, double num) {
mat B(A.size(), vec(A[0].size()));
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[0].size(); j++)
B[i][j] = A[i][j] * num;
return B;
}
int main() {
int n;
scanf("%d", &n); //輸入階數
if(n == 0)
return 0;
mat A(n, vec(n));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%lf", &A[i][j]); //輸入A各行各列的元素
mat B = num_mul(company_mat(A), 1 / det(A));
output(B);
return 0;
}
原文鏈接:https://blog.csdn.net/qq_54180412/article/details/122943327
相關推薦
- 2022-07-16 BOM與DOM的進階知識
- 2022-12-12 C++?Boost?Bind庫示例分析使用_C 語言
- 2022-09-24 如何將一個CSV格式的文件分割成兩個CSV文件_python
- 2023-11-26 數據結構:數組—特殊矩陣的壓縮存儲
- 2023-03-13 pandas按某列降序的實現_python
- 2021-12-08 C++數組的定義詳情_C 語言
- 2023-07-26 vscode中配置代碼片段
- 2022-11-06 Golang常用包使用介紹_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支