@return the determinant of the original Matrix A, |A|
()
| 56 | * @return the determinant of the original Matrix A, |A| |
| 57 | */ |
| 58 | public double det() |
| 59 | { |
| 60 | if(!isSquare()) |
| 61 | throw new ArithmeticException("Rectangual matricies do not have a determinat"); |
| 62 | double det = 1; |
| 63 | |
| 64 | for(int i = 0; i < Math.min(U.rows(), U.cols()); i++) |
| 65 | det *= U.get(i, i); |
| 66 | |
| 67 | //We need to swap back P to get the sign, so we make a clone. This could be cached if we need to |
| 68 | int rowSwaps = 0; |
| 69 | |
| 70 | Matrix pCopy = P.clone(); |
| 71 | //The number of row swaps in P is the sign change |
| 72 | for(int i = 0; i < pCopy.cols(); i++) |
| 73 | if(pCopy.get(i, i) != 1) |
| 74 | { |
| 75 | rowSwaps++; |
| 76 | //find the row that has our '1'! |
| 77 | int j = i+1; |
| 78 | while(pCopy.get(j, i) == 0) |
| 79 | j++; |
| 80 | |
| 81 | pCopy.swapRows(i, j);//Dont really care who we swap with, it will work out in the end |
| 82 | } |
| 83 | |
| 84 | |
| 85 | return rowSwaps % 2 !=0 ? -det : det; |
| 86 | } |
| 87 | |
| 88 | public Vec solve(Vec b) |
| 89 | { |