From 0c818df3caa413ff3c70876a9c981e704ce9db73 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 3 Oct 2020 22:48:04 -0400 Subject: [PATCH] mjo/ldlt.py: get fast block_ldlt() working. --- mjo/ldlt.py | 90 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/mjo/ldlt.py b/mjo/ldlt.py index 4955d9f..6db747f 100644 --- a/mjo/ldlt.py +++ b/mjo/ldlt.py @@ -372,6 +372,9 @@ def block_ldlt(A): # entries of "L" in the copy of "A" that we're going to make. # Contrast this with the non-block LDL^T factorization where the # entries of both "L" and "D" overwrite the lower-left half of "A". + # + # This grants us an additional speedup, since we don't have to + # permute the rows/columns of "L" *and* "A" at each iteration. ring = A.base_ring().fraction_field() A = A.change_ring(ring) MS = A.matrix_space() @@ -385,30 +388,44 @@ def block_ldlt(A): p = list(range(n)) d = [] - def pivot1x1(M, k, s): + def swap_rows_columns(M, k, s): r""" - Perform a 1x1 pivot swapping rows/columns `k` and `s >= k`. - Relies on the fact that matrices are passed by reference, - since for performance reasons this routine should overwrite - its argument. Updates the local variables ``p`` and ``d`` as - well. - - Note that ``A`` is passed in by reference here, so it doesn't - matter if we shadow the name ``A`` with itself. + Swap rows/columns ``k`` and ``s`` of the matrix ``M``, and update + the list ``p`` accordingly. """ if s > k: # s == k would swap row/column k with itself, and we don't - # actually want to perform the identity permutation. - # We don't have to permute "L" separately so long as "L" - # is stored within "A". - A.swap_columns(k,s) - A.swap_rows(k,s) + # actually want to perform the identity permutation. If + # you work out the recursive factorization by hand, you'll + # notice that the rows/columns of "L" need to be permuted + # as well. A nice side effect of storing "L" within "A" + # itself is that we can skip that step. The first column + # of "L" is hit by all of the transpositions in + # succession, and the second column is hit by all but the + # first transposition, and so on. + M.swap_columns(k,s) + M.swap_rows(k,s) - # Update the permutation "matrix" with the swap we just did. p_k = p[k] p[k] = p[s] p[s] = p_k + # No return value, we're only interested in the "side effects" + # of modifing the matrix M (by reference) and the permutation + # list p (which is in scope when this function is defined). + return + + + def pivot1x1(M, k, s): + r""" + Perform a 1x1 pivot swapping rows/columns `k` and `s >= k`. + Relies on the fact that matrices are passed by reference, + since for performance reasons this routine should overwrite + its argument. Updates the local variables ``p`` and ``d`` as + well. + """ + swap_rows_columns(M,k,s) + # Now the pivot is in the (k,k)th position. d.append( matrix(ring, 1, [[A[k,k]]]) ) @@ -455,7 +472,7 @@ def block_ldlt(A): # because "lambda" can lead to some confusion. Beware: # the subdiagonals of our matrix are being overwritten! # So we actually use the corresponding row entries instead. - column_1_subdiag = [ a_ki.abs() for a_ki in A[k,1:].list() ] + column_1_subdiag = [ a_ki.abs() for a_ki in A[k,k+1:].list() ] omega_1 = max([ a_ki for a_ki in column_1_subdiag ]) if omega_1 == 0: @@ -504,14 +521,51 @@ def block_ldlt(A): if A[r,r].abs() > alpha*omega_r: # This is Step (3) in Higham or Step (5) in B&K. Still a 1x1 # pivot, but this time we need to swap rows/columns k and r. - pivot1x1(A1,k,r) + pivot1x1(A,k,r) k += 1 continue # If we've made it this far, we're at Step (4) in Higham or # Step (6) in B&K, where we perform a 2x2 pivot. - k += 2 + swap_rows_columns(A,k+1,r) + + # The top-left 2x2 submatrix (starting at position k,k) is now + # our pivot. + E = A[k:k+2,k:k+2] + d.append(E) + + C = A[k+2:n,k:k+2] + B = A[k+2:,k+2:] + + # TODO: don't invert, there are better ways to get the C*E^(-1) + # that we need. + E_inverse = E.inverse() + + schur_complement = B - (C*E_inverse*C.transpose()) + # Compute the Schur complement that we'll work on during + # the following iteration, and store it back in the lower- + # right-hand corner of "A". + for i in range(n-k-2): + for j in range(i+1): + A[k+2+j,k+2+i] = A[k+2+j,k+2+i] - schur_complement[j,i] + A[k+2+i,k+2+j] = A[k+2+j,k+2+i] # keep it symmetric! + + # The on- and above-diagonal entries of "L" will be fixed + # later, so we only need to worry about the lower-left entry + # of the 2x2 identity matrix that belongs at the top of the + # new column of "L". + A[k+1,k] = 0 + for i in range(n-k-2): + for j in range(2): + # Store the new (k and (k+1)st) columns of "L" within + # the lower-left-hand corner of "A", being sure to set + # the lower-left entries from the upper-right ones to + # avoid collisions. + A[k+i+2,k+j] = (C*E_inverse)[i,j] + + + k += 2 MS = A.matrix_space() P = MS.matrix(lambda i,j: p[j] == i) -- 2.44.2