/**因要求解四元一次方程组, 甚至五元一次方程组, 为了避免重复的劳动遂有了写这个求解函数的想法
* @系统名称 :wm
* @创建人:cqf
* @创建时间 :2013-9-7 下午2:35:38
*/
package com.framework.util;
/**
* @系统名称 :wm
* @创建人:cqf
* @功能描述 :本程序利用克拉默法则解多元一次方程组
* @创建时间 :2013-9-7 下午2:35:38
*/
public class ZcramerRuleMatrix {
private double[][] savequot;// 保存变量系数
private double[] constquot;// 保存常量系数
private double[] saveResult;// 保存解的集合
public ZcramerRuleMatrix(double quot[][]) {
int count = quot.length;
savequot = new double[count][count];
constquot = new double[count];
saveResult = new double[count];
int i = 0, j = 0;
for (i = 0; i < count; i++) {
for (j = 0; j < count; j++) {
savequot[i][j] = quot[i][j];
}
constquot[i] = quot[i][count];
saveResult[i] = 0;
}
}
private double getMatrixResult(double input[][])// 递归的方法求得某个行列式的值
{
if (input.length == 2)// 递归出口,为二阶行列式时,直接返回
{
return input[0][0] * input[1][1] - input[0][1] * input[1][0];
} else {
double[] temp = new double[input.length];// 存放第一列的系数值
double[][] tempinput = new double[input.length - 1][input.length - 1];
double result = 0;
for (int i = 0; i < input.length; i++) {
temp[i] = input[i][0];
int m = 0, n = 0;
for (int k = 0; k < input.length; k++) {
if (k != i) {
for (m = 0; m < input.length - 1; m++) {
tempinput[n][m] = input[k][m + 1];// 删除当前变量系数所在的行和列,得到减少一阶的新的行列式
}
n++;
}
}
if (i % 2 == 0)// 递归调用,利用代数余子式与相应系数变量的乘积之和得到多阶行列式的值
{
result = result + temp[i] * getMatrixResult(tempinput);
} else {
result = result - temp[i] * getMatrixResult(tempinput);
}
}
return result;
}
}
private double[][] getReplaceMatrix(int i)// 用常数系数替换相应的变量系数,得到新的行列式
{
double tempresult[][] = new double[savequot.length][savequot.length];
for (int m = 0; m < savequot.length; m++) {
for (int n = 0; n < savequot.length; n++) {
if (i != m) {
tempresult[n][m] = savequot[n][m];
} else {
tempresult[n][i] = constquot[n];// 用常量系数替换当前变量系数
}
}
}
return tempresult;
}
public double[] getResult() {
double basic = 0;
basic = getMatrixResult(savequot);// 得到变量系数行列式的值
if (Math.abs(basic) < 0.00001) {
System.out.println("it dose not have the queue result!");
return saveResult;
}
double[][] temp = new double[saveResult.length][saveResult.length];
for (int i = 0; i < saveResult.length; i++) {
temp = getReplaceMatrix(i);
saveResult[i] = getMatrixResult(temp) / basic;
}
return saveResult;
}
/**
*
* @@param args
* @创建人:cqf
* @功能描述 :测试克拉默法则解多元一次方程组
* @创建时间 :2013-9-7 下午2:45:48
*/
public static void main(String[] args) {
/**
* 测试方程组 2a+b-5c+d=8
* a-3b-6d=9
* 2b-c+2d=-5
* a+4b-7c+6d=0
*/
double[][] test = { { 2, 1, -5, 1, 8 }, { 1, -3, 0, -6, 9 }, { 0, 2, -1, 2, -5 }, { 1, 4, -7, 6, 0 } };
ZcramerRuleMatrix gm = new ZcramerRuleMatrix(test);
double[] uu = new double[test.length];// 返回结果集
uu = gm.getResult();
for (int i = 0; i < uu.length; i++) {
System.out.println(uu[i] + ",");
}
}
}
多元一次方程组的三种算法
4星 · 超过85%的资源 需积分: 28 186 浏览量
2013-09-07
15:57:38
上传
评论 1
收藏 4KB RAR 举报
chqf518
- 粉丝: 13
- 资源: 224
最新资源
- C#,煎饼排序问题(Pancake Sorting Problem)算法与源代码
- C#,排列组合的堆生成法(Heap’s Algorithm for generating permutations)算法与源代码
- C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码
- 6693eeb8d683458a07938615fba9e68f.apk
- C#,数值计算,解微分方程的龙格-库塔二阶方法与源代码
- C#,数值计算,用割线法(Secant Method)求方程根的算法与源代码
- C#,子集和问题(Subset Sum Problem)的算法与源代码
- mongodb 数据库基本操作
- Linux操作系统基础教程
- Linux操作系统相关习题集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈