Skip to content

Commit

Permalink
Fix broadcast with empty arrays
Browse files Browse the repository at this point in the history
Resolves #248
  • Loading branch information
c42f committed Jul 24, 2017
1 parent af478d6 commit 5c937dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/broadcast.jl
Expand Up @@ -76,7 +76,7 @@ end
newsize = tuple(newsize...)

exprs = Array{Expr}(newsize)
more = newsize[1] != 0
more = prod(newsize) > 0
current_ind = ones(Int, length(newsize))

while more
Expand Down Expand Up @@ -139,7 +139,7 @@ end

exprs = Array{Expr}(newsize)
j = 1
more = newsize[1] != 0
more = prod(newsize) > 0
current_ind = ones(Int, max(length(newsize), length.(sizes)...))
while more
exprs_vals = [(!(as[i] <: AbstractArray) ? :(as[$i]) : :(as[$i][$(broadcasted_index(sizes[i], current_ind))])) for i = 1:length(sizes)]
Expand Down
9 changes: 9 additions & 0 deletions test/broadcast.jl
Expand Up @@ -109,6 +109,15 @@ end
@test @inferred(2 .^ v) === SVector(2, 4)
end

@testset "Empty arrays" begin
@test @inferred(1.0 .+ zeros(SMatrix{2,0})) === zeros(SMatrix{2,0})
@test @inferred(1.0 .+ zeros(SMatrix{0,2})) === zeros(SMatrix{0,2})
@test @inferred(1.0 .+ zeros(SArray{Tuple{2,3,0}})) === zeros(SArray{Tuple{2,3,0}})
@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})
end

@testset "Mutating broadcast!" begin
# No setindex! error
A = eye(SMatrix{2, 2}); @test_throws ErrorException broadcast!(+, A, A, SVector(1, 4))
Expand Down

0 comments on commit 5c937dd

Please sign in to comment.