如果有相应的OJ题目,欢迎同学们提供相应的链接
相关链接
简单的测试
INPUT:
3
2 1 0
1 2 1
1 1 1
OUTPUT:
0.5 -0.5 0.5
0 1 -1
-0.5 -0.5 1.5
代码模板
inline 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;
}
inline 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;
}
/*
* AB=BA=I,B is the inverse of matrix A.
* SO,AB=I,A->I,I->C,thus IB=C,IB=B=C.
*
* TIME COMPLEXITY:O(n^3)
* PARAMS:
* A Raw matrix.
* C Inverse of raw matrix.
* N order.
*/
inline void inverse_matrix(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[i][j])>0){
swap(A[i],A[j]);
swap(C[i],C[j]);
break;
}
C[i]=C[i]*(1/A[i][i]);
A[i]=A[i]*(1/A[i][i]);
for(int j=0;j<N;j++)
if(j!=i && fabs(A[j][i])>0){
C[j]=C[j]-C[i]*A[j][i];
A[j]=A[j]-A[i]*A[j][i];
}
}
}