Skip to content

Commit 21af0af

Browse files
authored
Fix 3-arg dot for empty arrays (#1485)
1 parent 6beb32c commit 21af0af

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
@@ -1292,7 +1292,7 @@ function dot(x::AbstractVector, B::Bidiagonal, y::AbstractVector)
12921292
nx, ny = length(x), length(y)
12931293
(nx == size(B, 1) == ny) || throw(DimensionMismatch())
12941294
if nx 1
1295-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(B)), zero(eltype(y)))
1295+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(B)), zero(eltype(y))))
12961296
return dot(x[1], B.dv[1], y[1])
12971297
end
12981298
ev, dv = B.ev, B.dv

src/generic.jl

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

10351035
function dot(x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
10361036
(axes(x)..., axes(y)...) == axes(A) || throw(DimensionMismatch())
1037+
# outermost zero call to avoid spurious sign ambiguity (like 0.0 - 0.0im)
1038+
any(isempty, (x, y)) && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
10371039
T = typeof(dot(first(x), first(A), first(y)))
10381040
s = zero(T)
10391041
i₁ = first(eachindex(x))

src/hessenberg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ function dot(x::AbstractVector, H::UpperHessenberg, y::AbstractVector)
404404
m = size(H, 1)
405405
(length(x) == m == length(y)) || throw(DimensionMismatch())
406406
if iszero(m)
407-
return dot(zero(eltype(x)), zero(eltype(H)), zero(eltype(y)))
407+
return zero(dot(zero(eltype(x)), zero(eltype(H)), zero(eltype(y))))
408408
end
409409
x₁ = x[1]
410410
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
@@ -774,6 +774,7 @@ function dot(x::AbstractVector, A::HermOrSym, y::AbstractVector)
774774
require_one_based_indexing(x, y)
775775
n = length(x)
776776
(n == length(y) == size(A, 1)) || throw(DimensionMismatch())
777+
iszero(n) && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
777778
data = A.data
778779
s = dot(first(x), first(A), first(y))
779780
r = zero(s+s)

src/triangular.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ function dot(x::AbstractVector, A::UpperTriangular, y::AbstractVector)
893893
m = size(A, 1)
894894
(length(x) == m == length(y)) || throw(DimensionMismatch())
895895
if iszero(m)
896-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
896+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
897897
end
898898
x₁ = x[1]
899899
r = dot(x₁, A[1,1], y[1])
@@ -914,7 +914,7 @@ function dot(x::AbstractVector, A::UnitUpperTriangular, y::AbstractVector)
914914
m = size(A, 1)
915915
(length(x) == m == length(y)) || throw(DimensionMismatch())
916916
if iszero(m)
917-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
917+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
918918
end
919919
x₁ = first(x)
920920
r = dot(x₁, y[1])
@@ -936,7 +936,7 @@ function dot(x::AbstractVector, A::LowerTriangular, y::AbstractVector)
936936
m = size(A, 1)
937937
(length(x) == m == length(y)) || throw(DimensionMismatch())
938938
if iszero(m)
939-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
939+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
940940
end
941941
r = zero(typeof(dot(first(x), first(A), first(y))))
942942
@inbounds for j in axes(A, 2)
@@ -956,7 +956,7 @@ function dot(x::AbstractVector, A::UnitLowerTriangular, y::AbstractVector)
956956
m = size(A, 1)
957957
(length(x) == m == length(y)) || throw(DimensionMismatch())
958958
if iszero(m)
959-
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
959+
return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
960960
end
961961
r = zero(typeof(dot(first(x), first(y))))
962962
@inbounds for j in axes(A, 2)

src/tridiag.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function dot(x::AbstractVector, S::SymTridiagonal, y::AbstractVector)
253253
nx, ny = length(x), length(y)
254254
(nx == size(S, 1) == ny) || throw(DimensionMismatch("dot"))
255255
if nx 1
256-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(S)), zero(eltype(y)))
256+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(S)), zero(eltype(y))))
257257
return dot(x[1], S.dv[1], y[1])
258258
end
259259
dv, ev = S.dv, S.ev
@@ -1022,7 +1022,7 @@ function dot(x::AbstractVector, A::Tridiagonal, y::AbstractVector)
10221022
nx, ny = length(x), length(y)
10231023
(nx == size(A, 1) == ny) || throw(DimensionMismatch())
10241024
if nx 1
1025-
nx == 0 && return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
1025+
nx == 0 && return zero(dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y))))
10261026
return dot(x[1], A.d[1], y[1])
10271027
end
10281028
@inbounds begin

test/generic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,10 @@ end
784784
@test dot(x, B', y) dot(B*x, y)
785785
elty <: Real && @test dot(x, transpose(B), y) dot(x, transpose(B)*y)
786786
end
787+
for (m, n) in ((0, 0), (1, 0), (0, 1))
788+
v = zeros(ComplexF64, m); a = zeros(ComplexF64, m, n); w = zeros(Float64, n)
789+
@test dot(v, a, w) === zero(ComplexF64)
790+
end
787791
end
788792

789793
@testset "condskeel #34512" begin

0 commit comments

Comments
 (0)