N阶行列式计算(JAVA)

阅读更多
package gaodai;

import java.util.List;

/**
 * N阶行列式计算
 * @author 邱万迟
 *
 */
public class DeterminantCalculation {
	
	public DeterminantCalculation(List> determinant){
		this.determinant = determinant;
		this.sign = 1;
	}
	
	private List> determinant;
	private int sign;
	private int caculateTimes;
	
	/**
	 * 转置
	 */
	public void transpose(){
		int lineNum = determinant.size();
		int columnNum = determinant.get(0).size();
		for(int i = 0; i < lineNum; i++){
			for(int j = i; j < columnNum; j++){
				double t = determinant.get(i).get(j);
				determinant.get(i).set(j,determinant.get(j).get(i));
				determinant.get(j).set(i,t);
			}
		}
	}
	
	/**
	 * 获取行列式的值
	 * @return
	 */
	public double getValue(){
		double temp = 1.0;
		for(int i = 0; i < determinant.size(); i++){
			temp *= determinant.get(i).get(i);
		}
		return Math.round((temp * getSign()) * 100)/100.0;
	}

	/**
	 * a行与b行互换(两行互换)
	 * @param determinant 行列式
	 * @param a 行号
	 * @param b 行号
	 * @throws Exception 
	 */
	public void changeLine(int a,int b) throws Exception{
		if(a < 1 || a > determinant.size() || b < 1 || b > determinant.size()){
			throw new Exception("输入的行号不合法");
		}
		List aLine = determinant.get(a - 1);
		List bLine = determinant.get(b - 1);
		
		determinant.set(a - 1, bLine);
		determinant.set(b - 1, aLine);
		changeSign();
		System.out.println("第" + a + "行与" + b + "行互换");
	}
	
	/**
	 * 两列互换
	 * @param a
	 * @param b
	 * @throws Exception 
	 */
	public void changeColumn(int a,int b) throws Exception{
		if(a < 1 || a > determinant.get(0).size() || b < 1 || b > determinant.get(0).size()){
			throw new Exception("输入的列号不合法");
		}
		
		for(int i = 0; i < determinant.size(); i++){
			double temp = determinant.get(i).get(a - 1);
			determinant.get(i).set(a - 1,determinant.get(i).get(b - 1));
			determinant.get(i).set(b - 1,formateDouble(temp));
		}
		changeSign();
	}
	
	/**
	 * a行加到b行上
	 * @param a
	 * @param b
	 * @throws Exception 
	 */
	public void lineAdd2OtherLine(int a, int b) throws Exception{
		lineMultiplyNumAdd2OtherLine(1, 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 > determinant.size() || b < 1 || b > determinant.size()){
			throw new Exception("输入的行号不合法");
		}
		List aLine = determinant.get(a - 1);
		List bLine = determinant.get(b - 1);
		
		for(int i = 0; i < bLine.size(); i++){
			double temp = bLine.get(i) + aLine.get(i) * number;
			bLine.set(i, formateDouble(temp));
		}
		System.out.println("第" + a + "行乘以" + number + "加到第" + b + "行:");
		print();
	}
	
	public double formateDouble(double data){
		return Math.round(data * 10000000000L)/10000000000.0;
	}
	
	/**
	 * a列加到b列
	 * @param a
	 * @param b
	 */
	public void columnAdd2OtherColumn(int a, int b){
		columnMultiplyNumAdd2OtherColumn(1, a, b);
	}
	
	/**
	 * 第a列乘以number 加到第b列上
	 * @param number 乘以的数
	 * @param a行号
	 * @param b行号
	 */
	public void columnMultiplyNumAdd2OtherColumn(double number,int a, int b){
		for(int i = 0; i < determinant.size(); i++){
			double temp = determinant.get(i).get(b - 1) + determinant.get(i).get(a - 1) * number;
			determinant.get(i).set(b - 1, temp);
		}
	}
	
	/**
	 * 校验是否是上三角,不是就的继续计算
	 * @return
	 */
	public boolean isCaculate(){
		boolean hasCaculate = false;
		for(int i = 0; i < determinant.size(); i++){
			for(int j = 0; j < i; j++){
				if(determinant.get(i).get(j) != 0.0){
					System.out.println("(" + (i + 1) + "," + (j + 1) + ")元素值不为零");
					hasCaculate = true;
					break;
				}
			}
			if(hasCaculate){
				break;
			}
		}
		//System.out.println("isUpperTriangle?>>>>>>>>>>>" + hasCaculate);
		return hasCaculate;
	}
	
	/**
	 * @throws Exception 
	 * 变为上三角
	 * @throws  
	 */
	public void chang2UpperTriangle() throws Exception{
		
		if(!isCaculate()){
			return;
		}
		caculateTimes++;
		System.out.println("----------------------------------第" + caculateTimes + "次计算----------------------------------");
		for(int i = 0; i < determinant.size(); i++){
			for(int j = i + 1; j < determinant.size(); j++){
				if(determinant.get(i).get(i) == 0){//每一行在对角线上元素是零就互换行
					
					changeLine(i + 1, j + 1);
					
					print();
					continue;
				}
				double multiplyNum = -1 * determinant.get(j).get(i) / determinant.get(i).get(i);
				if(multiplyNum == 0){
					continue;
				}
				this.lineMultiplyNumAdd2OtherLine(multiplyNum, (i + 1), (j + 1));
				
			}
		}
		print();
		chang2UpperTriangle();
	
	}
	
	/**
	 * 获取a行b列元素所在的下一个非零元素所在的行
	 * @param a
	 * @param b
	 */
	public int getNotZeroLineByColumn(int a,int b){
		for(int i = a + 1; i < determinant.size(); i++){
			if(determinant.get(i).get(b) != 0){
				return i;
			}
		}
		return 0;
	}
	
	/**
	 * 改变符号
	 */
	private void changeSign(){
		this.sign = this.sign * -1;
	}
	
	public void print(){
		int i = 0,j = 0;
		for(List line : determinant){
			for(double element : line){
				System.out.print(element);
				//System.out.print(Math.round(element*100)/100.0);
				System.out.print("(" + i + "," + j + ")  ");
				System.out.print("  ");
				j++;
			}
			System.out.println();
			i++;
			j = 0;
		}
		System.out.println();
	}
	
	public List> getDeterminant() {
		return determinant;
	}


	public void setDeterminant(List> determinant) {
		this.determinant = determinant;
	}

	public int getSign() {
		return sign;
	}
}

 

package gaodai;

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

import org.apache.commons.lang.StringUtils;

public class Test {

	public static void main(String[] args) {
		int lineNum = 0;
		Scanner scanner = new Scanner(System.in);
		
		boolean flag = true;
		System.out.println("请输入行列式的阶数,系统会随机生成行列式里的数:");
		while(flag){ 
			String num = scanner.next();
			if(StringUtils.isNotBlank(num)){
				try {
					lineNum = Integer.valueOf(num);
				} catch (Exception e) {
					lineNum = 0;
				}
				if(lineNum < 1){
					System.out.println("请重新输入(需是整数且大于零)");
				}else{
					flag = false;
				}
				
			}else{
				System.out.println("请重新输入(需是整数且大于零)");
			}
		}
		int columnNum = lineNum;
		
		List> result = new ArrayList>();
		for(int i = 0; i < lineNum; i++){
			List temp = new ArrayList();
			
			for(int j = 0; j < columnNum; j++){
				temp.add((double) (int)(Math.random()*10 + 1));
			}
			
			result.add(temp);
		}
		DeterminantCalculation determinant = new DeterminantCalculation(result);
		System.out.println("初始化行列式:");
		determinant.print();
		
		determinant.transpose();
		
		determinant.print();
		try {
			determinant.chang2UpperTriangle();
		} catch (Exception e) {
			e.printStackTrace();
		}
		determinant.print();
		
		System.out.println("行列式值为:" + determinant.getValue());
		
	}
	
}

 

JAVA实现的N阶行列式计算,一起分享学习,有不足之处还望指正批评。

 

 

你可能感兴趣的:(N阶行列式计算)