在c或者c++里如何求一个矩阵的逆矩阵?

gchao3325 2003-12-04 04:53:03
数学上的手算方法是左边放原矩阵,右边放一个单位矩阵,然后对有这两个矩阵组成的新矩阵的行进行数乘和相加的初等运算(相信学过线性代数的人一定不会忘记),将左面的原矩阵变成单位矩阵,那么,右面的那个随原矩阵同时做相同运算的单位矩阵就变成了原矩阵的逆矩阵,这种算法对于维数少的还可以使用,但是随着维数的增多,运算量便以指数增长。想问一下,有没有人知道什么快捷的算法,使运算量降下来?
...全文
1928 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gchao3325 2003-12-11
  • 打赏
  • 举报
回复
zhouqingyuan(浪帆)说的另一种方法是什么?
laiben 2003-12-06
  • 打赏
  • 举报
回复
/*---------------------快速转置矩阵-------------------*/

#include <stdio.h>
#include <stdlib.h>

#define H 6 /* 行 */
#define L 7 /* 列 */
#define MAXSIZE 1250 /*非零个数的最大值*/

int M[H][L]={
{0,12,9,0,0,0,0},
{0,0,0,0,0,0,0},
{-3,0,0,0,0,14,0},
{0,0,24,0,0,0,0},
{0,18,0,0,0,0,0},
{15,0,0,-7,0,0,0}};

struct Triple
{
int i,j;
int e;
};

struct TSMatrix
{
struct Triple data[MAXSIZE+1];
int mu,nu,tu;
}; /* 稀疏矩阵的三元组顺序表存储表示 */

void createTSMatrix(struct TSMatrix *pt) /* 稀疏矩阵转为三元组存储 */
{ int wid,col,n=1;
for(wid=0;wid<H;wid++)
for(col=0;col<L;col++)
if(M[wid][col]&&n<=MAXSIZE) {
pt->data[n].i=wid+1;
pt->data[n].j=col+1;
pt->data[n].e=M[wid][col];
n++;
}
pt->mu=H;
pt->nu=L;
pt->tu=--n;
}


int FastTransposeSmatrix(struct TSMatrix *M,struct TSMatrix *T)
{ int col,t,p,q;
int num[MAXSIZE],cpot[MAXSIZE];
T->mu=M->mu;
T->nu=M->nu;
T->tu=M->tu;
if(T->tu) {
for(col=1;col<=M->nu;col++) num[col]=0;
for(t=1;t<=M->tu;t++) ++num[M->data[t].j];
cpot[1]=1;
for(col=2;col<=M->nu;col++) cpot[col]=cpot[col-1]+num[col-1];
for(p=1;p<=M->tu;p++) {
col=M->data[p].j;
q=cpot[col];
T->data[q].i=M->data[p].j;
T->data[q].j=M->data[p].i;
T->data[q].e=M->data[p].e;
++cpot[col];
}
return 1; /* OK! */
}
return 0; /* NULL */
}


void main()
{ int n;
struct TSMatrix *a=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
struct TSMatrix *b=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
createTSMatrix(a);
if(FastTransposeSmatrix(a,b)) {
printf("\na is :\n"); /* PRINT MATRIX */
printf(" i j v\n");
printf(" ---------\n");
if(a->tu)
for(n=1;n<=a->tu;n++)
printf("%4d%4d%4d\n",a->data[n].i,a->data[n].j,a->data[n].e);
printf("\na's transposematrix b is :\n"); /* PRINT MATRIX A */
printf(" i j v\n");
printf(" ---------\n");
if(b->tu)
for(n=1;n<=b->tu;n++)
printf("%4d%4d%4d\n",b->data[n].i,b->data[n].j,b->data[n].e);
}
else printf("There's no element in the matrix!");
}
laiben 2003-12-06
  • 打赏
  • 举报
回复
/*---------------------经典转置矩阵-------------------*/

#include <stdio.h>
#include <stdlib.h>

#define H 6 /* 行 */
#define L 7 /* 列 */
#define MAXSIZE 1250 /*非零个数的最大值*/

int M[H][L]={
{0,12,9,0,0,0,0},
{0,0,0,0,0,0,0},
{-3,0,0,0,0,14,0},
{0,0,24,0,0,0,0},
{0,18,0,0,0,0,0},
{15,0,0,-7,0,0,0}};

struct Triple
{
int i,j;
int e;
};

struct TSMatrix
{
struct Triple data[MAXSIZE+1];
int mu,nu,tu;
}; /* 稀疏矩阵的三元组顺序表存储表示 */

void createTSMatrix(struct TSMatrix *pt) /* 稀疏矩阵转为三元组存储 */
{ int wid,col,n=1;
for(wid=0;wid<H;wid++)
for(col=0;col<L;col++)
if(M[wid][col]&&n<=MAXSIZE) {
pt->data[n].i=wid+1;
pt->data[n].j=col+1;
pt->data[n].e=M[wid][col];
n++;
}
pt->mu=H;
pt->nu=L;
pt->tu=--n;
}


int transposeSMatrix(struct TSMatrix *M,struct TSMatrix *T) /* transpose M to T */
{ int p,q,col;
T->mu=M->mu;
T->nu=M->nu;
T->tu=M->tu;
if(T->tu) {
q=1;
for(col=1;col<=M->nu;col++)
for(p=1;p<=M->tu;p++)
if(M->data[p].j==col) {
T->data[q].i=M->data[p].j;
T->data[q].j=M->data[p].i;
T->data[q].e=M->data[p].e;
q++;
}
return 1; /* ok! */
}
else return 0; /* NULL */
}


void main()
{ int n;
struct TSMatrix *a=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
struct TSMatrix *b=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
createTSMatrix(a);
if(transposeSMatrix(a,b)) {
printf("\na is :\n"); /* PRINT MATRIX A */
printf(" i j v\n");
printf(" ---------\n");
if(a->tu)
for(n=1;n<=a->tu;n++)
printf("%4d%4d%4d\n",a->data[n].i,a->data[n].j,a->data[n].e);
printf("\na's transposematrix b is :\n"); /* PRINT MATRIX A */
printf(" i j v\n");
printf(" ---------\n");
if(b->tu)
for(n=1;n<=b->tu;n++)
printf("%4d%4d%4d\n",b->data[n].i,b->data[n].j,b->data[n].e);
}
else printf("There's no element in the matrix!");
}
cai114 2003-12-05
  • 打赏
  • 举报
回复
我也想找一个简单的方法
帮你UP
gchao3325 2003-12-05
  • 打赏
  • 举报
回复
我学线性代数求逆矩阵就学了初等变换和通过伴随矩阵除以行列式两种方法,楼上的能告诉我你的代码是用了什么算法,或者说理论依据是什么?
zhouqingyuan 2003-12-05
  • 打赏
  • 举报
回复
求逆矩阵数学上面有两种方法的,你说了一种,另外一种你可以用程序实现的,虽然有点麻烦,但一次使用后,可以多次使用的。
galaxy_fxstar 2003-12-05
  • 打赏
  • 举报
回复
hehe,游侠兄弟有意思!
我感觉用伴随距阵的方法应该会快一点!
gchao3325 2003-12-05
  • 打赏
  • 举报
回复
crcr(游侠)同志,你说的是求转制,不是逆
  • 打赏
  • 举报
回复
是这样的,设一个变量就可以了
大致思想是这样的,
void reverseMarix(int aa[M][N],int i,int j)
{
for(i=0;i<M;i++)
for(j=0;j<i;j++)
{
int tt=a[i][j];
aa[i][j]=aa[j][i];
a[j][i]=tt;
}
}
wjyhl 2003-12-04
  • 打赏
  • 举报
回复
矩阵的逆运算你可以参考很多的书,但是前提条件必须知道逆是什么意思,然后你才可以看懂程序,下面是个例子
说明:aryin是输入的矩阵,numrow行numcol列输出
void MatTxTix(double *aryin,int numrow,int numcol,double *aryout)
{
int i, j, k;

for(i=0;i<=numcol-1;++i)
for(j=0;j<=numcol-1;++j)
{
aryout[i*numcol+j]=0.0;
for(k=0;k<=numrow-1;++k)
aryout[i*numcol+j]=aryout[i*numcol+j]+aryin[k*numcol+j]*aryin[k*numcol+i];
}
}

69,380

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧