package gaodai; import java.util.List; /** * N阶行列式计算 * @author 邱万迟 * */ public class DeterminantCalculation { public DeterminantCalculation(List<List<Double>> determinant){ this.determinant = determinant; this.sign = 1; } private List<List<Double>> 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<Double> aLine = determinant.get(a - 1); List<Double> 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<Double> aLine = determinant.get(a - 1); List<Double> 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<Double> 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<List<Double>> getDeterminant() { return determinant; } public void setDeterminant(List<List<Double>> 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<List<Double>> result = new ArrayList<List<Double>>(); for(int i = 0; i < lineNum; i++){ List<Double> temp = new ArrayList<Double>(); 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阶行列式计算,一起分享学习,有不足之处还望指正批评。
相关推荐
本程序采用java编写,具有计算n阶行列式的功能。采用递归算法实现,将一个高阶行列式不断展开,直到变成2阶,然后计算。用输入框获取输入,单击按钮执行程序。通过本程序,可以学习递归算法、BorderLayout和...
使用java语言,递归实现的N阶行列式求值
计算N阶行列式的若干方法 计算N阶行列式的若干方法 计算N阶行列式的若干方法 计算N阶行列式的若干方法
用高斯消元法计算n阶行列式,每一步都配有详细的注释说明。
用C语言实现的n阶行列式计算,希望大家能多提意见让程序更加完善~
这是一个用JAVA代码编的N阶行列式求值程序,先输入阶数,再输入各元素,即可求出其值。
自己写的基于C++的计算行列式的源代码,适合初学者
几类特殊N阶行列式的计算.doc
N阶行列式算法.PPT 线性代数里的 同济四班 高教出版社
用VB编写的行列式计算器,用递归算法,原则上可以是N阶,可运行超过12阶后,计算速度很慢
简单的c语言程序,计算行列式,五阶以内,但可以通过修改增加阶数,新手练手作品
可求N阶矩阵行列式,代数余子式等。注意需安装Qt。测试代码自行撰写
本软件可以方便的计算四阶以内的行列式(包括上三角式),并可以生成计算过程,减轻行列式计算烦琐的问题。
n阶行列式的计算方法.pdf
用于线性代数里的行列式计算,切为分数型。。》》》》》》》。。。。。。。
计算5阶行列式,输入变量后计算5阶矩阵的行列式,6维空间中的5个矢量的叉乘,相当于求解这个行列式我就是这样计算的。
使用java实现矩阵行列式的计算,下载者可根据自身需要进行修改
行列式计算,可以解决行列式计算的相关问题。并以此为基础,编写高阶行列式计算器。
这是一个c语言描述的数据结构计算b阶行列式的算法
用的是三维数组的结构做的,特殊值也测试过,很成功。