Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[extensions]
FillArraysPDMatsExt = "PDMats"
FillArraysSparseArraysExt = "SparseArrays"
FillArraysStatisticsExt = "Statistics"
FillArraysStaticArraysExt = "StaticArrays"

[compat]
Aqua = "0.8"
Expand Down
28 changes: 28 additions & 0 deletions ext/FillArraysStaticArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module FillArraysStaticArraysExt

using FillArrays
using StaticArrays

import Base: promote_op
import FillArrays: elconvert

# Disambiguity methods for StaticArrays

function Base.:+(a::FillArrays.Zeros, b::StaticArray)
promote_shape(a,b)
return elconvert(promote_op(+,eltype(a),eltype(b)),b)
end
function Base.:+(a::StaticArray, b::FillArrays.Zeros)
promote_shape(a,b)
return elconvert(promote_op(+,eltype(a),eltype(b)),a)
end
function Base.:-(a::StaticArray, b::FillArrays.Zeros)
promote_shape(a,b)
return elconvert(promote_op(-,eltype(a),eltype(b)),a)
end
function Base.:-(a::FillArrays.Zeros, b::StaticArray)
promote_shape(a,b)
return elconvert(promote_op(-,eltype(a),eltype(b)),-b)
end

end # module
19 changes: 16 additions & 3 deletions src/fillalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,25 @@ function +(a::AbstractZeros{T}, b::AbstractZeros{V}) where {T, V} # for disambig
promote_shape(a,b)
return elconvert(promote_op(+,T,V),a)
end
# no AbstractArray. Otherwise incompatible with StaticArrays.jl
# AbstractFill for disambiguity
for TYPE in (:Array, :AbstractFill, :AbstractRange, :Diagonal)

function -(a::AbstractZeros{T}, b::AbstractZeros{V}) where {T, V} # for disambiguity
promote_shape(a,b)
return elconvert(promote_op(-,T,V),-b)
end

# AbstractFill and Array for disambiguity
for TYPE in (:Array, :AbstractFill, :AbstractRange, :AbstractArray)
@eval function +(a::$TYPE{T}, b::AbstractZeros{V}) where {T, V}
promote_shape(a,b)
return elconvert(promote_op(+,T,V),a)
end
@eval function -(a::$TYPE{T}, b::AbstractZeros{V}) where {T, V}
promote_shape(a,b)
return elconvert(promote_op(-,T,V),a)
end
@eval function -(a::AbstractZeros{T}, b::$TYPE{V}) where {T, V}
promote_shape(a,b)
return elconvert(promote_op(-,T,V),-b)
end
@eval +(a::AbstractZeros, b::$TYPE) = b + a
end
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ end
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
test_addition_and_subtraction_dim_mismatch(A, Z)
end

# Zeros should act as an additive identity
# Arbitrary AbstractMatrix
D = Diagonal([1, 1])
Z = Zeros(2, 2)
@test D + Z isa Diagonal
@test D + Z == D
@test D - Z == D
@test Z - D == -D

@test Z + Z isa Zeros
@test Z + Z == Z
@test Z - Z == Z
end
end

Expand Down
Loading