矩阵求逆(JAVA)利用伴随矩阵

阅读更多
package gaodai.matrix;

import gaodai.determinant.DeterminantCalculation;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 矩阵求逆(利用伴随矩阵)
 * @author 邱万迟
 *
 */
public class InverseOfMatrix2 {
	
	private List> matrix;
	private int lineNum;
	private int columnNum;
	private double determinantValue;
	private List> adjointMatrix;

	public List> getMatrix() {
		return matrix;
	}

	public double getDeterminantValue() {
		return determinantValue;
	}

	public void setMatrix(List> matrix) {
		this.matrix = matrix;
	}

	public void setDeterminantValue(double determinantValue) {
		this.determinantValue = determinantValue;
	}
	
	public InverseOfMatrix2(List> data){
		matrix = data;
		lineNum = data.size();
		columnNum = data.get(0).size();
	}
	
	public void caculate() throws Exception{
		
		//1.非方正不能求逆
		//2.方正的行列式值为零不能求逆
		if( lineNum != columnNum){
			throw new Exception("此矩阵不能求逆>>>>>>>>>>>>>>>>>");
		}
		List> tempList = new ArrayList>();
		for(List l : matrix){
			List newList = new ArrayList();
			newList.addAll(l);
			tempList.add(newList);
		}
		
		DeterminantCalculation d = new DeterminantCalculation(tempList);
		d.chang2UpperTriangle();
		double result = d.getValue();
		if(result == 0){
			throw new Exception("此矩阵不能求逆>>>>>>>>>>>>>>>>>");
		}
		
		determinantValue = result;
		
		adjointMatrix = new ArrayList>();//伴随矩阵
		
		for(int i = 0; i < lineNum; i++){
			List line = new ArrayList();
			adjointMatrix.add(line);
			for(int j = 0; j < columnNum; j++){
				
				List> list = new ArrayList>();//余子式
				for(int t = 0; t < lineNum; t++){
					if(i == t){
						continue;
					}
					List newList = new ArrayList();
					list.add(newList);
					for(int k = 0; k < columnNum; k++){
						if(j == k){
							continue;
						}
						newList.add(matrix.get(t).get(k));
					}
				}
				
				DeterminantCalculation determinant = new DeterminantCalculation(list);
				determinant.chang2UpperTriangle();
				double tempValue = determinant.getValue();
				int sign = getSign(i, j);
				System.out.println( "(" + i + j + ")的余子式:" + tempValue);
				line.add(sign * tempValue);
			}
		}
		System.out.println("伴随矩阵的转置>>>>>>>>>>>>>>>>>>>>");
		print(adjointMatrix);
		int lineNum = adjointMatrix.size();
		int columnNum = adjointMatrix.get(0).size();
		for(int i = 0; i < lineNum; i++){
			for(int j = i; j < columnNum; j++){
				double t = adjointMatrix.get(i).get(j);
				adjointMatrix.get(i).set(j,adjointMatrix.get(j).get(i));
				adjointMatrix.get(j).set(i,t);
			}
		}
		
		System.out.println("伴随矩阵如下>>>>>>>>>>>>>>>>>>>>");
		print(adjointMatrix);
		System.out.println("逆矩阵如下>>>>>>>>>>>>>>>>>>>>");
		for(int i = 0; i < lineNum; i++){
			for(int j = i; j < columnNum; j++){
				adjointMatrix.get(i).set(j,adjointMatrix.get(i).get(j) / determinantValue);
			}
		}
		print(adjointMatrix);
	}
	
	private int getSign(int i,int j){
		if((i + j) % 2 == 0){
			return 1;
		}
		return -1;
	}
	
	/**
	 * 打印
	 */
	public void print(List> data) {
		int i = 0, j = 0;
		for (List line : data) {
			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();
	}
	
	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> matrix = new ArrayList>();
		for(int i = 0; i < lineNum; i++){
			System.out.println("请输入第" + (i + 1) + "行的数,用逗号分隔:");
			String lineData = scanner.next();
			String[] lineDataArr = lineData.split(","); 
			List line = new ArrayList();
			matrix.add(line);
			for(int j = 0; j < columnNum; j++){
				line.add(Double.valueOf(lineDataArr[j]));
			}
		}
		
		InverseOfMatrix2 m = new InverseOfMatrix2(matrix);
		m.print(m.getMatrix());
		try {
			m.caculate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

你可能感兴趣的:(利用伴随矩阵求逆矩阵)