公告

联系我:我的126邮箱: billhsu。 Locations of visitors to this page
<2010年4月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

统计

  • 随笔 - 41
  • 文章 - 0
  • 评论 - 82
  • 引用 - 0

常用链接

留言簿(16)

随笔分类

随笔档案

相册

Game Dev

搜索

  •  

最新评论

阅读排行榜

评论排行榜

矩阵求逆代码

12-12-2009更新:加入图形化界面

程序下载(含使用说明):http://www.cppblog.com/Files/billhsu/MatInv.rar

感觉线性代数作业里一直少不了矩阵求逆,

写个带输出算逆矩阵的步骤的矩阵求逆程序,希望给即将或正在学线代的同学一点方便。

代码写的不好,大家见谅。

/* ==================================
 *
 *  Copyright (C) Bill Hsu 
 *   
http://hi.baidu.com/probill
 *  2009-12-11
 **********************************
*/
#include  
< iostream >
#include  
< vector >
#include  
< math.h >
using     namespace   std;

typedef vector  
< float >   s_line;  //  用来表示一行
s_line line;

typedef vector  
< s_line >   s_matrix;  //  用来表示一个矩阵
s_matrix matrix;
s_matrix mat;
int   nSize;  //  矩阵维数
int   nSign;  //  标记行列式正负
void   outprint(s_matrix  &   _mat);
void   printstep(s_matrix  &   _mat);
int   step  =   0  ;
void   line_add(s_matrix  &   _mat,  int   a,  int   b,  float   k  =   1.0f  )  //  第b行乘k加到第a行
{
int   size  =  _mat[  0  ].size();

for  (  int   i  =   0  ;i  <  size;  ++  i)
{
_mat[a][i] 
+=  _mat[b][i]  *  k;

//  end for
}



void   work1(s_matrix  &   _mat)  //  主计算函数
{

for  (  int   i  =   1  ;i  <  nSize;  ++  i)
{

if  (fabs(_mat[i  -   1  ][i  -   1  ])  <   0.000001 )
{
int   mm;
for  (mm  =  i;mm  <  nSize;  ++  mm)
{
if  (fabs(_mat[mm  -   1  ][i  -   1  ])  >   0.000001  )   break  ;
//  end for
line_add(_mat,i  -   1  ,mm  -   1  );
//  end if

for  (  int   j  =  i;j  <  nSize;  ++  j)
{
line_add(_mat,j,i 
-   1  ,  -  _mat[j][i  -   1  ]  /  _mat[i  -   1  ][i  -   1  ]);

//  end for j
printstep(_mat);
//  end for i

}


void   work2(s_matrix  &   _mat)  //  第二部计算
{
for  (  int   i  =  nSize  -   2  ;i  >=   0  ;  --  i)
{
for  (  int   j  =  i;j  >=   0  ;  --  j)
{
line_add(_mat,j,i 
+   1  ,  -  _mat[j][i  +   1  ]  /  _mat[i  +   1  ][i  +   1  ]);
}
printstep(_mat);
}

}


void   makeunit(s_matrix  &   _mat)  //  单位化
{

mat.clear();

for  (  int   i  =   0  ;i  <  nSize;  ++  i)
{
line.clear();
for  (  int   j  =   0  ;j  <  nSize  *   2  ;  ++  j)
{
float   tmp  =  _mat[i][j]  /  _mat[i][i];
if  (fabs(tmp)  <   0.000001  ) tmp  =   0  ;
line.push_back(tmp);
}
mat.push_back(line);
//  cout<<endl;
}
_mat 
=  mat;
}

void   printstep(s_matrix  &   _mat)  //  显示求的过程
{
cout 
<<   "  第   "   <<++  step  <<   "  步  "   <<  endl;
for  (  int   i  =   0  ;i  <  nSize;  ++  i)
{

for   (  int   j  =   0  ;j  <   2   *  nSize;  ++  j)
{
if  (fabs(_mat[i][j])  <   0.000001 ) _mat[i][j]  =   0  ;
cout 
<<  _mat[i][j]  <<   "     "  ;
if  (j  ==  nSize  -   1  )cout   <<   "   |   "  ;
}
cout 
<<  endl;
}
cout 
<<  endl;

}

void   outprint(s_matrix  &   _mat)  //  输出函数
{
for  (  int   i  =   0  ;i  <  nSize;  ++  i)
{

for   (  int   j  =  nSize;j  <   2   *  nSize;  ++  j)
{
cout 
<<  _mat[i][j]  <<   "     "  ;
}
cout 
<<  endl;
}


}

int   main()
{
step 
=   0  ;
matrix.clear();
line.clear();
cout 
<<   "  *********矩阵 求逆*********  "   <<  endl;
cout 
<<   "  *********Bill  Hsu*********  "   <<  endl;
cout 
<<   "  http://hi.baidu.com/probill  "   <<  endl  <<  endl;

cout 
<<   "  请输入矩阵维数(输入0退出):  "  ; 
cin 
>>  nSize;
if  (nSize  <=   0  )   return     0  ;
for  (  int   i  =   0  ;i  <  nSize;  ++  i)
{
line.clear(); 
cout 
<<   "  输入第  "   <<  i  +   1   <<   "   行:   "   <<  endl;
for   (  int   j  =   0  ;j  <  nSize;  ++  j) 
{
float   tmp;
cin 
>>  tmp;
line.push_back(tmp);  
//  压入一个数到某行
}

for   (  int   j  =   0  ;j  <  nSize;  ++  j) 
{
if  (i  ==  j) line.push_back(  1.0f  );
else   line.push_back(  0.0f  );
}


matrix.push_back(line);  
//  压入一行到矩阵
}

cout  
<<  endl;
work1(matrix);
work2(matrix);
makeunit(matrix);
cout 
<<  endl  <<   "  ########################  "   <<  endl
<<   "  求逆结果:  "   <<  endl;
outprint(matrix);
cout 
<<   "  ########################  "   <<  endl;

main();


return     0  ;    
}

有图有真相:

输入矩阵数据

计算步骤

计算结果

执行文件下载:http://www.cppblog.com/Files/billhsu/%E7%9F%A9%E9%98%B5%E6%B1%82%E9%80%86.rar

.

posted on 2009-12-11 22:23 Bill Hsu 阅读(6692) 评论(8)  编辑 收藏 引用 所属分类: C/C++Algorithm

评论

# re: 矩阵求逆代码 2009-12-12 13:10 凡客诚品官方网

阿斯兰卡卡拉斯揆理度势
  回复  更多评论    

# re: 矩阵求逆代码 2009-12-18 09:10 rosamond

如果会matlab的话,直接用inv()命令
  回复  更多评论    

# re: 矩阵求逆代码[未登录] 2009-12-18 11:03 Bill Hsu

@rosamond
嗯,对的
  回复  更多评论    

# re: 矩阵求逆代码 2010-04-20 08:54 ccsdu2009

我是如何计算矩阵求逆的
首先使用matlab获取矩阵求逆参数表达式
然后换成c++表达式
虽然没什么技巧
但是很简便很很实用
  回复  更多评论    

# re: 矩阵求逆代码 2010-04-20 12:39 Bill Hsu

@ccsdu2009
同意
  回复  更多评论    

# re: 矩阵求逆代码 2010-06-08 22:28 zhaiduo

好东西,收藏~
  回复  更多评论    

# re: 矩阵求逆代码 2011-01-17 11:28 唐同学

谢谢你!
  回复  更多评论    

# re: 矩阵求逆代码 2011-11-25 11:05 C语言爱好者

正需要这个资料,很给力
  回复  更多评论    

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理