package gaodai.matrix; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the number of equations and the number of unknowns, separated by commas:"); String sn = scanner.next(); String[] snArr = sn.split(","); int lineNum = Integer.valueOf(snArr[0]); int columnNum = Integer.valueOf(snArr[1]); List<List<Double>> result = new ArrayList<List<Double>>(); for(int i = 0; i < lineNum; i++){ System.out.println("Please enter all the coefficients of the equation " + (i + 1) + " (the coefficient of the unknown quantity and the coefficient of the constant term), separated by commas: "); String lineData = scanner.next(); String[] lineDataArr = lineData.split(","); List<Double> line = new ArrayList<Double>(); result.add(line); for(int j = 0; j <= columnNum; j++){ line.add(Double.valueOf(lineDataArr[j])); } } Matrix m = new Matrix(result); m.print(); try { m.chang2UpperTriangle();//Turn into upper triangle m.changeReducedMatrix();//Reduce to reduced matrix m.clearZeroLine();//Remove zero lines m.getValue();//Determine whether there is a solution } catch (Exception e) { e.printStackTrace (); } } }
package gaodai.matrix; import java.util.List; /** * matrix * @author Qiu Wanchi * */ public class Matrix { private int s;//The unknown number of equations + 1 (that is, including the constant term) private int r;//Number of equations public Matrix(List<List<Double>> data){ result = data; this.r = data.size(); this.s = data.get(0).size(); } private List<List<Double>> result; /** * Line a and line b are interchanged (two lines are interchanged) * @param a line number * @param b line number * @throws Exception */ public void changeLine(int a, int b) throws Exception { if (a < 1 || a > result.size() || b < 1 || b > result.size()) { throw new Exception("The input line number is invalid"); } List<Double> aLine = result.get(a - 1); List<Double> bLine = result.get(b - 1); result.set (a - 1, bLine); result.set(b - 1, aLine); System.out.println("Line " + a + " is interchanged with " + b + " line "); } /** * Line a times number and add to line b * @param number The number to multiply by * @param a line number * @param b line number * @throws Exception */ public void lineMultiplyNumAdd2OtherLine(double number, int a, int b) throws Exception { if (a < 1 || a > result.size() || b < 1 || b > result.size()) { throw new Exception("The input line number is invalid"); } List<Double> aLine = result.get(a - 1); List<Double> bLine = result.get(b - 1); for (int i = 0; i < bLine.size(); i++) { double temp = bLine.get(i) + aLine.get(i) * number; bLine.set (i, temp); } System.out.println("Line " + a + " multiplied by " + number + " and added to line " + b + " Line: "); } /** * Print */ public void print() { int i = 0, j = 0; for (List<Double> line : result) { for (double element : line) { System.out.print(element); System.out.print("(" + i + "," + j + ") "); System.out.print(" "); j++; } System.out.println(); i++; j = 0; } System.out.println(); } /** * Check whether it is an upper triangle, if not, continue to calculate * * @return */ public boolean isCaculate() { boolean hasCaculate = false; for (int i = 0; i < result.size(); i++) { for (int j = 0; j < i; j++) { if (result.get(i).get(j) != 0.0) { System.out.println("(" + (i + 1) + "," + (j + 1) + ") element value is not zero"); hasCaculate = true; break; } } if (hasCaculate) { break; } } return hasCaculate; } private int caculateTimes; /** * into an upper triangle * @throws Exception */ public void chang2UpperTriangle() throws Exception { if (!isCaculate()) { return; } int min = r < s ? r : s; caculateTimes++; System.out.println("-------------th" + caculateTimes + "time calculation-------------"); for (int i = 0; i < min; i++) { for (int j = i + 1; j < min; j++) { double multiplyNum = -1 * result.get(j).get(i) / result.get(i).get(i); if (multiplyNum == 0) { continue; } this.lineMultiplyNumAdd2OtherLine(multiplyNum, (i + 1), (j + 1)); print(); } } print(); chang2UpperTriangle(); } /** * becomes a reduced matrix */ public void changeReducedMatrix() throws Exception{ for(int i = 0; i < r; i++){//行 if(i == 0){ continue; } List<Double> temp = result.get(i); for(Double d : temp){ if(d == 0){ continue; } double multiplyNum = 1.0 / d; for(int a = 0; a < temp.size(); a++){ temp.set(a, temp.get(a) * multiplyNum); } break; } print(); for(int j = 0; j <= s; j++){//列 if(temp.get(j) != 0){//This number is not zero, this number is the i-th row and the j-th column for(int t = 0; t < r; t++){//行 if(t == i || result.get(t).get(j) == 0){//Other lines in this column continue; } double multiplyNum = -1 * result.get(t).get(j) / temp.get(j); this.lineMultiplyNumAdd2OtherLine(multiplyNum, (i + 1), (t + 1)); print(); } break; } } } } /** * remove zero lines */ public void clearZeroLine(){ for(int i = 0; i < result.size(); i++){ boolean flag = true; List<Double> temp = result.get(i); for(Double d : temp){ if(d != 0){ flag = false; } } if(flag){ result.remove(i); } } this.r = result.size(); } public void getValue(){ if(result.get(r - 1).get(s - 2) == 0 && result.get(r - 1).get(s - 1) != 0){ System.out.println("This system of equations has no solution!!!!!!!!!!!!!!!!!!!!!!!!"); return; } if(r < (s - 1)){ System.out.println("There are countless solutions to this system of equations!!!!!!!!!!!!"); return; } System.out.println("This system of equations has a unique solution!!!!!!!!!!!!!"); for(int i = 0; i < result.size(); i++){ List<Double> temp = result.get(i); System.out.println("x" + (i + 1) + "=" + temp.get(temp.size() - 1)); } } }
JAVA implementation of solving linear equations
1. No solution
2. There is a unique solution, the solution is. . .
3. There are infinite solutions