解线性方程组

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("请输入方程的个数与未知量的个数,用逗号分隔:");
		
		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("请输入第" + (i + 1) + "个方程的全部系数(未知量的系数跟常数项系数),用逗号分隔:");
			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();//化为上三角
			m.changeReducedMatrix();//化为约化矩阵
			m.clearZeroLine();//去除零行
			m.getValue();//判断是否有解
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

package gaodai.matrix;

import java.util.List;

/**
 * 矩阵
 * @author 邱万迟
 *
 */
public class Matrix {

	private int s;//方程的未知个数 + 1(即包括常数项)
	private int r;//方程的个数
	
	public Matrix(List<List<Double>> data){
		result = data;
		this.r = data.size();
		this.s = data.get(0).size();
	}
	
	private List<List<Double>> result;

	/**
	 * a行与b行互换(两行互换)
	 * @param a 行号
	 * @param b 行号
	 * @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("输入的行号不合法");
		}
		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("第" + a + "行与" + b + "行互换");
	}

	/**
	 * 第a行乘以number 加到第b行上
	 * @param number 乘以的数
	 * @param a行号
	 * @param b行号
	 * @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("输入的行号不合法");
		}
		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("第" + a + "行乘以" + number + "加到第" + b + "行:");
	}

	/**
	 * 打印
	 */
	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();
	}

	/**
	 * 校验是否是上三角,不是就的继续计算
	 * 
	 * @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) + ")元素值不为零");
					hasCaculate = true;
					break;
				}
			}
			if (hasCaculate) {
				break;
			}
		}
		return hasCaculate;
	}

	private int caculateTimes;

	/**
	 * 化为上三角
	 * @throws Exception
	 */
	public void chang2UpperTriangle() throws Exception {

		if (!isCaculate()) {
			return;
		}
		
		int min = r < s ? r : s;
		
		caculateTimes++;
		System.out.println("--------------第" + caculateTimes + "次计算--------------");
		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();
	}
	
	/**
	 * 变为约化矩阵
	 */
	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){//这个数不为零 ,此数为第 i行第j列
					for(int t = 0; t < r; t++){//行
						if(t == i || result.get(t).get(j) == 0){//本列的其他行
							continue;
						}
						
						double multiplyNum = -1 * result.get(t).get(j) / temp.get(j);
						this.lineMultiplyNumAdd2OtherLine(multiplyNum, (i + 1), (t + 1));
						print();
					}
					break;
				}
			}
		}
	}

	/**
	 * 去除零行
	 */
	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("此方程组无解!!!!!!!!!!!!!!!!!!!!!!!!");
			return;
		}
		
		if(r < (s - 1)){
			System.out.println("此方程组有无数解!!!!!!!!!!!!");
			return;
		}
		
		System.out.println("此方程组有唯一解!!!!!!!!!!!!!");
		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实现的解线性方程组

1.无解

2.有唯一解,解为。。。

3.有无穷解

 

你可能感兴趣的:(解线性方程组)