C++ program to find the inverse of matrix
Linear algebra matrix inversion is a headache. This article presents a lovely C++ program to help you inverse.
Basic idea-use adjoint matrix
From Baidu Encyclopedia
The adjoint matrix has a property: if A is invertible, then
AA*=|A|E;
so the inverse of matrix A is A* / |A|, here is the inverse of the matrix (square matrix) based on this feature:
Matrix inversion algorithm
If no algorithm is used, the solution is simply brute force, convenient and simple! Hehe!
- Find the algebraic remainder of each item in the matrix
- Find the value of each algebraic remainder to form the adjoint matrix A*
- Find the value of the original matrix |A|
- Divide each item of the adjoint matrix A* by |A|
In short, the following three functions are used
Find the algebraic remainder function
//计算方阵arcs每一行每一列的每个元素所对应的余子式,组成伴随矩阵ans[N]
void getAStart(double arcs[N][N],int n,double ans[N][N])
{
if(n==1)
{
ans[0][0] = 1;
return;
}
int i,j,k,t;
double temp[N][N];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n-1;k++)
{
for(t=0;t<n-1;t++)
{
temp[k][t] = arcs[k>=i?k+1:k][t>=j?t+1:t];
}
}
ans[j][i] = getA(temp,n-1);
if((i+j)%2 == 1)
{
ans[j][i] = - ans[j][i];
}
}
}
}
Determinant expansion evaluation function
//按第一行展开计算|A|
double getA(double arcs[N][N],int n)
{
if(n==1)
{
return arcs[0][0];
}
double ans = 0;
double temp[N][N]={
0.0};
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
for(k=0;k<n-1;k++)
{
temp[j][k] = arcs[j+1][(k>=i)?k+1:k];
}
}
double t = getA(temp,n-1);
if(i%2==0)
{
ans += arcs[0][i]*t;
}
else
{
ans -= arcs[0][i]*t;
}
}
return ans;
}
Function to find the inverse of a matrix
Just call this function directly
//得到给定矩阵src的逆矩阵保存到des中。
bool GetMatrixInverse(double src[N][N],int n,double des[N][N])
{
double flag=getA(src,n);
double t[N][N];
if(flag==0)
{
return false;
}
else
{
getAStart(src,n,t);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
des[i][j]=t[i][j]/flag;
}
}
}
return true;
}
Attach directly operable source code
#include<bits/stdc++.h>
using namespace std;
const int N = 3;//行列式的阶数
//按第一行展开计算|A|
double getA(double arcs[N][N],int n)
{
if(n==1)
{
return arcs[0][0];
}
double ans = 0;
double temp[N][N]={
0.0};
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
for(k=0;k<n-1;k++)
{
temp[j][k] = arcs[j+1][(k>=i)?k+1:k];
}
}
double t = getA(temp,n-1);
if(i%2==0)
{
ans += arcs[0][i]*t;
}
else
{
ans -= arcs[0][i]*t;
}
}
return ans;
}
//计算每一行每一列的每个元素所对应的余子式,组成A*
void getAStart(double arcs[N][N],int n,double ans[N][N])
{
if(n==1)
{
ans[0][0] = 1;
return;
}
int i,j,k,t;
double temp[N][N];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n-1;k++)
{
for(t=0;t<n-1;t++)
{
temp[k][t] = arcs[k>=i?k+1:k][t>=j?t+1:t];
}
}
ans[j][i] = getA(temp,n-1);
if((i+j)%2 == 1)
{
ans[j][i] = - ans[j][i];
}
}
}
}
//得到给定矩阵src的逆矩阵保存到des中。
bool GetMatrixInverse(double src[N][N],int n,double des[N][N])
{
double flag=getA(src,n);
double t[N][N];
if(flag==0)
{
return false;
}
else
{
getAStart(src,n,t);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
des[i][j]=t[i][j]/flag;
}
}
}
return true;
}
int main(){
double a[N][N];
double ans[N][N];
cout << " 请输入"<<N<<" 阶矩阵:"<<endl;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
cin >> a[i][j];
}
GetMatrixInverse(a,N,ans);
cout<<" 该矩阵的逆为:"<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
cout << ans[i][j]<<" ";
}
cout<<endl;
}
}