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
(cherry picked from commit 25e3809)
  • Loading branch information
dkarrasch authored and KristofferC committed Dec 21, 2022
1 parent 9895223 commit 43a5056
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 @@ -699,6 +699,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(Base.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 @@ -738,10 +738,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 "Inner product" begin
Expand Down

0 comments on commit 43a5056

Please sign in to comment.