Skip to content

Commit 94ce5e2

Browse files
authored
Fix diff of nested static arrays (#801)
The eltype of these can just be inferred based on values from the element tuple (correct algebraic alternative in the type domain would be to use `zero(eltype(a))` rather than `one(eltype(a))`, but this wouldn't work for normal `Array`s inside static arrays.)
1 parent 34b84ca commit 94ce5e2

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/mapreduce.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ end
302302

303303
return quote
304304
@_inline_meta
305-
T = typeof(one(eltype(a)) - one(eltype(a)))
306-
@inbounds return similar_type(a, T, Size($Snew))(tuple($(exprs...)))
305+
elements = tuple($(exprs...))
306+
@inbounds return similar_type(a, eltype(elements), Size($Snew))(elements)
307307
end
308308
end
309309

test/mapreduce.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ using Statistics: mean
165165
@test diff(sv, dims=Val(1)) == diff(sv, dims=1) == diff(sv) == diff(v)
166166
@test diff(sm, dims=Val(1)) == diff(sm, dims=1) == diff(m, dims=1)
167167
@test diff(sm, dims=Val(2)) == diff(sm, dims=2) == diff(m, dims=2)
168+
169+
# diff on nested arrays should result in the correct eltype
170+
@test @inferred(diff(SA[SA[1,2],SA[3,4]])) === SVector{1,SVector{2,Int}}(((2,2),))
171+
@test @inferred(diff(SA[[1,2],[3,4]]))::SVector{1,Vector{Int}} == SA[[2,2]]
172+
# For larger cases, check eltype infers correctly
173+
@test @inferred(diff(zeros(SVector{100,SVector{2,Int}}))) == fill(SA[0,0],99)
168174
end
169175

170176
@testset "broadcast and broadcast!" begin

0 commit comments

Comments
 (0)