Matrix inversion (JAVA) using adjoint matrices

package gaodai.matrix;

import gaodai.determinant.DeterminantCalculation;

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

/**
 * Matrix inversion (using adjoint matrix)
 * @author Qiu Wanchi
 *
 */
public class InverseOfMatrix2 {
	
	private List<List<Double>> matrix;
	private int lineNum;
	private int columnNum;
	private double determinantValue;
	private List<List<Double>> adjointMatrix;

	public List<List<Double>> getMatrix() {
		return matrix;
	}

	public double getDeterminantValue() {
		return determinantValue;
	}

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

	public void setDeterminantValue(double determinantValue) {
		this.determinantValue = determinantValue;
	}
	
	public InverseOfMatrix2(List<List<Double>> data){
		matrix = data;
		lineNum = data.size();
		columnNum = data.get(0).size();
	}
	
	public void caculate() throws Exception{
		
		//1. Non-square cannot be reversed
		//2. Founder's determinant value is zero and cannot be inverted
		if( lineNum != columnNum){
			throw new Exception("This matrix cannot be inverse>>>>>>>>>>>>>>>>>>");
		}
		List<List<Double>> tempList = new ArrayList<List<Double>>();
		for(List<Double> l : matrix){
			List<Double> newList = new ArrayList<Double>();
			newList.addAll(l);
			tempList.add(newList);
		}
		
		DeterminantCalculation d = new DeterminantCalculation(tempList);
		d.chang2UpperTriangle();
		double result = d.getValue();
		if(result == 0){
			throw new Exception("This matrix cannot be inverse>>>>>>>>>>>>>>>>>>");
		}
		
		determinantValue = result;
		
		adjointMatrix = new ArrayList<List<Double>>();//Adjoint matrix
		
		for(int i = 0; i < lineNum; i++){
			List<Double> line = new ArrayList<Double>();
			adjointMatrix.add(line);
			for(int j = 0; j < columnNum; j++){
				
				List<List<Double>> list = new ArrayList<List<Double>>();//余子式
				for(int t = 0; t < lineNum; t++){
					if(i == t){
						continue;
					}
					List<Double> newList = new ArrayList<Double>();
					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("Transpose of adjoint matrix>>>>>>>>>>>>>>>>>>>>");
		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;
	}
	
	/**
	 * Print
	 */
	public void print(List<List<Double>> data) {
		int i = 0, j = 0;
		for (List<Double> 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("Please enter the number of rows and columns of the matrix, 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>> matrix = new ArrayList<List<Double>>();
		for(int i = 0; i < lineNum; i++){
			System.out.println("Please enter the number of lines " + (i + 1) + ", separated by commas: ");
			String lineData = scanner.next();
			String[] lineDataArr = lineData.split(",");
			List<Double> line = new ArrayList<Double>();
			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 ();
		}
	}
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327015606&siteId=291194637
Recommended