Skip to content

Commit ece81cf

Browse files
committed
Fix LinearIndices for Broadcasted
1 parent ae55dc2 commit ece81cf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

base/broadcast.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ Base.IndexStyle(bc::Broadcasted) = IndexStyle(typeof(bc))
223223
Base.IndexStyle(::Type{<:Broadcasted{<:Any,<:Tuple{Any}}}) = IndexLinear()
224224
Base.IndexStyle(::Type{<:Broadcasted{<:Any}}) = IndexCartesian()
225225

226-
Base.LinearIndices(bc::Broadcasted{<:Any,Tuple{Any}}) = axes(bc)[1]
226+
Base.LinearIndices(bc::Broadcasted{<:Any,<:Tuple{Any}}) = axes(bc)[1]
227227

228228
Base.ndims(::Broadcasted{<:Any,<:NTuple{N,Any}}) where {N} = N
229229
Base.ndims(::Type{<:Broadcasted{<:Any,<:NTuple{N,Any}}}) where {N} = N

test/broadcast.jl

+14
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,17 @@ let
814814
@test eltype(copy(bc)) == eltype([v for v in bc]) == eltype(collect(bc))
815815
@test ndims(copy(bc)) == ndims([v for v in bc]) == ndims(collect(bc)) == ndims(bc)
816816
end
817+
818+
@testset "broadcasted mapreduce" begin
819+
xs = 1:10
820+
ys = 1:2:20
821+
bc = Broadcast.instantiate(Broadcast.broadcasted(*, xs, ys))
822+
@test IndexStyle(bc) == IndexLinear()
823+
@test sum(bc) == mapreduce(Base.splat(*), +, zip(xs, ys))
824+
825+
xs2 = reshape(xs, 1, :)
826+
ys2 = reshape(ys, 1, :)
827+
bc = Broadcast.instantiate(Broadcast.broadcasted(*, xs2, ys2))
828+
@test IndexStyle(bc) == IndexCartesian()
829+
@test sum(bc) == mapreduce(Base.splat(*), +, zip(xs, ys))
830+
end

0 commit comments

Comments
 (0)