Skip to content

inconsistent return value of dot for empty vectors #1483

@matthias314

Description

@matthias314

It seems that for non-empty zero vectors, dot consistently returns the zero element of the target type:

julia> x = Complex{Float64}[0]; y = Float64[0];

julia> dot(x, y), dot(-x, y), dot(conj(x), y), dot(x, -y)
(0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im)

For empty vectors, however, this is not always the case:

julia> v = Complex{Float64}[]; w = Float64[];

julia> dot(v, w)
0.0 - 0.0im

The reason are the lines

if lx == 0
return dot(zero(eltype(x)), zero(eltype(y)))
end

in dot(x::AbstractArray, y::AbstractArray). Wouldn't it be more consistent to use zero(dot(zero(eltype(x)), zero(eltype(y)))) or maybe just zero(eltype(x))*zero(eltype(y))?

By the way, the above examples also show that the claim

dot is semantically equivalent to sum(dot(vx,vy) for (vx,vy) in zip(x, y))

in the docstring is not correct:

julia> sum(dot(vx,vy) for (vx,vy) in zip(x, y))
0.0 - 0.0im

julia> sum(dot(vx,vy) for (vx,vy) in zip(v, w))
ERROR: ArgumentError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions