c++高斯消元法求解线性方程组
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
#define MaxNum 10
int unuse_result[MaxNum];
int GaussFun(int equ, int var, int result[],int array[MaxNum][MaxNum])
{
int i, j, k, col, num1, num2;
int max_r, ta, tb, gcdtemp, lcmtemp;
int temp, unuse_x_num, unuse_index;
col = ;
for (k = ; k < equ && col < var; k++, col++)//循环处理增广矩阵的各行
{
max_r = k;
for (i = k + ; i < equ; i++)
{
if (abs(array[i][col]) > abs(array[max_r][col]))
{
max_r = i;//保存绝对值最大的行
}
}
if (max_r != k)
{
for (j = k; j < var + ; j++)
{
temp = array[k][j];
array[k][j] = array[max_r][j];
array[max_r][j] = temp;
}
}
if (array[k][col] == )
{
k--;
continue;
}
for (i = k + ; i < equ; i++)
{
if (array[i][col] != )
{
num1 = abs(array[i][col]);
num2 = abs(array[k][col]);
while (num2 != )
{
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
gcdtemp = num1;//最大公约数
lcmtemp = (abs(array[i][col]) * abs(array[k][col])) / gcdtemp;
ta = lcmtemp / abs(array[i][col]);
tb = lcmtemp / abs(array[k][col]);
if (array[i][col] * array[k][col] < )
{
tb = -tb;
}
for (j = col; j < var + ; j++)
{
array[i][j] = array[i][j] * ta - array[k][j] * tb;
}
}
}
}
for (i = k; i < equ; i++)
{
if (array[i][col] != )
{
return -;
}
}
if (k < var)
{
for (i = k - ; i >= ; i--)
{
unuse_x_num = ;
for (j = ; j < var; j++)
{
if (array[i][j] != && unuse_result[j])
{
unuse_x_num++;
unuse_index = j;
}
}
if (unuse_x_num > )
{
continue;
}
temp = array[i][var];
for (j = ; j < var; j++)
{
if (array[i][j] != && j != unuse_index)
{
temp -= array[i][j] * result[j];
}
}
result[unuse_index] = temp / array[i][unuse_index];
unuse_result[unuse_index] = ;
}
return var - k;
}
for (i = var - ; i >= ; i--)
{
temp = array[i][var];
for (j = i + ; j < var; j++)
{
if (array[i][j] != )
{
temp -= array[i][j] * result[j];
}
}
if (temp % array[i][i] != )
{
return -;
}
result[i] = temp / array[i][i];
}
return ;
}
int main()
{
int i, type;
int equnum, varnum;
int array[MaxNum][MaxNum] = {
{,,-,},
{,,,-},
{,-,,-} };
int result[MaxNum];
equnum = ;
varnum = ;
type = GaussFun(equnum, varnum, result,array);//调用高斯函数
if (type == -)
{
cout << "该方程无解。" << endl;
}
else if (type == -)
{
cout << "该方程又浮点数解没有整数解。" << endl;
}
else if (type > )
{
cout << "该方程有无穷多解!自由变量的数量为" << type << endl;
for (i = ; i < varnum; i++)
{
if (unuse_result[i])
{
cout << i + << "是不确定的" << endl;
}
else
{
cout << i + << result[i] << endl;
}
}
}
else
{
cout << "该方程的解为:" << endl;
for (i = ; i < varnum; i++)
{
cout << i + << result[i] << endl;
}
}
return ;
}
c++高斯消元法求解线性方程组的更多相关文章
- matlab中求解线性方程组的rref函数
摘自:http://www.maybe520.net/blog/987/ matlab中怎么求解线性方程组呢? matlab中求解线性方程组可应用克拉默法则(Cramer's Rule)即通过det( ...
- matlab 求解线性方程组之LU分解
线性代数中的一个核心思想就是矩阵分解,既将一个复杂的矩阵分解为更简单的矩阵的乘积.常见的有如下分解: LU分解:A=LU,A是m×n矩阵,L是m×m下三角矩阵,U是m×n阶梯形矩阵 QR分解: 秩分解 ...
- 【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- python 求解线性方程组
Python线性方程组求解 求解线性方程组比较简单,只需要用到一个函数(scipy.linalg.solve)就可以了.比如我们要求以下方程的解,这是一个非齐次线性方程组: 3x_1 + x_2 - ...
- Numpy库进阶教程(一)求解线性方程组
前言 Numpy是一个很强大的python科学计算库.为了机器学习的须要.想深入研究一下Numpy库的使用方法.用这个系列的博客.记录下我的学习过程. 系列: Numpy库进阶教程(二) 正在持续更新 ...
- [Matlab]求解线性方程组
转自:http://silencethinking.blog.163.com/blog/static/911490562008928105813169/ AX=B或XA=B在MATLAB中,求解线性方 ...
- Numpy计算逆矩阵求解线性方程组
对于这样的线性方程组: x + y + z = 6 2y + 5z = -4 2x + 5y - z = 27 可以表示成矩阵的形式: 用公式可以表示为:Ax=b,其中A是矩阵,x和b都是列向量 逆矩 ...
- Numpy求解线性方程组
Numpy求解线性方程组 对于Ax=b,已知A和b,怎么算出x? 1. 引入包 2. 求解 验证
- Lapack求解线性方程组
可参见这两个页面: 1. http://www.culatools.com/dense/lapack/ 2. http://www.netlib.org/lapack/lug/node1.html 根 ...
随机推荐
- Working copy not locked; this is probably a bug, please report
问题描述 svn: Unable to lock 'G:\chengXu\2017_Year\JFW\Easy7\src' 解决方案
- java基础(四)之this的使用
作用: 1.使用this调用成员变量和成员函数2.使用this调用构造函数 Person.java: class Person{ String name; //成员变量 void talk(Strin ...
- MS SQL Server 2014,sa登录失败问题
1.用Windows身份验证登录 2.服务器属性-安全性 3.进入服务,重启所有SQL服务
- make && make install
./configure --prefix= /usr/local/python3.6.6 make && make install prefix=/usr/local/pyth ...
- numpy广播机制,取特定行、特定列的元素 的高级索引取法
numpy广播机制,取特定行.特定列的元素 的高级索引取法 enter description here enter description here
- SSH通道 Xshell
SSH是每一台Linux电脑的标准配置. SSH是一种网络协议,用于计算机之间的加密登录(安全的). 1. 登录远程主机 $ ssh user@host 2. SSH的默认端口是22, ...
- 短网址url接口api,url短链接(t.cn、url.cn)生成
简要说明 短网址api接口有很多格式,不同的接口生成的短网址格式也不同,比如常见的t.cn.url.cn.w.url.cn等格式.总而言之短网址接口就是用来将一个冗长的链接缩短成10个字符以内的短链接 ...
- 微服务监控平台获取网关(zuul)配置列表
步骤: (1)读取zuul的配置文件,获取路由配置项信息: private static Properties props; static { String fileName = "appl ...
- Linux中通配符
通配符是由shell处理的, 它只会出现在 命令的“参数”里.当shell在“参数”中遇到了通配符时,shell会将其当作路径或文件名去在磁盘上搜寻可能的匹配:若符合要求的匹配存在,则进行代换(路径扩 ...
- IE的css hack
#element { color:orange; } #element { *color: white; } /* IE6+7, doesn’t work in IE8/9 as IE7 */ #el ...