Skip to content

Commit

Permalink
Check sizes in 3-arg diagonal (dot-)product (#47114)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch authored Oct 10, 2022
1 parent b67adb1 commit 25e3809
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ end
dot(A::AbstractMatrix, B::Diagonal) = conj(dot(B, A))

function _mapreduce_prod(f, x, D::Diagonal, y)
if !(length(x) == length(D.diag) == length(y))
throw(DimensionMismatch("x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
end
if isempty(x) && isempty(D) && isempty(y)
return zero(promote_op(f, eltype(x), eltype(D), eltype(y)))
else
Expand Down
6 changes: 5 additions & 1 deletion stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -977,10 +977,14 @@ end
@test s1 == prod(sign, d)
end

@testset "Empty (#35424)" begin
@testset "Empty (#35424) & size checks (#47060)" begin
@test zeros(0)'*Diagonal(zeros(0))*zeros(0) === 0.0
@test transpose(zeros(0))*Diagonal(zeros(Complex{Int}, 0))*zeros(0) === 0.0 + 0.0im
@test dot(zeros(Int32, 0), Diagonal(zeros(Int, 0)), zeros(Int16, 0)) === 0
@test_throws DimensionMismatch zeros(2)' * Diagonal(zeros(2)) * zeros(3)
@test_throws DimensionMismatch zeros(3)' * Diagonal(zeros(2)) * zeros(2)
@test_throws DimensionMismatch dot(zeros(2), Diagonal(zeros(2)), zeros(3))
@test_throws DimensionMismatch dot(zeros(3), Diagonal(zeros(2)), zeros(2))
end

@testset "Diagonal(undef)" begin
Expand Down

0 comments on commit 25e3809

Please sign in to comment.