C++ 矩阵求逆

gao30101 2010-04-20 12:44:09
各位高手,本人有个亟待解决的问题,就是矩阵求逆中如何实现求矩阵的行列式与伴随矩阵中的元素,其中矩阵为自行输入,恳请指点,感激不尽!
...全文
456 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinkli 2010-05-02
  • 打赏
  • 举报
回复
楼主,这个在徐士良老师的《C常用算法程序集》第三版上面有,求行列式和求逆的程序都有,一个rinv.c,一个det.c,直接copy就可以啦,都不用自己敲的。
gao30101 2010-04-22
  • 打赏
  • 举报
回复
谁能帮我整出来,我把分都给他!
gao30101 2010-04-22
  • 打赏
  • 举报
回复
还是很感谢!
Qlaiaqu 2010-04-21
  • 打赏
  • 举报
回复
我这里没有伴随矩阵的计算啊,就这两个
行列式计算

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cmath>
using namespace std;

int factorial(int step)//阶乘
{
int result = 1;
for(int i = 2; i < step+1; i++)
result *= i;
return result;
}


bool reversekeyindex(int *permutate,int step)//逆序数
{
int num = 0;
for(int i = 0; i < step; i++)
{
for(int j = 0; j < i; j++)
if(permutate[i] < permutate[j])
num++;
}

if(num%2)
return false;
else
return true;
}

int getdata(vector<double> &vec,_TCHAR* filename)//获取矩阵
{
ifstream ifs(filename);
int count;
double temp;
while(!ifs.eof())
{
ifs>>temp;
vec.push_back(temp);
}
ifs.close();
return count = sqrt(static_cast<double>(vec.size()));
}

int _tmain(int argc, _TCHAR* argv[])
{
vector<double> vec;
int step = getdata(vec, argv[1]);
int *permutate = new int[step];
for(int i = 0; i < step; i++)
permutate[i] = i;

int result = 0;
for(int i = 0; i < factorial(step); i++)
{
int temp = 1;
for(int j =0 ; j < step; j++)
{
temp *= vec[j*step+permutate[j]];
}
if(!reversekeyindex(permutate, step))
temp = -temp;
result += temp;
prev_permutation(permutate, permutate+step);
}
cout<<result<<endl;
delete[] permutate;
return 0;
}



转置矩阵

#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>

using namespace std;

void reversion(vector<int> &vec, int order)
{
int temp;
for (int i = 0; i < order; i++ )
{
for (int j = 0; j < order; j++ )
{
if(i > j)
{
temp = vec[i*order+j];
vec[i*order+j] = vec[i+order*j];
vec[i+order*j] = temp;
}
}
}

}

int main()
{
ifstream ifs("d:\\test.txt");
if(!ifs) return 1;

vector<int> vec;
int temp;
while(!ifs.eof())
{
ifs>>temp;
vec.push_back(temp);
}
int order = (int)sqrt((double)vec.size());
cout<<"The region data:"<<endl;
for(unsigned int i = 0; i < vec.size(); i++)
{
cout<<vec[i]<<ends;
if((i+1) % order == 0)
cout<<endl;
}

cout<<endl<<"The reversion data:"<<endl;
reversion(vec, order);
for(unsigned int i = 0; i < vec.size(); i++)
{
cout<<vec[i]<<ends;
if((i+1) % order == 0)
cout<<endl;
}
return 0;
}

gao30101 2010-04-20
  • 打赏
  • 举报
回复
我需要行列式计算和伴随矩阵的计算,就这两个就可以了,先谢了!
Qlaiaqu 2010-04-20
  • 打赏
  • 举报
回复
以前玩过,现在不记得该怎么玩了
我实现里面有行列式计算,转置矩阵和克拉默法则的实现,lz要哪些?
gao30101 2010-04-20
  • 打赏
  • 举报
回复
关键是好久没有用c++编程了 手生了
CCCCCCCCCCCCCCC 2010-04-20
  • 打赏
  • 举报
回复
线性代数和数值计算方法,懂一个即可解决此问题
  • 打赏
  • 举报
回复
SF 数学没学好的路过...嘻嘻!

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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