From 10b4c008e21d683d733009496630a096b532eb06 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 28 Mar 2023 19:12:13 +0100 Subject: [PATCH] Structured Broadcasting (#228) * Structured Broadcasting * add jishnubs tests * Update runtests.jl --- Project.toml | 2 +- src/fillbroadcast.jl | 5 +++++ test/runtests.jl | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0d75506b..5ad72414 100644 --- a/Project.toml +++ b/Project.toml @@ -9,7 +9,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [compat] -Aqua = "0.5" +Aqua = "0.5, 0.6" julia = "1.6" [extras] diff --git a/src/fillbroadcast.jl b/src/fillbroadcast.jl index 574fb145..1cd24010 100644 --- a/src/fillbroadcast.jl +++ b/src/fillbroadcast.jl @@ -247,3 +247,8 @@ broadcasted(::DefaultArrayStyle{N}, ::typeof(Base.literal_pow), ::Base.RefValue{ broadcasted(::DefaultArrayStyle{N}, ::typeof(Base.literal_pow), ::Base.RefValue{typeof(^)}, r::Ones{T,N}, ::Base.RefValue{Val{k}}) where {T,N,k} = Ones{T}(axes(r)) broadcasted(::DefaultArrayStyle{N}, ::typeof(Base.literal_pow), ::Base.RefValue{typeof(^)}, r::Zeros{T,N}, ::Base.RefValue{Val{0}}) where {T,N} = Ones{T}(axes(r)) broadcasted(::DefaultArrayStyle{N}, ::typeof(Base.literal_pow), ::Base.RefValue{typeof(^)}, r::Zeros{T,N}, ::Base.RefValue{Val{k}}) where {T,N,k} = Zeros{T}(axes(r)) + +# supports structured broadcast +if isdefined(LinearAlgebra, :fzero) + LinearAlgebra.fzero(x::Zeros) = zero(eltype(x)) +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index b6ec7924..d8449543 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1471,3 +1471,19 @@ end @test cor(Fill(3, 4, 5)) ≈ cor(fill(3, 4, 5)) nans=true @test cor(Fill(3, 4, 5), dims=2) ≈ cor(fill(3, 4, 5), dims=2) nans=true end + +@testset "Structured broadcast" begin + D = Diagonal(1:5) + @test D + Zeros(5,5) isa Diagonal + @test D - Zeros(5,5) isa Diagonal + @test D .+ Zeros(5,5) isa Diagonal + @test D .- Zeros(5,5) isa Diagonal + @test D .* Zeros(5,5) isa Diagonal + @test Zeros(5,5) .* D isa Diagonal + @test Zeros(5,5) - D isa Diagonal + @test Zeros(5,5) + D isa Diagonal + @test Zeros(5,5) .- D isa Diagonal + @test Zeros(5,5) .+ D isa Diagonal + f = (x,y) -> x+1 + @test f.(D, Zeros(5,5)) isa Matrix +end \ No newline at end of file