Skip to content

Commit 85603e1

Browse files
dkarraschandreasnoack
authored andcommitted
bugfix for ldiv!(D::Diagonal, B::StridedVecOrMat) and tests (#32104)
1 parent 6451bdd commit 85603e1

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
@@ -455,6 +455,15 @@ end
455455
@test det(D) == 4
456456
end
457457

458+
@testset "linear solve for block diagonal matrices" begin
459+
D = Diagonal([rand(2,2) for _ in 1:5])
460+
b = [rand(2,2) for _ in 1:5]
461+
B = [rand(2,2) for _ in 1:5, _ in 1:5]
462+
@test ldiv!(D, copy(b)) Diagonal(inv.(D.diag)) * b
463+
@test ldiv!(D, copy(B)) Diagonal(inv.(D.diag)) * B
464+
@test rdiv!(copy(B), D) B * Diagonal(inv.(D.diag))
465+
end
466+
458467
@testset "multiplication with Symmetric/Hermitian" begin
459468
for T in (Float64, ComplexF64)
460469
D = Diagonal(randn(T, n))

0 commit comments

Comments
 (0)