求逆矩阵算法
#include #include #include
using namespace std; #define DataType double
/*矩阵基本数据结构*/ typedef struct { int row,col; DataType** mat; /*初始化矩阵,即创建矩阵*/ int Init_Mat(Matrix* m) { int i=0,j=0; co**pdata=m->mat=new DataType*[m->row];//申请行指针数组
if (NULL==pdata) { cout
cout
return 0; }
/*输出矩阵*/ void Out_Mat(Matrix* m) { int i,j; coutrow;i++) { for (j=0;jcol;j++)
{ coutmat)[i][j] cout
/*矩阵乘法,t=m1*m2*/ int Mul_Mat(Matrix* m1,Matrix* m2,Matrix* t) { in if (m1->col != m2->row) { cout
for (j=0;jcol;j++) { for (t->mat[i][j]=0,p=0;pcol;p++) { t->mat[i][j]+=m1->mat[i][p]*m2->mat[p][j]; } } }
return 0; }
/*矩阵求逆——全选主元高斯-约当(Gauss-Jordan)法*/ int Inv_Mat(Matrix *m,Ma if(m->row!=m->col) { cout
if(NULL==t->mat) { cout
n=m->row; t->row=m->row; t->col=m->col; is=new int[n]; js=new if (NULL==is || NULL==js) { cout
for (i=k;imat[i][j]); if (p>d) { d=p; is[k]=i; js[k]=j; } } } if(1.0==d+1.0) { delete []is; delete []js; printf(
{ p=t->mat[k][j]; t->mat[k][j]=t->mat[is[k]][j]; t->mat[is[k]][j]= if(js[k] != k) /*列交换*/ { for (i=0;i
{ p=t->mat[i][k]; t->mat[i][k]=t->mat[i][js[k]]; t->mat[i][js[k]]= t->mat[k][k]=1/t->mat[k][k];
for (j=0;jmat[k][j]=t->mat[k][j]*t->mat for (i=0;imat[i][j]=t->mat[i][j]-t->mat[i][k]*t->mat[k][j]; } } for (i=0;imat[i][k]=-t->mat[i][k]*t->mat[
for (k=n-1;k>=0;k--) { if (js[k]!=k) { for (j=0;j
{ p=t->mat[k][j]; t->mat[k][j]=t->mat[js[k]][j]; t->mat[js[k]][j]= if (is[k] != k) { for (i=0;i
{ p=t->mat[i][k]; t->mat[i][k]=t->mat[i][is[k]]; t->mat[i][is[k]]= delete []is; delete []js; return 0; }
/*求矩阵行列式值——全选主元高斯消去法*/ DataType Det_Mat(Matrix *m,Matrix f=1.0,det=1.0,q,d; if(m->row!=m->col) { cout
if(NULL==t->mat) { cout
n=m->row; t->row=m->row; t->col=m->col;
for(k=0;k
for(i=k;imat[i][j]); if(d>q) { q=d; is=i; js=j; } }
if(1.0==q+1.0) { det=0.0; return(det); }
if(is!=k) { f=-f;
for(j=k;jmat[k][j]; t->mat[k][j]=t->mat[is][j]; if(js!=k) { f=-f;
for(i=k;imat[i][js]; t->mat[i][js]=t->mat[i][k];
det=det*t->mat[k][k];
for(i=k+1;imat[i][k]/t->mat[k][k];
for(j=k+1;jmat[i][j]-=d*t->mat[k][j]; } } det=f*det*t->mat[n-1][n-1]; return (det); }
//主函数 int main() {
Matrix m1,ans;
Init_Mat(&m1);//输入矩阵 Out_Mat(&m1); //输出原始矩阵 /*求矩阵行列式值 Inv_Mat(&m1,&ans);//求逆矩阵 Out_Mat(&ans);//输出逆矩阵 getchar();
求逆矩阵算法
#include #include #include
using namespace std; #define DataType double
/*矩阵基本数据结构*/ typedef struct { int row,col; DataType** mat; /*初始化矩阵,即创建矩阵*/ int Init_Mat(Matrix* m) { int i=0,j=0; co**pdata=m->mat=new DataType*[m->row];//申请行指针数组
if (NULL==pdata) { cout
cout
return 0; }
/*输出矩阵*/ void Out_Mat(Matrix* m) { int i,j; coutrow;i++) { for (j=0;jcol;j++)
{ coutmat)[i][j] cout
/*矩阵乘法,t=m1*m2*/ int Mul_Mat(Matrix* m1,Matrix* m2,Matrix* t) { in if (m1->col != m2->row) { cout
for (j=0;jcol;j++) { for (t->mat[i][j]=0,p=0;pcol;p++) { t->mat[i][j]+=m1->mat[i][p]*m2->mat[p][j]; } } }
return 0; }
/*矩阵求逆——全选主元高斯-约当(Gauss-Jordan)法*/ int Inv_Mat(Matrix *m,Ma if(m->row!=m->col) { cout
if(NULL==t->mat) { cout
n=m->row; t->row=m->row; t->col=m->col; is=new int[n]; js=new if (NULL==is || NULL==js) { cout
for (i=k;imat[i][j]); if (p>d) { d=p; is[k]=i; js[k]=j; } } } if(1.0==d+1.0) { delete []is; delete []js; printf(
{ p=t->mat[k][j]; t->mat[k][j]=t->mat[is[k]][j]; t->mat[is[k]][j]= if(js[k] != k) /*列交换*/ { for (i=0;i
{ p=t->mat[i][k]; t->mat[i][k]=t->mat[i][js[k]]; t->mat[i][js[k]]= t->mat[k][k]=1/t->mat[k][k];
for (j=0;jmat[k][j]=t->mat[k][j]*t->mat for (i=0;imat[i][j]=t->mat[i][j]-t->mat[i][k]*t->mat[k][j]; } } for (i=0;imat[i][k]=-t->mat[i][k]*t->mat[
for (k=n-1;k>=0;k--) { if (js[k]!=k) { for (j=0;j
{ p=t->mat[k][j]; t->mat[k][j]=t->mat[js[k]][j]; t->mat[js[k]][j]= if (is[k] != k) { for (i=0;i
{ p=t->mat[i][k]; t->mat[i][k]=t->mat[i][is[k]]; t->mat[i][is[k]]= delete []is; delete []js; return 0; }
/*求矩阵行列式值——全选主元高斯消去法*/ DataType Det_Mat(Matrix *m,Matrix f=1.0,det=1.0,q,d; if(m->row!=m->col) { cout
if(NULL==t->mat) { cout
n=m->row; t->row=m->row; t->col=m->col;
for(k=0;k
for(i=k;imat[i][j]); if(d>q) { q=d; is=i; js=j; } }
if(1.0==q+1.0) { det=0.0; return(det); }
if(is!=k) { f=-f;
for(j=k;jmat[k][j]; t->mat[k][j]=t->mat[is][j]; if(js!=k) { f=-f;
for(i=k;imat[i][js]; t->mat[i][js]=t->mat[i][k];
det=det*t->mat[k][k];
for(i=k+1;imat[i][k]/t->mat[k][k];
for(j=k+1;jmat[i][j]-=d*t->mat[k][j]; } } det=f*det*t->mat[n-1][n-1]; return (det); }
//主函数 int main() {
Matrix m1,ans;
Init_Mat(&m1);//输入矩阵 Out_Mat(&m1); //输出原始矩阵 /*求矩阵行列式值 Inv_Mat(&m1,&ans);//求逆矩阵 Out_Mat(&ans);//输出逆矩阵 getchar();