Skip to content

Commit c5c054b

Browse files
dkarraschJeffBezanson
authored andcommitted
bugfix for ldiv!(D::Diagonal, B::StridedVecOrMat) and tests (#32104)
(cherry picked from commit 85603e1)
1 parent 5f4a567 commit c5c054b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ function ldiv!(D::Diagonal, B::StridedVecOrMat)
481481
if di == 0
482482
throw(SingularException(i))
483483
end
484-
B[i,j] /= di
484+
B[i,j] = di \ B[i,j]
485485
end
486486
end
487487
return B

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ end
441441
@test det(D) == 4
442442
end
443443

444+
@testset "linear solve for block diagonal matrices" begin
445+
D = Diagonal([rand(2,2) for _ in 1:5])
446+
b = [rand(2,2) for _ in 1:5]
447+
B = [rand(2,2) for _ in 1:5, _ in 1:5]
448+
@test ldiv!(D, copy(b)) Diagonal(inv.(D.diag)) * b
449+
@test ldiv!(D, copy(B)) Diagonal(inv.(D.diag)) * B
450+
@test rdiv!(copy(B), D) B * Diagonal(inv.(D.diag))
451+
end
452+
444453
@testset "multiplication with Symmetric/Hermitian" begin
445454
for T in (Float64, ComplexF64)
446455
D = Diagonal(randn(T, n))

0 commit comments

Comments
 (0)