Add LDL decomposition
See issue #865, this decomposition is also mentioned in #762 but in the end only the UDU decomposition was implemented (PR #766). I need this for my own use case where I basically want a Cholesky decomposition of a semi-definite covariance matrix to generate random correlated numbers. Currently I was applying the Cholesky decomposition on a modified matrix with all zero rows/columns removed, which are then later re-added to the lower triangular matrix.
I used the code from #766 as a template, but changed the code to implement the LDL algorithm instead, everything else is basically the same just re-named. This is not the standard algorithm, but a variant that should also work with semi-definite matrices (i.e. with eigenvalues equal to zero), which I need. I haven't added any tests for this yet though. I do not know why the existing UDU decomposition also does not include this capability as its just an extra 1-2 lines of code.
I also added a cholesky_l function that reconstructs the lower triangular matrix from the Cholesky decomposition.
hi, looking forward to this being added - I was wondering whether it's necessary for it to insist on the scalar type having trait RealField? In the UDU factorisation PR it's set to RealField because nobody was sure whether the UDU decomposition made sense for complex Hermitian matrices, but LDL is pretty widely used for complex matrices.
I tested it out on a few complex Hermitian matrices and got the results I was expecting. Maybe the functionality could be extended to ComplexField and you add some tests for equality with Cholesky on positive-definite complex Hermitian examples?
I added functionality for Hermitian matrices, and rewrote the tests.
Currently, the algorithm checks for diagonal values that are exactly zero, to zero out the corresponding columns/rows. One could add a threshold value eps, similar to the way its done for the pseudo_inverse() function, so that values below eps are set to zero. Only problem here is that the diagonal values have no real relation to the eigenvalues, so the interpretation is not clear.
@ajefweiss cool! for clarity i'm not a maintainer/reviewer of nalgebra i'm just interested in having this in the package :)
Hi @ajefweiss, I'd be willing to review this but I need your help :slightly_smiling_face: I skimmed the paper linked in https://github.com/dimforge/nalgebra/issues/762, but I couldn't find an algorithm for the LDL decomposition. I see there's a screenshot in the issue for UDU decomposition. Could you point me to the reference that tells me how you implemented the LDL decomposition, so I can read up on it?
EDIT: well, I scrolled down in the issue and there I see this. Is that the reference you used?
Hi @geo-ant!
Thank you for taking a look at this. The actual reference I used is just the wikipedia entry which has imo a nicer description of the algorithm (https://en.wikipedia.org/wiki/Cholesky_decomposition#LDL_decomposition_2) which cites "Fundamentals of Matrix Computations" from Watkins (PDF versions are easily obtainable online). The relevant pages here are 82-84 although the algorithm here is just explained as a variant of the LU decomposition without being explicitly written out.
The only problem is that I have not been able to find an explicit reference for an algorithm that allows for semi-positive definite matrices. Both the reference you linked, and mine, require positive-definite matrices in their algorithms due to the division by v_i / d_i. Thus when one of the diagonal entries is 0 the algorithm fails. From my understanding, the A=LL/LDL factorizations are unique if, and only if, the matrix A is positive-definite. The theorems from both references state that the LDL factorization does exist only under the requirement that A is symmetric (and not necessarily positive definite).
I think the factorization is no longer unique because you can fill out the relevant column in the L matrix with any values as they will be subsequently zero'd out in the multiplication. In my code this column is filled with zeros.
PS: I might try to think a better name for the function "cholesky_ldl" as I actually don't think that name is appropriate and might be a bit confusing.
I am planning to take a look at this again this weekend and clean up the remaining documentation. I believe that having both implementations should be useful as the strictly diagonal LDL version should be quicker if the assumed constraints hold (I plan on testing this).
There is only one thing I think that needs to be resolved regarding the naming convention. LAPACK has both LDL / UDU implemented incl. using the Bunch-Kaufman algorithm. But algebra nalready now has the strictly diagonal version for UDU already implemented. It might be confusing to call the BK implementation just "LDL" as it behaves differently than the existing UDU.
@ajefweiss One thought on naming: since this is the strictly diagonal / no-pivot variant, would something like Qdldl make sense here? That seems to make the restricted contract clearer than plain LDL.
Also, would it make sense to detect breakdown explicitly instead of zeroing the column and continuing when a diagonal pivot is zero? The [QDLDL implementation](https://github.com/osqp/qdldl/blob/master/src/qdldl.c) feels fairly close in spirit here and returns failure on zero pivots.
If semidefinite-ish cases are still important, maybe that behavior could be configurable via an API choice or pivot tolerance.
Curious what you think.
Hey, @ajefweiss and @awinick, first of all sorry to both of you for not reviewing your implementations so far, life got in the way...
I'm happy you are already discussing the tradeoffs of having both implementations in nalgebra. In terms of naming, I think we could follow the suggestion of @alexhroom and do it like in faer, while also making sure that the documentation is very explicit about what the decomposition does and doesn't provide.
I have added extra documentation pointing out the limitation and also referred to the BK algorithm. Until that's merged I don't think I have a good way of creating a proper link to those types but that's always something we can clean up later.
Renamed d/l functions to have the same names as in the upcoming LBL implementation.
LDL::new now has the "expected" behavior and returns None if there is a zero pivot.
I also added a LDL::new_unchecked for the special case with zero pivots. This decomposition may or may not return a valid LDL decomposition depending on the structure of the matrix, always works for covariance matrices. I have not added this function in the decomposition.rs file