# 矩阵求逆（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<List<Double>> matrix;
private int lineNum;
private int columnNum;
private double determinantValue;

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>();
}

DeterminantCalculation d = new DeterminantCalculation(tempList);
d.chang2UpperTriangle();
double result = d.getValue();
if(result == 0){
throw new Exception("此矩阵不能求逆>>>>>>>>>>>>>>>>>");
}

determinantValue = result;

for(int i = 0; i < lineNum; i++){
List<Double> line = new ArrayList<Double>();
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>();
for(int k = 0; k < columnNum; k++){
if(j == k){
continue;
}
}
}

DeterminantCalculation determinant = new DeterminantCalculation(list);
determinant.chang2UpperTriangle();
double tempValue = determinant.getValue();
int sign = getSign(i, j);
System.out.println( "(" + i + j + ")的余子式：" + tempValue);
}
}
System.out.println("伴随矩阵的转置>>>>>>>>>>>>>>>>>>>>");
for(int i = 0; i < lineNum; i++){
for(int j = i; j < columnNum; j++){
}
}

System.out.println("伴随矩阵如下>>>>>>>>>>>>>>>>>>>>");
System.out.println("逆矩阵如下>>>>>>>>>>>>>>>>>>>>");
for(int i = 0; i < lineNum; i++){
for(int j = i; j < columnNum; j++){
}
}
}

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>();
for(int j = 0; j < columnNum; j++){
}
}

InverseOfMatrix2 m = new InverseOfMatrix2(matrix);
m.print(m.getMatrix());
try {
m.caculate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊