Skip to content

Commit 25e3809

Browse files
authored
Check sizes in 3-arg diagonal (dot-)product (#47114)
1 parent b67adb1 commit 25e3809

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ end
808808
dot(A::AbstractMatrix, B::Diagonal) = conj(dot(B, A))
809809

810810
function _mapreduce_prod(f, x, D::Diagonal, y)
811+
if !(length(x) == length(D.diag) == length(y))
812+
throw(DimensionMismatch("x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
813+
end
811814
if isempty(x) && isempty(D) && isempty(y)
812815
return zero(promote_op(f, eltype(x), eltype(D), eltype(y)))
813816
else

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,14 @@ end
977977
@test s1 == prod(sign, d)
978978
end
979979

980-
@testset "Empty (#35424)" begin
980+
@testset "Empty (#35424) & size checks (#47060)" begin
981981
@test zeros(0)'*Diagonal(zeros(0))*zeros(0) === 0.0
982982
@test transpose(zeros(0))*Diagonal(zeros(Complex{Int}, 0))*zeros(0) === 0.0 + 0.0im
983983
@test dot(zeros(Int32, 0), Diagonal(zeros(Int, 0)), zeros(Int16, 0)) === 0
984+
@test_throws DimensionMismatch zeros(2)' * Diagonal(zeros(2)) * zeros(3)
985+
@test_throws DimensionMismatch zeros(3)' * Diagonal(zeros(2)) * zeros(2)
986+
@test_throws DimensionMismatch dot(zeros(2), Diagonal(zeros(2)), zeros(3))
987+
@test_throws DimensionMismatch dot(zeros(3), Diagonal(zeros(2)), zeros(2))
984988
end
985989

986990
@testset "Diagonal(undef)" begin

0 commit comments

Comments
 (0)