Skip to content

Commit 762f9f5

Browse files
dkarraschKristofferC
authored andcommitted
bugfix for ldiv!(D::Diagonal, B::StridedVecOrMat) and tests (#32104)
(cherry picked from commit 85603e1)
1 parent c2c8e12 commit 762f9f5

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
@@ -437,7 +437,7 @@ function ldiv!(D::Diagonal, B::StridedVecOrMat)
437437
if di == 0
438438
throw(SingularException(i))
439439
end
440-
B[i,j] /= di
440+
B[i,j] = di \ B[i,j]
441441
end
442442
end
443443
return B

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ end
414414
@test sqrt(D) == Diagonal([sqrt([1 2; 3 4]), sqrt([1 2; 3 4])])
415415
end
416416

417+
@testset "linear solve for block diagonal matrices" begin
418+
D = Diagonal([rand(2,2) for _ in 1:5])
419+
b = [rand(2,2) for _ in 1:5]
420+
B = [rand(2,2) for _ in 1:5, _ in 1:5]
421+
@test ldiv!(D, copy(b)) Diagonal(inv.(D.diag)) * b
422+
@test ldiv!(D, copy(B)) Diagonal(inv.(D.diag)) * B
423+
@test rdiv!(copy(B), D) B * Diagonal(inv.(D.diag))
424+
end
425+
417426
@testset "multiplication with Symmetric/Hermitian" begin
418427
for T in (Float64, ComplexF64)
419428
D = Diagonal(randn(T, n))

0 commit comments

Comments
 (0)