Skip to content

Commit 65862c9

Browse files
Add Static Arrays Extension (#427)
This is a continuation of [this PR](https://github.com/JuliaArrays/FillArrays.jl/pull/422/files). This PR adds an extension of StaticArrays.jl. This ends up fixing an issue where adding or subtracting by `Zeros` always returns a dense `Array `. By adding this extension, we resolve ambiguity issues and thus are able to override `+(a::AbstractArray, b::Zeros)` so that it preserves the type of `a`.
1 parent 3e305a8 commit 65862c9

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "1.15.0"
3+
version = "1.16.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -9,11 +9,13 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
1010
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1111
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
12+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1213

1314
[extensions]
1415
FillArraysPDMatsExt = "PDMats"
1516
FillArraysSparseArraysExt = "SparseArrays"
1617
FillArraysStatisticsExt = "Statistics"
18+
FillArraysStaticArraysExt = "StaticArrays"
1719

1820
[compat]
1921
Aqua = "0.8"

ext/FillArraysStaticArraysExt.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module FillArraysStaticArraysExt
2+
3+
using FillArrays
4+
using StaticArrays
5+
6+
import Base: promote_op
7+
import FillArrays: elconvert
8+
9+
# Disambiguity methods for StaticArrays
10+
11+
function Base.:+(a::FillArrays.Zeros, b::StaticArray)
12+
promote_shape(a,b)
13+
return elconvert(promote_op(+,eltype(a),eltype(b)),b)
14+
end
15+
function Base.:+(a::StaticArray, b::FillArrays.Zeros)
16+
promote_shape(a,b)
17+
return elconvert(promote_op(+,eltype(a),eltype(b)),a)
18+
end
19+
function Base.:-(a::StaticArray, b::FillArrays.Zeros)
20+
promote_shape(a,b)
21+
return elconvert(promote_op(-,eltype(a),eltype(b)),a)
22+
end
23+
function Base.:-(a::FillArrays.Zeros, b::StaticArray)
24+
promote_shape(a,b)
25+
return elconvert(promote_op(-,eltype(a),eltype(b)),-b)
26+
end
27+
28+
end # module

src/fillalgebra.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,25 @@ function +(a::AbstractZeros{T}, b::AbstractZeros{V}) where {T, V} # for disambig
436436
promote_shape(a,b)
437437
return elconvert(promote_op(+,T,V),a)
438438
end
439-
# no AbstractArray. Otherwise incompatible with StaticArrays.jl
440-
# AbstractFill for disambiguity
441-
for TYPE in (:Array, :AbstractFill, :AbstractRange, :Diagonal)
439+
440+
function -(a::AbstractZeros{T}, b::AbstractZeros{V}) where {T, V} # for disambiguity
441+
promote_shape(a,b)
442+
return elconvert(promote_op(-,T,V),-b)
443+
end
444+
445+
# AbstractFill and Array for disambiguity
446+
for TYPE in (:Array, :AbstractFill, :AbstractRange, :AbstractArray)
442447
@eval function +(a::$TYPE{T}, b::AbstractZeros{V}) where {T, V}
443448
promote_shape(a,b)
444449
return elconvert(promote_op(+,T,V),a)
450+
end
451+
@eval function -(a::$TYPE{T}, b::AbstractZeros{V}) where {T, V}
452+
promote_shape(a,b)
453+
return elconvert(promote_op(-,T,V),a)
454+
end
455+
@eval function -(a::AbstractZeros{T}, b::$TYPE{V}) where {T, V}
456+
promote_shape(a,b)
457+
return elconvert(promote_op(-,T,V),-b)
445458
end
446459
@eval +(a::AbstractZeros, b::$TYPE) = b + a
447460
end

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,19 @@ end
505505
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
506506
test_addition_and_subtraction_dim_mismatch(A, Z)
507507
end
508+
509+
# Zeros should act as an additive identity
510+
# Arbitrary AbstractMatrix
511+
D = Diagonal([1, 1])
512+
Z = Zeros(2, 2)
513+
@test D + Z isa Diagonal
514+
@test D + Z == D
515+
@test D - Z == D
516+
@test Z - D == -D
517+
518+
@test Z + Z isa Zeros
519+
@test Z + Z == Z
520+
@test Z - Z == Z
508521
end
509522
end
510523

0 commit comments

Comments
 (0)