Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit ee6f749

Browse files
fredrikekreDrTodd13
authored andcommitted
fix matrix multiplication interaction with HermOrSym and Diagonal (JuliaLang#22474)
1 parent 0cb4880 commit ee6f749

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

base/linalg/diagonal.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ end
150150
(*)(D::Diagonal, B::AbstractTriangular) = A_mul_B!(D, copy(B))
151151

152152
(*)(A::AbstractMatrix, D::Diagonal) =
153-
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), A, D.diag)
153+
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), A, D.diag)
154154
(*)(D::Diagonal, A::AbstractMatrix) =
155-
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), D.diag, A)
155+
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), D.diag, A)
156156

157157
A_mul_B!(A::Union{LowerTriangular,UpperTriangular}, D::Diagonal) =
158158
typeof(A)(A_mul_B!(A.data, D))

test/linalg/diagonal.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,18 @@ end
357357
@test logm(D) == Diagonal([logm([1 2; 3 4]), logm([1 2; 3 4])])
358358
@test sqrtm(D) == Diagonal([sqrtm([1 2; 3 4]), sqrtm([1 2; 3 4])])
359359
end
360+
361+
@testset "multiplication with Symmetric/Hermitian" begin
362+
for T in (Float64, Complex128)
363+
D = Diagonal(randn(T, n))
364+
A = randn(T, n, n); A = A'A
365+
S = Symmetric(A)
366+
H = Hermitian(A)
367+
for f in (*, Ac_mul_B, A_mul_Bc, Ac_mul_Bc, At_mul_B, A_mul_Bt, At_mul_Bt)
368+
@test f(D, S) f(Matrix(D), Matrix(S))
369+
@test f(D, H) f(Matrix(D), Matrix(H))
370+
@test f(S, D) f(Matrix(S), Matrix(D))
371+
@test f(S, H) f(Matrix(S), Matrix(H))
372+
end
373+
end
374+
end

0 commit comments

Comments
 (0)