From 90caa80efd3e578737391cf01f88ac7f3aaa230e Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Tue, 18 Jun 2019 08:28:27 +0200 Subject: [PATCH] bugfix for ldiv!(D::Diagonal, B::StridedVecOrMat) and tests (#32104) (cherry picked from commit 85603e15a025b011a95e705d6a43117cb478e30a) --- stdlib/LinearAlgebra/src/diagonal.jl | 2 +- stdlib/LinearAlgebra/test/diagonal.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/diagonal.jl b/stdlib/LinearAlgebra/src/diagonal.jl index f5714e6444170c..c6ecf559f000f0 100644 --- a/stdlib/LinearAlgebra/src/diagonal.jl +++ b/stdlib/LinearAlgebra/src/diagonal.jl @@ -437,7 +437,7 @@ function ldiv!(D::Diagonal, B::StridedVecOrMat) if di == 0 throw(SingularException(i)) end - B[i,j] /= di + B[i,j] = di \ B[i,j] end end return B diff --git a/stdlib/LinearAlgebra/test/diagonal.jl b/stdlib/LinearAlgebra/test/diagonal.jl index 149c07c91a3ce6..103521bdfe0f2f 100644 --- a/stdlib/LinearAlgebra/test/diagonal.jl +++ b/stdlib/LinearAlgebra/test/diagonal.jl @@ -414,6 +414,15 @@ end @test sqrt(D) == Diagonal([sqrt([1 2; 3 4]), sqrt([1 2; 3 4])]) end +@testset "linear solve for block diagonal matrices" begin + D = Diagonal([rand(2,2) for _ in 1:5]) + b = [rand(2,2) for _ in 1:5] + B = [rand(2,2) for _ in 1:5, _ in 1:5] + @test ldiv!(D, copy(b)) ≈ Diagonal(inv.(D.diag)) * b + @test ldiv!(D, copy(B)) ≈ Diagonal(inv.(D.diag)) * B + @test rdiv!(copy(B), D) ≈ B * Diagonal(inv.(D.diag)) +end + @testset "multiplication with Symmetric/Hermitian" begin for T in (Float64, ComplexF64) D = Diagonal(randn(T, n))