public class CalculationEquations {
static DecimalFormat df = new DecimalFormat("0.##");
* 增广矩阵机型初等行变化的算法
* @param value
* 需要算的增广矩阵
* @return 计算的结果
public static double[][] mathDeterminantCalculation(double[][] value)
throws Exception {
// 当矩阵的行数大于2时
for (int i = 0; i < value.length; i++) {
// 检查数组对角线位置的数值是否是0,如果是零则对该数组进行调换,查找到一行不为0的进行调换
if (value[i][i] == 0) {
value = changeDeterminantNoZero(value, i, i);
for (int j = 0; j < i; j++) {
// 让开始处理的行的首位为0处理为三角形式
// 如果要处理的列为0则和自己调换一下位置,这样就省去了计算
if (value[i][j] == 0) {
continue;
// 如果要是要处理的行是0则和上面的一行进行调换
if (value[j][j] == 0) {
double[] temp = value[i];
value[i] = value[i - 1];
value[i - 1] = temp;
continue;
double ratio = -(value[i][j] / value[j][j]);
value[i] = addValue(value[i], value[j], ratio);
return value;
* 将i行之前的每一行乘以一个系数,使得从i行的第i列之前的数字置换为0
* @param currentRow
* 当前要处理的行
* @param frontRow
* i行之前的遍历的行
* @param ratio
* 要乘以的系数
* @return 将i行i列之前数字置换为0后的新的行
public static double[] addValue(double[] currentRow, double[] frontRow,
double ratio) throws Exception {
for (int i = 0; i < currentRow.length; i++) {
currentRow[i] += frontRow[i] * ratio;
currentRow[i] = Double.parseDouble(df.format(currentRow[i]));
return currentRow;
* 指定列的位置是否为0,查找第一个不为0的位置的行进行位置调换,如果没有则返回原来的值
* @param determinant
* 需要处理的行列式
* @param line
* 要调换的行
* @param row
* 要判断的列
public static double[][] changeDeterminantNoZero(double[][] determinant,
int line, int row) throws Exception {
for (int j = line; j < determinant.length; j++) {
// 进行行调换
if (determinant[j][row] != 0) {
double[] temp = determinant[line];
determinant[line] = determinant[j];
determinant[j] = temp;
return determinant;
return determinant;
* 将系数矩阵和方程值的矩阵进行合并成增广矩阵
* @param coefficient
* 系数矩阵
* @param value
* 方程值
* @return 增广矩阵
public static double[][] transferMatrix(double[][] coefficient,
double[] value) {
double[][] temp = new double[coefficient.length][coefficient[0].length + 1];
if (coefficient.length != value.length) {
return temp;
// 将方程值添加到系数矩阵中
for (int i = 0; i < coefficient.length; i++) {
for (int j = 0; j < coefficient[0].length; j++) {
temp[i][j] = coefficient[i][j];
for (int i = 0; i < value.length; i++) {
temp[i][temp[i].length - 1] = value[i];
return temp;
* 检查有效的行数,看非零行的个数
* @param value
* 需要检查的数组
* @return 非零行的个数
public static int effectiveMatrix(double[][] value) {
for (int i = value.length - 1; i > -1; i--) {
for (int j = 0; j < value[i].length; j++) {
if (value[i][j] != 0) {
return i + 1;
return 0;
* 当方程组有解的时候计算方程组的解
* @param mathMatrix
* 方程组的增广矩阵
* @return 方程组的解
private static double[] calculationResult(double[][] mathMatrix) {
// 有解时方程组的个数等于方程组的未知数
double[] result = new double[mathMatrix.length];
for (int i = mathMatrix.length - 1; i > -1; i--) {
double temp = 0;
for (int j = mathMatrix[i].length; j > 0; j--) {
// 第一个为方程的解,需要将解赋值给临时变量
if (mathMatrix[i][j - 1] != 0) {
if (j == mathMatrix[i].length) {
temp = mathMatrix[i][j - 1];
} else if (j - 1 > -1 && result[j - 1] != 0) {
temp -= mathMatrix[i][j - 1] * result[j - 1];
} else {
result[i] = temp / mathMatrix[i][j - 1];
continue;
return result;
public static void main(String[] args) {
// 方程的未知数的个数
int n = 3;
// 系数矩阵
double[][] test = { { 2, 3, 1 }, { 1,1, 1 }, { 1, 2, -1 } };
// 方程的解
double[] value = { 11, 6, 2 };
方程组的解为x1=1.0 方程组的解为x2=2.0 方程组的解为x3=3.0
// 方程的未知数的个数
int n = 4;
// 系数矩阵
double[][] test = { { 3, 1, -1, 1 },{ 1, -1, 1,2 }, {2,1,2,-1},{ 1,0, 2, 1 } };
// 方程的解
double[] value ={ -3, 4, 7,6 };
方程组的解为x1=1.0 方程组的解为x2=-2.0 方程组的解为x3=3.0 方程组的解为x4=-1.0
// 方程的未知数的个数
int n = 4;
// 系数矩阵
double[][] test = { { 1, -3, 4, -5 },{ 1, -1, -1,2 }, {1,2,0,5},{ 2,-1, 3, -2 } };
// 方程的解
double[] value ={ 0, 0, 0,0 };
方程组有零解
// 方程的未知数的个数
int n = 5;
// 系数矩阵
double[][] test = { { 2,1, 1,1,1 },{ 1, 2, 1,1,1 }, {1,1,3,1,1},{ 1,1,1,4,1 },{1,1,1,1,5} };
// 方程的解
double[] value ={ 4,5,9,0,-5 };
try {
// 转换成增广矩阵并进行初等行变化
double[][] mathMatrix = mathDeterminantCalculation(transferMatrix(
test, value));
// 找出非零行的个数
int checkMatrixRow = effectiveMatrix(mathMatrix);
// 根据未知数的个数和方程组非零行的个数来判断当前方程组的解的情况
if (n > checkMatrixRow) {
System.out.println("未知数有" + n + "个,消元法后获取的阶梯方程组有"
+ checkMatrixRow + "个方程,少于未知数个数,所以该方程有无数组解");
} else if (n < checkMatrixRow) {
System.out.println("未知数有" + n + "个,消元法后获取的阶梯方程组有"
+ checkMatrixRow + "个方程,多于未知数个数,所以该方程有无解");
} else {
System.out.println("未知数有" + n + "个,消元法后获取的阶梯方程组有"
+ checkMatrixRow + "个方程,等于未知数个数,所以该方程有解");
double[] result = calculationResult(mathMatrix);
for (int i = 0; i < result.length; i++) {
System.out.println("方程组的解为x" + (i + 1) + "="
+ df.format(result[i]));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
这是一个利用线性
方程组
的行列式解法
实现
自动
求解
n
元
一次方程
组的VBS代码。对输入格式有要求,可
求解
任意有效的n
元
一次方程
组。
压缩包里有源码和说明,未作加密,可直接查看。
(分类真的不好选。。)
时间分割符2023/3/9 1h推荐使用方法一。方法就是一个代码集,其实就是函数,需要先定义,再调用。分为 带返回值的方法(int、boolean、float等等) 和 不带返回值的放法(void)。具体每一种又分是否带参数。1、定义2、调用推荐格式2,且要注意方法的返回值通常会使用变量接收,否则该返回值将毫无意义。!!!!!!
多
元
一次
不定方程的强力
算法
---同余筛数法uniqueleion6152018-10-24Python100例——第五章----不定方程的解wdt338516542013-07-19高斯消
元
求解
多
元
一次方程
组nikelong021632016-03-26二
元
一次
不定方程的快速解法uniqueleion37972018-07-30
JAVA
解N
元
一次方程
组(矩阵)yirentianran4259200...
在写
算法
时,有时会用到行求和或列求和,下面将以
java
中的math简单
实现
这一操作:
例如,我们遇到这样多个一维数组:
double a[]=new double[]{1,2,3,4};
double b[]=new double[]{1,2,3,3};
实现
每个数组对应列上的数字相加,然后求均值,即要得到的结果为:
1.0,2.0,3.0,3.5
下面为math3操作的方式:
pac...
java
解线性
方程组
Java
解线性
方程组
方法一:高斯消去法 import
java
.util.Scanner; public class Gauss { /*** @ 列主
元
高斯消去法*/ static double a[][]; static double b[]; static double x[]; static int n; static int n2; //记录换行的次数publi...
Option Base 1Function Determinant(ByRef factor) As SingleDim i As Long, j As Long, k As Long, row As Long, order As LongDim r As Long, c As Long, Pivot As S...
求n
元
一次
不定方程的解:解n
元
一次
不定方程时,可先顺次求出gcd(a1,a2)=d2,gcd(d2,a3)=d3,gcd(d3,a4)=d4,......若dn|c,则方程有解。作
方程组
1、一个N
元
一次方程
组,可以通过求系数矩阵获得方程的解。
(1)计算所有a的系数矩阵值d,如果d不为0,则方程有解,否则,方程无解。
(2)将第一列的系数a11,a21,……an1用b1、b2、……bn替代,获取该系数矩阵值d1,则X1=d1/d;以此类推,求得X2、X3、……Xn。
2、系数矩阵运用拉普拉斯展开定理逐
该楼层疑似违规已被系统折叠隐藏此楼查看此楼解N
元
一次方程
从文件读入整数N,然后读入N*(N+1)矩阵,得到解并输出到文件中。intmain(){intch;printf("+=======================+\n");printf("+GaussianMatrixCal+\n");printf("+=======================+\n");pri...