Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special case empty covec-diagonal-vec product #35557

Merged
merged 10 commits into from
Apr 25, 2020
18 changes: 14 additions & 4 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,20 @@ end
# disambiguation methods: * of Diagonal and Adj/Trans AbsVec
*(x::Adjoint{<:Any,<:AbstractVector}, D::Diagonal) = Adjoint(map((t,s) -> t'*s, D.diag, parent(x)))
*(x::Transpose{<:Any,<:AbstractVector}, D::Diagonal) = Transpose(map((t,s) -> transpose(t)*s, D.diag, parent(x)))
*(x::Adjoint{<:Any,<:AbstractVector}, D::Diagonal, y::AbstractVector) =
mapreduce(t -> t[1]*t[2]*t[3], +, zip(x, D.diag, y))
*(x::Transpose{<:Any,<:AbstractVector}, D::Diagonal, y::AbstractVector) =
mapreduce(t -> t[1]*t[2]*t[3], +, zip(x, D.diag, y))
function *(x::Adjoint{<:Any,<:AbstractVector}, D::Diagonal, y::AbstractVector)
if all(isempty.((x, D, y)))
return zero(promote_type(eltype.((x, D, y))...))
else
return mapreduce(t -> t[1]*t[2]*t[3], +, zip(x, D.diag, y))
end
MasonProtter marked this conversation as resolved.
Show resolved Hide resolved
end
function *(x::Transpose{<:Any,<:AbstractVector}, D::Diagonal, y::AbstractVector)
if all(isempty.((x, D, y)))
return zero(promote_type(eltype.((x, D, y))...))
else
return mapreduce(t -> t[1]*t[2]*t[3], +, zip(x, D.diag, y))
end
MasonProtter marked this conversation as resolved.
Show resolved Hide resolved
end
function dot(x::AbstractVector, D::Diagonal, y::AbstractVector)
mapreduce(t -> dot(t[1], t[2], t[3]), +, zip(x, D.diag, y))
MasonProtter marked this conversation as resolved.
Show resolved Hide resolved
end
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,9 @@ end
@test s1 == prod(sign, d)
end

@testset "Empty (#35424)" begin
@test zeros(0)'*Diagonal(zeros(0))*zeros(0) === 0.0
@test zeros(0)'*Diagonal(zeros(Complex{Int}, 0))*zeros(0) === 0.0 + 0.0im
end

end # module TestDiagonal