矩阵求逆(JAVA)利用伴随矩阵
Java  /  管理员 发布于 4年前   746
java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * 矩阵求逆(利用伴随矩阵) * @author 邱万迟 * */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.非方正不能求逆//2.方正的行列式值为零不能求逆if( lineNum != columnNum){throw new Exception("此矩阵不能求逆>>>>>>>>>>>>>>>>>");}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("此矩阵不能求逆>>>>>>>>>>>>>>>>>");}determinantValue = result;adjointMatrix = new ArrayList<List<Double>>();//伴随矩阵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("伴随矩阵的转置>>>>>>>>>>>>>>>>>>>>");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<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("请输入矩阵的行数与列数,用逗号分隔:");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("请输入第" + (i + 1) + "行的数,用逗号分隔:");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();}}}
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号