Skip to content

Commit

Permalink
Structured Broadcasting (#228)
Browse files Browse the repository at this point in the history
* Structured Broadcasting

* add jishnubs tests

* Update runtests.jl
  • Loading branch information
dlfivefifty authored Mar 28, 2023
1 parent 0bcd057 commit 10b4c00
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions src/fillbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 10b4c00

Please sign in to comment.