Skip to content

Commit 3d737ec

Browse files
committed
Fix 3-arg dot for empty arrays (#1485)
1 parent 0a79d73 commit 3d737ec

File tree

7 files changed

+15
-8
lines changed

7 files changed

+15
-8
lines changed

src/bidiag.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ function dot(x::AbstractVector, B::Bidiagonal, y::AbstractVector)
12091209
nx, ny = length(x), length(y)
12101210
(nx == size(B, 1) == ny) || throw(DimensionMismatch())
12111211
if nx 1
1212-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(B)), zero(eltype(y)))
1212+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(B)), zero(eltype(y))))
12131213
return dot(x[1], B.dv[1], y[1])
12141214
end
12151215
ev, dv = B.ev, B.dv

src/generic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,8 @@ dot(x, A, y) = dot(x, A*y) # generic fallback for cases that are not covered by
10271027

10281028
function dot(x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
10291029
(axes(x)..., axes(y)...) == axes(A) || throw(DimensionMismatch())
1030+
# outermost zero call to avoid spurious sign ambiguity (like 0.0 - 0.0im)
1031+
any(isempty, (x, y)) && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
10301032
T = typeof(dot(first(x), first(A), first(y)))
10311033
s = zero(T)
10321034
i₁ = first(eachindex(x))

src/hessenberg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function dot(x::AbstractVector, H::UpperHessenberg, y::AbstractVector)
360360
m = size(H, 1)
361361
(length(x) == m == length(y)) || throw(DimensionMismatch())
362362
if iszero(m)
363-
return dot(zero(eltype(x)), zero(eltype(H)), zero(eltype(y)))
363+
return zero(dot(zero(eltype(x)), zero(eltype(H)), zero(eltype(y))))
364364
end
365365
x₁ = x[1]
366366
r = dot(x₁, H[1,1], y[1])

src/symmetric.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ function dot(x::AbstractVector, A::RealHermSymComplexHerm, y::AbstractVector)
719719
require_one_based_indexing(x, y)
720720
n = length(x)
721721
(n == length(y) == size(A, 1)) || throw(DimensionMismatch())
722+
iszero(n) && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
722723
data = A.data
723724
r = dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
724725
iszero(n) && return r

src/triangular.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ function dot(x::AbstractVector, A::UpperTriangular, y::AbstractVector)
849849
m = size(A, 1)
850850
(length(x) == m == length(y)) || throw(DimensionMismatch())
851851
if iszero(m)
852-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
852+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
853853
end
854854
x₁ = x[1]
855855
r = dot(x₁, A[1,1], y[1])
@@ -870,7 +870,7 @@ function dot(x::AbstractVector, A::UnitUpperTriangular, y::AbstractVector)
870870
m = size(A, 1)
871871
(length(x) == m == length(y)) || throw(DimensionMismatch())
872872
if iszero(m)
873-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
873+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
874874
end
875875
x₁ = first(x)
876876
r = dot(x₁, y[1])
@@ -892,7 +892,7 @@ function dot(x::AbstractVector, A::LowerTriangular, y::AbstractVector)
892892
m = size(A, 1)
893893
(length(x) == m == length(y)) || throw(DimensionMismatch())
894894
if iszero(m)
895-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
895+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
896896
end
897897
r = zero(typeof(dot(first(x), first(A), first(y))))
898898
@inbounds for j in axes(A, 2)
@@ -912,7 +912,7 @@ function dot(x::AbstractVector, A::UnitLowerTriangular, y::AbstractVector)
912912
m = size(A, 1)
913913
(length(x) == m == length(y)) || throw(DimensionMismatch())
914914
if iszero(m)
915-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
915+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
916916
end
917917
r = zero(typeof(dot(first(x), first(y))))
918918
@inbounds for j in axes(A, 2)

src/tridiag.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function dot(x::AbstractVector, S::SymTridiagonal, y::AbstractVector)
246246
nx, ny = length(x), length(y)
247247
(nx == size(S, 1) == ny) || throw(DimensionMismatch("dot"))
248248
if nx 1
249-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(S)), zero(eltype(y)))
249+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(S)), zero(eltype(y))))
250250
return dot(x[1], S.dv[1], y[1])
251251
end
252252
dv, ev = S.dv, S.ev
@@ -965,7 +965,7 @@ function dot(x::AbstractVector, A::Tridiagonal, y::AbstractVector)
965965
nx, ny = length(x), length(y)
966966
(nx == size(A, 1) == ny) || throw(DimensionMismatch())
967967
if nx 1
968-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
968+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
969969
return dot(x[1], A.d[1], y[1])
970970
end
971971
@inbounds begin

test/generic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,10 @@ end
733733
@test dot(x, B', y) dot(B*x, y)
734734
elty <: Real && @test dot(x, transpose(B), y) dot(x, transpose(B)*y)
735735
end
736+
for (m, n) in ((0, 0), (1, 0), (0, 1))
737+
v = zeros(ComplexF64, m); a = zeros(ComplexF64, m, n); w = zeros(Float64, n)
738+
@test dot(v, a, w) === zero(ComplexF64)
739+
end
736740
end
737741

738742
@testset "condskeel #34512" begin

0 commit comments

Comments
 (0)