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

Avoid eltype degrading to Union{} for empty map/broadcast #664

Merged
merged 1 commit into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ scalar_getindex(x::Ref) = x[]
scalar_getindex(x::Tuple{<: Any}) = x[1]

@generated function _broadcast(f, ::Size{newsize}, s::Tuple{Vararg{Size}}, a...) where newsize
first_staticarray = 0
for i = 1:length(a)
if a[i] <: StaticArray
first_staticarray = a[i]
break
first_staticarray = a[findfirst(ai -> ai <: StaticArray, a)]

if prod(newsize) == 0
# Use inference to get eltype in empty case (see also comments in _map)
eltys = [:(eltype(a[$i])) for i ∈ 1:length(a)]
return quote
@_inline_meta
T = Core.Compiler.return_type(f, Tuple{$(eltys...)})
@inbounds return similar_type($first_staticarray, T, Size(newsize))()
end
end

Expand Down
22 changes: 18 additions & 4 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@ end
end

@generated function _map(f, a::AbstractArray...)
i = findfirst(ai -> ai <: StaticArray, a)
if i === nothing
first_staticarray = findfirst(ai -> ai <: StaticArray, a)
if first_staticarray === nothing
return :(throw(ArgumentError("No StaticArray found in argument list")))
end
# Passing the Size as an argument to _map leads to inference issues when
# recursively mapping over nested StaticArrays (see issue #593). Calling
# Size in the generator here is valid because a[i] is known to be a
# Size in the generator here is valid because a[first_staticarray] is known to be a
# StaticArray for which the default Size method is correct. If wrapped
# StaticArrays (with a custom Size method) are to be supported, this will
# no longer be valid.
S = Size(a[i])
S = Size(a[first_staticarray])

if prod(S) == 0
# In the empty case only, use inference to try figuring out a sensible
# eltype, as is done in Base.collect and broadcast.
# See https://github.com/JuliaArrays/StaticArrays.jl/issues/528
eltys = [:(eltype(a[$i])) for i ∈ 1:length(a)]
return quote
@_inline_meta
S = same_size(a...)
T = Core.Compiler.return_type(f, Tuple{$(eltys...)})
@inbounds return similar_type(a[$first_staticarray], T, S)()
end
end

exprs = Vector{Expr}(undef, prod(S))
for i ∈ 1:prod(S)
tmp = [:(a[$j][$i]) for j ∈ 1:length(a)]
Expand Down
10 changes: 7 additions & 3 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ end
@test @inferred(v2 .- v1) === SVector(0, 2)
@test @inferred(v1 .^ v2) === SVector(1, 16)
@test @inferred(v2 .^ v1) === SVector(1, 16)
# Issue #199: broadcast with empty SArray
@test @inferred(SVector(1) .+ SVector{0,Int}()) === SVector{0,Union{}}()
@test @inferred(SVector{0,Int}() .+ SVector(1)) === SVector{0,Union{}}()
# Issue #200: broadcast with Adjoint
@test @inferred(v1 .+ v2') === @SMatrix [2 5; 3 6]
@test @inferred(v1 .+ transpose(v2)) === @SMatrix [2 5; 3 6]
Expand Down Expand Up @@ -142,6 +139,13 @@ end
@test @inferred(zeros(SVector{0}) .+ zeros(SMatrix{0,2})) === zeros(SMatrix{0,2})
m = zeros(MMatrix{0,2})
@test @inferred(broadcast!(+, m, m, zeros(SVector{0}))) == zeros(SMatrix{0,2})
# Issue #199: broadcast with empty SArray
@test @inferred(SVector(1) .+ SVector{0,Int}()) === SVector{0,Int}()
@test @inferred(SVector{0,Int}() .+ SVector(1.0)) === SVector{0,Float64}()
# Issue #528
@test @inferred(isapprox(SMatrix{3,0,Float64}(), SMatrix{3,0,Float64}()))
@test @inferred(broadcast(length, SVector{0,String}())) === SVector{0,Int}()
@test @inferred(broadcast(join, SVector{0,String}(), SVector{0,String}(), SVector{0,String}())) === SVector{0,String}()
end

@testset "Mutating broadcast!" begin
Expand Down
6 changes: 5 additions & 1 deletion test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using StaticArrays, Test, LinearAlgebra

@testset "Linear algebra" begin

@testset "SVector as a (mathematical) vector space" begin
@testset "SArray as a (mathematical) vector space" begin
c = 2
v1 = @SVector [2,4,6,8]
v2 = @SVector [4,3,2,1]
Expand All @@ -14,6 +14,10 @@ using StaticArrays, Test, LinearAlgebra
@test @inferred(v1 + v2) === @SVector [6, 7, 8, 9]
@test @inferred(v1 - v2) === @SVector [-2, 1, 4, 7]

# #528 eltype with empty addition
zm = zeros(SMatrix{3, 0, Float64})
@test @inferred(zm + zm) === zm

# TODO Decide what to do about this stuff:
#v3 = [2,4,6,8]
#v4 = [4,3,2,1]
Expand Down
6 changes: 1 addition & 5 deletions test/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,5 @@ using StaticArrays, Test, LinearAlgebra

# decomposition is correct
l_u = l*u
if length(l_u) > 0 # Union{} element type breaks norm
@test l*u ≈ a[p,:]
else
@test_broken l*u ≈ a[p,:]
end
@test l*u ≈ a[p,:]
end
5 changes: 5 additions & 0 deletions test/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ using Statistics: mean
v3 = @SVector [1, 2, 3, 4]
map!(+, mv3, v1, v2, v3)
@test mv3 == @MVector [7, 9, 11, 13]

# Output eltype for empty cases #528
@test @inferred(map(/, SVector{0,Int}(), SVector{0,Int}())) === SVector{0,Float64}()
@test @inferred(map(+, SVector{0,Int}(), SVector{0,Float32}())) === SVector{0,Float32}()
@test @inferred(map(length, SVector{0,String}())) === SVector{0,Int}()
end

@testset "[map]reduce and [map]reducedim" begin
Expand Down