用java描述矩陣求逆的算法


今天很開心把困擾幾天的問題解決了,在學習線性代數這門課程的時候。想通過程序實現里面的計算方法,比如矩陣求逆,用java代碼該如何描述呢?

首先,咱們先用我們所交流語言描述一下算法思路:

1.求出一個矩陣A對應的行列式在第i,j(i表示行,j表示列)位置的余子式(余子式前面乘以-1^(i+j)即得代數余子式);

2.根據代數余子式求得矩陣A行列式的值。(行列式展開法);

3.根據代數余子式和行列式的值求出伴隨矩陣;

4.由伴隨矩陣和矩陣行列式值求逆矩陣。(A^-1 = A* / |A|)。

 

 

了解上述算法思路后,廢話少說,上代碼。

1.求出一個矩陣A對應的行列式在第i,j(i表示行,j表示列)位置的余子式(余子式前面乘以-1^(i+j)即得代數余子式);

 1 /**
 2      * 求矩陣在i,j處余子式
 3      * @param mat
 4      * @param i
 5      * @param j
 6      * @return
 7      */
 8     public static Matrix getComplementMinor(Matrix mat, int i, int j) {
 9         //創建一個新的矩陣用於接收表示該余子式,需刪除本行本列的數值
10         Matrix m = new Matrix(mat.getRow()-1,mat.getCol()-1); 
11         //用於遍歷新矩陣m的變量
12         int row =0 ,col=0;
13         /*
14          * 遍歷原矩陣的數據,j2表示行,k表示列
15          */
16         for (int j2 = 0; j2 < mat.getRow(); j2++) {
17             //在第i行除的數據省略
18             if(j2 == i) continue; 
19             for (int k = 0; k < mat.getCol(); k++) {
20                 //在第j列的數據省略
21                 if(k == j) continue;
22                 //賦值
23                 m.setValue(row, col,mat.getValue(j2, k));
24                 //遍歷新矩陣的變量
25                 col++;
26                 if(col >= m.getCol() ) {
27                     col = 0;
28                     row++;
29                 }
30             }
31         }
32         return m;
33     }

 

2.根據代數余子式求得矩陣A行列式的值。(行列式展開法);

 1 /**
 2      * 求矩陣的行列式的值
 3      * @param mat
 4      * @return
 5      */
 6     public static double getMatrixValue(Matrix mat) {
 7         if(mat.getRow() != mat.getCol()) {
 8             System.out.println("該矩陣不是方陣,沒有行列式");
 9             return Double.MIN_VALUE;
10         }
11         //若為1*1矩陣則直接返回
12         if(mat.getRow() == 1) return mat.getValue(0, 0); 
13         //若為2*2矩陣則直接計算返回結果
14         if(mat.getRow() == 2) {                             
15             return mat.getValue(0, 0)*mat.getValue(1, 1) - mat.getValue(0, 1)*mat.getValue(1, 0);
16         }
17         //行列式的值
18         double matrixValue = 0; 
19         for (int i = 0; i < mat.getCol(); i++) {
20             //獲取0,i位置的余子式,即第一行的余子式
21             Matrix m = getComplementMinor(mat, 0, i);
22             //將第一行的余子式相加 ,遞歸下去
23             matrixValue += Math.pow(-1, i) * getMatrixValue(m);
24             
25         }
26         return matrixValue;
27     }

 

3.根據代數余子式和行列式的值求出伴隨矩陣;

 1 /**
 2      * 求矩陣的伴隨矩陣
 3      * @param mat
 4      * @return
 5      */
 6     public static Matrix getWithMatrix(Matrix mat) {
 7         //創建一個矩陣存放伴隨矩陣的值
 8         Matrix withMatrix = new Matrix(mat.getRow(),mat.getCol());
 9         //遍歷withMatrix存放對應的mat的值
10         for (int i = 0; i < withMatrix.getRow(); i++) {
11             for (int j = 0; j < withMatrix.getCol(); j++) {
12                 double temp = Math.pow(-1, i+j) * MatrixUtil.getMatrixValue(MatrixUtil.getComplementMinor(mat, j, i));
13                 if(Math.abs(temp) <= 10e-6) temp = 0;
14                 withMatrix.setValue(i, j,temp);
15             }
16         }
17         //返回結果
18         return withMatrix;    
19     }

 

4.由伴隨矩陣和矩陣行列式值求逆矩陣。(A^-1 = A* / |A|)。

 1 /**
 2      * 求逆矩陣
 3      * @param mat
 4      * @return
 5      */
 6     public static Matrix getReMatrix(Matrix mat) {
 7         //創建一個矩陣接收逆矩陣數據
 8         Matrix reMatrix = new Matrix(mat.getRow(),mat.getCol()); 
 9         //得到原矩陣行列式的值
10         double value = MatrixUtil.getMatrixValue(mat); 
11         //判斷矩陣行列式的值是否為零
12         if(Math.abs(value) <= 10e-6) {
13             System.out.println("該矩陣不可逆!");
14             return null;
15         }
16         //將原矩陣mat賦值除以原行列式的值value給逆矩陣
17         for (int i = 0; i < reMatrix.getRow(); i++) {
18             for (int j = 0; j < reMatrix.getCol(); j++) {
19                 reMatrix.setValue(i, j, MatrixUtil.getWithMatrix(mat).getValue(i, j) / value);
20             }
21         }
22         return reMatrix;
23         
24     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM