From 4ce29abc71354a0dc6e89a00f0b5f1f1e21b66d9 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 22 Jun 2017 09:09:41 +0200 Subject: [PATCH] fix matrix multiplication interaction with HermOrSym and Diagonal --- base/linalg/diagonal.jl | 4 ++-- test/linalg/diagonal.jl | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/base/linalg/diagonal.jl b/base/linalg/diagonal.jl index 954848191bd58..ad00a49954e4a 100644 --- a/base/linalg/diagonal.jl +++ b/base/linalg/diagonal.jl @@ -150,9 +150,9 @@ end (*)(D::Diagonal, B::AbstractTriangular) = A_mul_B!(D, copy(B)) (*)(A::AbstractMatrix, D::Diagonal) = - scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), A, D.diag) + scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), A, D.diag) (*)(D::Diagonal, A::AbstractMatrix) = - scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), D.diag, A) + scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), D.diag, A) A_mul_B!(A::Union{LowerTriangular,UpperTriangular}, D::Diagonal) = typeof(A)(A_mul_B!(A.data, D)) diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index 01ea996517e32..62d2de4a517df 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -357,3 +357,18 @@ end @test logm(D) == Diagonal([logm([1 2; 3 4]), logm([1 2; 3 4])]) @test sqrtm(D) == Diagonal([sqrtm([1 2; 3 4]), sqrtm([1 2; 3 4])]) end + +@testset "multiplication with Symmetric/Hermitian" begin + for T in (Float64, Complex128) + D = Diagonal(randn(T, n)) + A = randn(T, n, n); A = A'A + S = Symmetric(A) + H = Hermitian(A) + for f in (*, Ac_mul_B, A_mul_Bc, Ac_mul_Bc, At_mul_B, A_mul_Bt, At_mul_Bt) + @test f(D, S) ≈ f(Matrix(D), Matrix(S)) + @test f(D, H) ≈ f(Matrix(D), Matrix(H)) + @test f(S, D) ≈ f(Matrix(S), Matrix(D)) + @test f(S, H) ≈ f(Matrix(S), Matrix(H)) + end + end +end