|
| 1 | +<!--?title Calculating the determinant using Kraut method--> |
| 2 | + |
| 3 | +# Calculating the determinant using Kraut method in $O(N^3)$ |
| 4 | + |
| 5 | +In this article, we'll describe how to find the determinant of the matrix using Kraut method, which works in $O(N^3)$. |
| 6 | + |
| 7 | +The Kraut algorithm finds decomposition of matrix $A$ as $A = L U$ where L is lower and and U is upper triangular matrix. Without loss of generality, we can assume that all the diagonal elements of L are equal to 1. But knowing these matrices, it is easy to calculate the determinant of A: it is equal to the product of all the elements on the main diagonal of the matrix U. |
| 8 | + |
| 9 | +There is a theorem stating that any invertible matrix has a LU-decomposition, and it is unique, if and only if all its principle minors are non-zero. It should be recall that we consider only such decomposition in which the diagonal L consists of ones. |
| 10 | + |
| 11 | +Let A be the matrix and N is its size. We will find the elements of the matrices L and U using the following steps: |
| 12 | + |
| 13 | +1. Let $L_{i i} = 1$ for $i = 1, 2, ..., N$ |
| 14 | +2. For each $j = 1, 2, ..., N$ perform: |
| 15 | + - For $i = 1, 2, ..., j$ find value $U_{ij}$: |
| 16 | + $U_{ij} = A_{ij} - Sum( L_{ik} * U_{kj} )$ |
| 17 | + where the sum is over all $k = 1, 2, ..., i-1$. |
| 18 | + - Next, for $i = j+1, j+2, ..., N$ have: |
| 19 | + $L_{ij} = (A_{ij} - Sum( L_{ik} * U_{kj} )) / U_{jj}$, |
| 20 | + where the sum is taken over all $k = 1, 2, ..., j-1$. |
| 21 | + |
| 22 | +## Implementation |
| 23 | + |
| 24 | +```java |
| 25 | +static BigInteger det (BigDecimal a [][], int n) { |
| 26 | + try { |
| 27 | + |
| 28 | + for (int i=0; i<n; i++) { |
| 29 | + boolean nonzero = false; |
| 30 | + for (int j=0; j<n; j++) |
| 31 | + if (a[i][j].compareTo (new BigDecimal (BigInteger.ZERO)) > 0) |
| 32 | + nonzero = true; |
| 33 | + if (!nonzero) |
| 34 | + return BigInteger.ZERO; |
| 35 | + } |
| 36 | + |
| 37 | + BigDecimal scaling [] = new BigDecimal [n]; |
| 38 | + for (int i=0; i<n; i++) { |
| 39 | + BigDecimal big = new BigDecimal (BigInteger.ZERO); |
| 40 | + for (int j=0; j<n; j++) |
| 41 | + if (a[i][j].abs().compareTo (big) > 0) |
| 42 | + big = a[i][j].abs(); |
| 43 | + scaling[i] = (new BigDecimal (BigInteger.ONE)) .divide |
| 44 | + (big, 100, BigDecimal.ROUND_HALF_EVEN); |
| 45 | + } |
| 46 | + |
| 47 | + int sign = 1; |
| 48 | + |
| 49 | + for (int j=0; j<n; j++) { |
| 50 | + for (int i=0; i<j; i++) { |
| 51 | + BigDecimal sum = a[i][j]; |
| 52 | + for (int k=0; k<i; k++) |
| 53 | + sum = sum.subtract (a[i][k].multiply (a[k][j])); |
| 54 | + a[i][j] = sum; |
| 55 | + } |
| 56 | + |
| 57 | + BigDecimal big = new BigDecimal (BigInteger.ZERO); |
| 58 | + int imax = -1; |
| 59 | + for (int i=j; i<n; i++) { |
| 60 | + BigDecimal sum = a[i][j]; |
| 61 | + for (int k=0; k<j; k++) |
| 62 | + sum = sum.subtract (a[i][k].multiply (a[k][j])); |
| 63 | + a[i][j] = sum; |
| 64 | + BigDecimal cur = sum.abs(); |
| 65 | + cur = cur.multiply (scaling[i]); |
| 66 | + if (cur.compareTo (big) >= 0) { |
| 67 | + big = cur; |
| 68 | + imax = i; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (j != imax) { |
| 73 | + for (int k=0; k<n; k++) { |
| 74 | + BigDecimal t = a[j][k]; |
| 75 | + a[j][k] = a[imax][k]; |
| 76 | + a[imax][k] = t; |
| 77 | + } |
| 78 | + |
| 79 | + BigDecimal t = scaling[imax]; |
| 80 | + scaling[imax] = scaling[j]; |
| 81 | + scaling[j] = t; |
| 82 | + |
| 83 | + sign = -sign; |
| 84 | + } |
| 85 | + |
| 86 | + if (j != n-1) |
| 87 | + for (int i=j+1; i<n; i++) |
| 88 | + a[i][j] = a[i][j].divide |
| 89 | + (a[j][j], 100, BigDecimal.ROUND_HALF_EVEN); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + BigDecimal result = new BigDecimal (1); |
| 94 | + if (sign == -1) |
| 95 | + result = result.negate(); |
| 96 | + for (int i=0; i<n; i++) |
| 97 | + result = result.multiply (a[i][i]); |
| 98 | + |
| 99 | + return result.divide |
| 100 | + (BigDecimal.valueOf(1), 0, BigDecimal.ROUND_HALF_EVEN).toBigInteger(); |
| 101 | + } |
| 102 | + catch (Exception e) { |
| 103 | + return BigInteger.ZERO; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments