-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
RFC: Extract Bunch-Kaufman factors and use them for printing #22601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e29cb2d
5878202
8a20788
19aa410
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,11 +69,135 @@ size(B::BunchKaufman, d::Integer) = size(B.LD, d) | |
| issymmetric(B::BunchKaufman) = B.symmetric | ||
| ishermitian(B::BunchKaufman) = !B.symmetric | ||
|
|
||
| function _ipiv2perm_bk(v::AbstractVector{T}, maxi::Integer, uplo::Char) where T | ||
| p = T[1:maxi;] | ||
| uploL = uplo == 'L' | ||
| i = uploL ? 1 : maxi | ||
| # if uplo == 'U' we construct the permutation backwards | ||
| @inbounds while 1 <= i <= length(v) | ||
| vi = v[i] | ||
| if vi > 0 # the 1x1 blocks | ||
| p[i], p[vi] = p[vi], p[i] | ||
| i += uploL ? 1 : -1 | ||
| else # the 2x2 blocks | ||
| if uploL | ||
| p[i + 1], p[-vi] = p[-vi], p[i + 1] | ||
| i += 2 | ||
| else # 'U' | ||
| p[i - 1], p[-vi] = p[-vi], p[i - 1] | ||
| i -= 2 | ||
| end | ||
| end | ||
| end | ||
| return p | ||
| end | ||
|
|
||
| """ | ||
| getindex(B::BunchKaufman, d::Symbol) | ||
|
|
||
| Extract the factors of the Bunch-Kaufman factorization `B`. The factorization can take the | ||
| two forms `L*D*L'` or `U*D*U'` (or `.'` in the complex symmetric case) where `L` is a | ||
| `UnitLowerTriangular` matrix, `U` is a `UnitUpperTriangular`, and `D` is a block diagonal | ||
| symmetric or Hermitian matrix with 1x1 or 2x2 blocks. The argument `d` can be | ||
| - `:D`: the block diagonal matrix | ||
| - `:U`: the upper triangular factor (if factorization is `U*D*U'`) | ||
| - `:L`: the lower triangular factor (if factorization is `L*D*L'`) | ||
| - `:p`: permutation vector | ||
| - `:P`: permutation matrix | ||
|
|
||
| # Examples | ||
| ```jldoctest | ||
| julia> A = [1 2 3; 2 1 2; 3 2 1] | ||
| 3×3 Array{Int64,2}: | ||
| 1 2 3 | ||
| 2 1 2 | ||
| 3 2 1 | ||
|
|
||
| julia> F = bkfact(Symmetric(A, :L)) | ||
| Base.LinAlg.BunchKaufman{Float64,Array{Float64,2}} | ||
| D factor: | ||
| 3×3 Tridiagonal{Float64}: | ||
| 1.0 3.0 ⋅ | ||
| 3.0 1.0 0.0 | ||
| ⋅ 0.0 -1.0 | ||
| L factor: | ||
| 3×3 Base.LinAlg.UnitLowerTriangular{Float64,Array{Float64,2}}: | ||
| 1.0 0.0 0.0 | ||
| 0.0 1.0 0.0 | ||
| 0.5 0.5 1.0 | ||
| permutation: | ||
| 3-element Array{Int64,1}: | ||
| 1 | ||
| 3 | ||
| 2 | ||
| successful: true | ||
|
|
||
| julia> F[:L]*F[:D]*F[:L].' - A[F[:p], F[:p]] | ||
| 3×3 Array{Float64,2}: | ||
| 0.0 0.0 0.0 | ||
| 0.0 0.0 0.0 | ||
| 0.0 0.0 0.0 | ||
|
|
||
| julia> F = bkfact(Symmetric(A)); | ||
|
|
||
| julia> F[:U]*F[:D]*F[:U].' - F[:P]*A*F[:P]' | ||
| 3×3 Array{Float64,2}: | ||
| 0.0 0.0 0.0 | ||
| 0.0 0.0 0.0 | ||
| 0.0 0.0 0.0 | ||
| ``` | ||
| """ | ||
| function getindex(B::BunchKaufman{T}, d::Symbol) where {T<:BlasFloat} | ||
| n = size(B, 1) | ||
| if d == :p | ||
| return _ipiv2perm_bk(B.ipiv, n, B.uplo) | ||
| elseif d == :P | ||
| return eye(T, n)[:,invperm(B[:p])] | ||
| elseif d == :L || d == :U || d == :D | ||
| if B.rook | ||
| # syconvf_rook just added to LAPACK 3.7.0. Uncomment and remove error when we distribute LAPACK 3.7.0 | ||
| # LUD, od = LAPACK.syconvf_rook!(B.uplo, 'C', copy(B.LD), B.ipiv) | ||
| throw(ArgumentError("reconstruction rook pivoted Bunch-Kaufman factorization not implemented yet")) | ||
| else | ||
| LUD, od = LAPACK.syconv!(B.uplo, copy(B.LD), B.ipiv) | ||
| end | ||
| if d == :D | ||
| if B.uplo == 'L' | ||
| odl = od[1:n - 1] | ||
| return Tridiagonal(odl, diag(LUD), B.symmetric ? odl : conj.(odl)) | ||
| else # 'U' | ||
| odu = od[2:n] | ||
| return Tridiagonal(B.symmetric ? odu : conj.(odu), diag(LUD), odu) | ||
| end | ||
| elseif d == :L | ||
| if B.uplo == 'L' | ||
| return UnitLowerTriangular(LUD) | ||
| else | ||
| throw(ArgumentError("factorization is U*D*U.' but you requested L")) | ||
| end | ||
| else # :U | ||
| if B.uplo == 'U' | ||
| return UnitUpperTriangular(LUD) | ||
| else | ||
| throw(ArgumentError("factorization is L*D*L.' but you requested U")) | ||
| end | ||
| end | ||
| else | ||
| throw(KeyError(d)) | ||
| end | ||
| end | ||
|
|
||
| issuccess(B::BunchKaufman) = B.info == 0 | ||
|
|
||
| function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, B::BunchKaufman) | ||
| println(io, summary(B)) | ||
| print(io, "successful: $(issuccess(B))") | ||
| println(io, "D factor:") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test the show method and/or fix the relevant doctest
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've adjusted the doctest
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's an existing one that was showing the raw fields of a BunchKaufman, came up in one of Kristoffer's recent pr's - this will make it fail in a different way
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. I've fixed that one as well. |
||
| show(io, mime, B[:D]) | ||
| println(io, "\n$(B.uplo) factor:") | ||
| show(io, mime, B[Symbol(B.uplo)]) | ||
| println(io, "\npermutation:") | ||
| show(io, mime, B[:p]) | ||
| print(io, "\nsuccessful: $(issuccess(B))") | ||
| end | ||
|
|
||
| function inv(B::BunchKaufman{<:BlasReal}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xref OpenMathLib/OpenBLAS#1118