Skip to content

Commit 7b70d49

Browse files
timholyandreasnoack
authored andcommitted
BunchKauffman: correct permutation for rook pivoting (fixes #32080) (#32108)
1 parent 516067b commit 7b70d49

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

stdlib/LinearAlgebra/src/bunchkaufman.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ size(B::BunchKaufman, d::Integer) = size(getfield(B, :LD), d)
125125
issymmetric(B::BunchKaufman) = B.symmetric
126126
ishermitian(B::BunchKaufman) = !B.symmetric
127127

128-
function _ipiv2perm_bk(v::AbstractVector{T}, maxi::Integer, uplo::AbstractChar) where T
128+
function _ipiv2perm_bk(v::AbstractVector{T}, maxi::Integer, uplo::AbstractChar, rook::Bool) where T
129129
require_one_based_indexing(v)
130130
p = T[1:maxi;]
131131
uploL = uplo == 'L'
@@ -137,11 +137,16 @@ function _ipiv2perm_bk(v::AbstractVector{T}, maxi::Integer, uplo::AbstractChar)
137137
p[i], p[vi] = p[vi], p[i]
138138
i += uploL ? 1 : -1
139139
else # the 2x2 blocks
140+
if rook
141+
p[i], p[-vi] = p[-vi], p[i]
142+
end
140143
if uploL
141-
p[i + 1], p[-vi] = p[-vi], p[i + 1]
144+
vp = rook ? -v[i+1] : -vi
145+
p[i + 1], p[vp] = p[vp], p[i + 1]
142146
i += 2
143147
else # 'U'
144-
p[i - 1], p[-vi] = p[-vi], p[i - 1]
148+
vp = rook ? -v[i-1] : -vi
149+
p[i - 1], p[vp] = p[vp], p[i - 1]
145150
i -= 2
146151
end
147152
end
@@ -208,7 +213,7 @@ julia> F.U*F.D*F.U' - F.P*A*F.P'
208213
function getproperty(B::BunchKaufman{T}, d::Symbol) where {T<:BlasFloat}
209214
n = size(B, 1)
210215
if d == :p
211-
return _ipiv2perm_bk(getfield(B, :ipiv), n, getfield(B, :uplo))
216+
return _ipiv2perm_bk(getfield(B, :ipiv), n, getfield(B, :uplo), B.rook)
212217
elseif d == :P
213218
return Matrix{T}(I, n, n)[:,invperm(B.p)]
214219
elseif d == :L || d == :U || d == :D

stdlib/LinearAlgebra/test/bunchkaufman.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ end
145145
@test F\v5 == F\v6[1:5]
146146
end
147147

148+
@testset "issue #32080" begin
149+
A = Symmetric([-5 -9 9; -9 4 1; 9 1 2])
150+
B = bunchkaufman(A, true)
151+
@test B.U * B.D * B.U' A[B.p, B.p]
152+
end
153+
148154
@test_throws DomainError logdet(bunchkaufman([-1 -1; -1 1]))
149155
@test logabsdet(bunchkaufman([8 4; 4 2]; check = false))[1] == -Inf
150156
@test isa(bunchkaufman(Symmetric(ones(0,0))), BunchKaufman) # 0x0 matrix

0 commit comments

Comments
 (0)