The Bareiss algorithm returns the determinant, with exact divisions full
This proof shows: correctness, termination.
Let A0 be the input, an n×n matrix over an integral domain R with field of fractions F, and let A be the working matrix. Indices start at 0, as in the pseudocode. Line L7 swaps two rows of A at or below row k; let A′ be the input with its rows in the current order, so that A′ is A0 with the same swaps applied. For 0≤k≤n let dk be the leading principal minor of order k of A′, with d0=1. For k≤i,j≤n−1 let
mk(i,j)=detA′[{0,…,k−1,i},{0,…,k−1,j}], the minor of A′ in the first k rows plus row i and the first k columns plus column j. Note that mk(k,k)=dk+1 and m0(i,j)=A′[i,j].
Claim. BAREISS(A0) stops, every division on line L10 is exact in R, and the result is detA0.
Termination. Line L3 runs k over at most n−1 values, and the loops on lines L5, L8 and L9 have fewer than n steps each.
Lemma (the Bareiss identity, a case of Sylvester's identity). If dk=0, then for all i,j with k<i,j≤n−1,
dk⋅mk+1(i,j)=mk(k,k)mk(i,j)−mk(i,k)mk(k,j). Proof. Let C be the (k+2)×(k+2) submatrix of A′ in the rows 0,…,k,i and the columns 0,…,k,j, so that detC=mk+1(i,j). Let B be its leading k×k block, with detB=dk=0. Since B is invertible over F, we can subtract from row k and from row i of C combinations of the first k rows so that both rows become zero in the first k columns. Call the result C′. It has the block form
C′=(B0XS),S=(skksikskjsij). Adding to a row a combination of other rows does not change a determinant. The same row operations, applied to the submatrix of C in the rows 0,…,k−1,i′ and the columns 0,…,k−1,j′ for i′∈{k,i} and j′∈{k,j}, turn it into (B0∗si′j′), which has determinant dksi′j′. So mk(i′,j′)=dksi′j′ for these four pairs. Also detC=detC′=dkdetS. Hence
mk(k,k)mk(i,j)−mk(i,k)mk(k,j)=dk2(skksij−sikskj)=dk2detS=dkdetC=dkmk+1(i,j).□ Invariant. At the start of the iteration of line L3 with index k, and also when the loop ends, with k=n−1:
- (J1) detA0=s⋅detA′.
- (J2) dk=0, and prev=dk.
- (J3) A[i,j]=mk(i,j) for all k≤i,j≤n−1.
- (J4) A[t,j]=mt(t,j) for all t<k and t≤j≤n−1.
Initialization. For k=0, no swap has happened, so A′=A0 and s=1. Also prev=1=d0, and m0(i,j)=A0[i,j]=A[i,j]. (J4) is empty.
Maintenance. Assume the invariant at the start of iteration k≤n−2. By (J3), A[i,k]=mk(i,k) for i≥k.
Case 1: lines L4 to L6 return 0. Then mk(i,k)=0 for every i≥k. Apply to all of A′ the row operations of the lemma: subtract from every row i≥k the combination of the first k rows that clears its first k entries. The result is (B0XS) with an (n−k)×(n−k) block S, and as in the lemma mk(i,j)=dkSij. So the first column of S is zero, detS=0, and detA′=dkdetS=0. By (J1), detA0=0, and the return value 0 is correct.
Case 2: line L7 swaps rows k and p>k. This swaps two rows of A′ that are not among the first k, so dk does not change, and the entries of the rows k and p of A are exchanged exactly as the minors mk(k,⋅) and mk(p,⋅) of the new A′ require. So (J3) and (J4) still hold. The swap negates detA′ and line L7 negates s, so (J1) still holds.
After lines L4 to L7, A[k,k]=mk(k,k)=dk+1=0. For k<i,j≤n−1, line L10 computes
prevA[k,k]A[i,j]−A[i,k]A[k,j]=dkmk(k,k)mk(i,j)−mk(i,k)mk(k,j)=mk+1(i,j) by the lemma. The value mk+1(i,j) is a minor of an integral domain matrix, so it is an element of R. In an integral domain, dk=0 divides the numerator with a unique quotient, so the division is exact and gives this minor. Line L10 reads A[i,k] and A[k,j], which it does not change in the iteration, so the order of the updates does not matter. Row k is not changed, so it holds mk(k,j) for j≥k, which gives (J4) for k+1. Line L11 sets prev=A[k,k]=dk+1=0, which gives (J2) for k+1. So the invariant holds at the start of iteration k+1.
End. If n=0, line L1 returns 1=detA0. Otherwise, if no return happens inside the loop, the loop ends with k=n−1. By (J3), A[n−1,n−1]=mn−1(n−1,n−1)=dn=detA′, and by (J1) line L12 returns sdetA′=s2detA0=detA0. ■
Remark. The proof shows more than the claim. Every number that the algorithm stores is a minor of the row-permuted input, and every numerator on line L10 is dk times such a minor. This is the fact that bounds the sizes of the numbers by the Hadamard bound.