The Berkowitz recurrence returns the characteristic polynomial full
This proof shows: correctness, termination.
Let R be a commutative ring and A an n×n matrix over R, indexed from 0 as in the pseudocode. For 0≤r≤n−1 let Ar be the trailing principal block in rows and columns r,…,n−1, of size n−r. Polynomials are stored as coefficient lists, highest degree first.
Claim. BERKOWITZ(A) stops and returns the coefficients of χA(x)=det(xI−A).
Termination. Line L3 runs r over n−1 values, the loop of line L6 has s≤n−1 steps, and every other line is a finite computation on lists of length at most n+1.
Lemma 1 (adjugate of xI−S). Let S be an s×s matrix over R with s≥1 and χS(x)=∑k=0sckxk. Then
adj(xI−S)=k=1∑sckj=0∑k−1xk−1−jSj. Proof. Call the right side Y(x), a matrix over R[x]. For each k, (xI−S)∑j=0k−1xk−1−jSj=xkI−Sk, a telescoping sum. So (xI−S)Y(x)=∑k=1sck(xkI−Sk). The term for k=0 would be c0(I−I)=0, so this sum equals ∑k=0sck(xkI−Sk)=χS(x)I−χS(S), and χS(S)=0 by the Cayley-Hamilton theorem, which holds over every commutative ring. Let M=xI−S. Multiply MY=χS(x)I on the left by adj(M); since adj(M)M=det(M)I=χS(x)I, we get χS(x)Y=χS(x)adj(M). The polynomial χS is monic, and a monic polynomial is not a zero divisor in R[x] (the leading coefficient of a product χS⋅f with f=0 is the leading coefficient of f). So Y=adj(M). □
Lemma 2 (bordered determinant). Let α∈R, β a row vector and γ a column vector of length s, and D an s×s matrix over R. Then
det(αγβD)=αdetD−βadj(D)γ. Proof. Both sides are polynomials with integer coefficients in the (s+1)2 entries, so it suffices to prove the identity when the entries are independent indeterminates over Z; every other case follows by substituting ring elements. In that case the entries lie in the field of fractions F of the polynomial ring, and detD=0 because it is a nonzero polynomial. Row reduction with D gives
(10−βD−1I)(αγβD)=(α−βD−1γγ0D).
The first factor has determinant 1, and the last matrix is block lower triangular, so the determinant is (α−βD−1γ)detD=αdetD−βadj(D)γ, using D−1=adj(D)/detD from Cramer's rule. □
Lemma 3 (one step). Write Ar=(aγρS) with a=A[r,r], ρ the rest of row r, γ the rest of column r and S=Ar+1 of size s=n−r−1≥1. Let mj=ρSjγ for j≥0, and let q=(q0,…,qs+1)=(1,−a,−m0,−m1,…,−ms−1). If p=(p0,…,ps) holds the coefficients of χS, highest first, then the coefficients of χAr, highest first, are
pi′=t∑qtpi−t(0≤i≤s+1), where the sum runs over the t with 0≤t≤s+1 and 0≤i−t≤s.
Proof. Apply Lemma 2 to xI−Ar over the ring R[x], with α=x−a, β=−ρ, γ′=−γ and D=xI−S:
χAr(x)=(x−a)χS(x)−ρadj(xI−S)γ. By Lemma 1, with χS=∑kckxk,
ρadj(xI−S)γ=∑k=1sck∑j=0k−1xk−1−jmj.
The coefficient of xe in it is ∑j≥0ce+1+jmj, with ck=0 for k>s. So the coefficient of xe in χAr is
ce−1−ace−j=0∑s−1ce+1+jmj, with c−1=0. Put e=s+1−i and pk=cs−k. The three parts become q0pi, q1pi−1 and ∑jqj+2pi−2−j, where a term is absent when its index of p is outside 0,…,s. Their sum is ∑tqtpi−t. □
Invariant. After line L2, and after each iteration of line L3 with index r, the list p holds the coefficients of χAr′, where r′ is n−1 after line L2 and r after the iteration.
Initialization. An−1 is the 1×1 matrix (A[n−1,n−1]), whose characteristic polynomial is x−A[n−1,n−1], the list (1,−A[n−1,n−1]) of line L2.
Maintenance. In the iteration with index r, line L5 starts q=(1,−a) and v=ρ. Before the step k of line L6, v=ρSk: true for k=0, and line L8 multiplies v on the right by S. So line L7 appends −vγ=−ρSkγ=−mk, and after the loop q is exactly the vector of Lemma 3. The loop skips the last product vS, which is never used. By the invariant before the iteration, p holds χS=χAr+1, and line L9 forms the sums pi′ of Lemma 3. So p now holds χAr.
End. If n=0, line L1 returns (1), the characteristic polynomial of the empty matrix. Otherwise, after the last iteration (r=0, or no iteration when n=1), p holds χA0=χA, which line L10 returns. Only additions, subtractions and multiplications in R were used. ■
Remark. Since χA(0)=det(−A)=(−1)ndetA, the last coefficient times (−1)n is the determinant, which is how the implementation's berkowitz_determinant gets it.