From fd72d42f95d9bb703c87ad48f5bde23df37c2c10 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 6 Mar 2023 13:06:40 +0530 Subject: [PATCH 1/5] don't materialize when broadcasting Zeros with Vector --- src/fillbroadcast.jl | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/fillbroadcast.jl b/src/fillbroadcast.jl index 574fb145..35101b40 100644 --- a/src/fillbroadcast.jl +++ b/src/fillbroadcast.jl @@ -202,21 +202,28 @@ _copy_oftype(A::AbstractArray{T,N}, ::Type{S}) where {T,N,S} = convert(AbstractA _copy_oftype(A::AbstractRange{T}, ::Type{T}) where T = copy(A) _copy_oftype(A::AbstractRange{T}, ::Type{S}) where {T,S} = map(S, A) -for op in (:+, -) +for op in (:+, :-) @eval begin - function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::ZerosVector{V}) where {T,V} + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractRange{T}, b::ZerosVector{V}) where {T,V} broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) _copy_oftype(a, promote_type(T,V)) end + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::Zeros{V,1}) where {T,V} + broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) + TT = promote_type(T,V) + broadcasted(TT∘$op, a) + end broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFill{T,1}, b::ZerosVector) where T = Base.invoke(broadcasted, Tuple{DefaultArrayStyle, typeof($op), AbstractFill, AbstractFill}, DefaultArrayStyle{1}(), $op, a, b) end end -function broadcasted(::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector{T}, b::AbstractVector{V}) where {T,V} - broadcast_shape(axes(a), axes(b)) - _copy_oftype(b, promote_type(T,V)) +function broadcasted(S::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractRange) + broadcasted(S, +, b, a) +end +function broadcasted(S::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractVector) + broadcasted(S, +, b, a) end broadcasted(::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractFillVector) = From d3474ec0655d3c85355fc2c6bb8b0025e83c585d Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 17 Mar 2023 10:08:50 +0530 Subject: [PATCH 2/5] fix method overwrite --- src/fillbroadcast.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fillbroadcast.jl b/src/fillbroadcast.jl index 35101b40..8cd71c40 100644 --- a/src/fillbroadcast.jl +++ b/src/fillbroadcast.jl @@ -208,7 +208,7 @@ for op in (:+, :-) broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) _copy_oftype(a, promote_type(T,V)) end - function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::Zeros{V,1}) where {T,V} + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFill{T}, b::ZerosVector{V}) where {T,V} broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) TT = promote_type(T,V) broadcasted(TT∘$op, a) From ef6da4579d836eb859719c364da93cbc8b4bdfa9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 17 Mar 2023 11:04:16 +0530 Subject: [PATCH 3/5] fix type signature --- src/fillbroadcast.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fillbroadcast.jl b/src/fillbroadcast.jl index 8cd71c40..c934a5e7 100644 --- a/src/fillbroadcast.jl +++ b/src/fillbroadcast.jl @@ -208,13 +208,13 @@ for op in (:+, :-) broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) _copy_oftype(a, promote_type(T,V)) end - function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFill{T}, b::ZerosVector{V}) where {T,V} + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::ZerosVector{V}) where {T,V} broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) TT = promote_type(T,V) broadcasted(TT∘$op, a) end - broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFill{T,1}, b::ZerosVector) where T = + broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFillVector{T}, b::ZerosVector) where T = Base.invoke(broadcasted, Tuple{DefaultArrayStyle, typeof($op), AbstractFill, AbstractFill}, DefaultArrayStyle{1}(), $op, a, b) end end From de441431d2ad5a1f3ee5989f57ab0d385ad0d08e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 17 Mar 2023 11:19:16 +0530 Subject: [PATCH 4/5] version bump to v0.13.9 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1ce598e3..496208c0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FillArrays" uuid = "1a297f60-69ca-5386-bcde-b61e274b549b" -version = "0.13.8" +version = "0.13.9" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" From 7bfe07b322f23d5ce36ac3b1302ccc23548b2af0 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 14 Apr 2023 11:08:12 +0530 Subject: [PATCH 5/5] Add tests --- test/runtests.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 81ba61c5..e2024b56 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -869,6 +869,15 @@ end @test Zeros(10) .- Zeros(1,9) ≡ Zeros(10,9) @test Ones(10) .- Zeros(1,9) ≡ Ones(10,9) @test Ones(10) .- Ones(1,9) ≡ Zeros(10,9) + + end + + @testset "issue #208" begin + u = rand(2); v = Zeros(2) + @test Broadcast.broadcasted(-, u, v) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(+, u, v) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(-, v, u) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(+, v, u) isa Broadcast.Broadcasted end @testset "Zero .*" begin