Skip to content

Commit 4ad663d

Browse files
add tests for and fix compatibility with StaticArrays.jl (#222)
* add tests for compatibility with StaticArrays.jl * fix test * fix compatibility Co-Authored-By: Christopher Rackauckas <[email protected]> * add Zeros x StaticArray compatibility * typo * bump version --------- Co-authored-by: Christopher Rackauckas <[email protected]>
1 parent a7c581a commit 4ad663d

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

Project.toml

Lines changed: 1 addition & 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 = "0.13.8"
3+
version = "0.13.9"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/fillalgebra.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ function +(a::Zeros{T}, b::Zeros{V}) where {T, V} # for disambiguity
216216
promote_shape(a,b)
217217
return elconvert(promote_op(+,T,V),a)
218218
end
219-
for TYPE in (:AbstractArray, :AbstractFill) # AbstractFill for disambiguity
219+
# no AbstractArray. Otherwise incompatible with StaticArrays.jl
220+
# AbstractFill for disambiguity
221+
for TYPE in (:Array, :AbstractFill, :AbstractRange, :Diagonal)
220222
@eval function +(a::$TYPE{T}, b::Zeros{V}) where {T, V}
221223
promote_shape(a,b)
222224
return elconvert(promote_op(+,T,V),a)
@@ -236,13 +238,17 @@ end
236238

237239
-(a::Ones, b::Ones) = Zeros(a) + Zeros(b)
238240

239-
# necessary for AbstractRange, Diagonal, etc
241+
# no AbstractArray. Otherwise incompatible with StaticArrays.jl
242+
for TYPE in (:Array, :AbstractRange)
243+
@eval begin
244+
+(a::$TYPE, b::AbstractFill) = fill_add(a, b)
245+
-(a::$TYPE, b::AbstractFill) = a + (-b)
246+
+(a::AbstractFill, b::$TYPE) = fill_add(b, a)
247+
-(a::AbstractFill, b::$TYPE) = a + (-b)
248+
end
249+
end
240250
+(a::AbstractFill, b::AbstractFill) = fill_add(a, b)
241-
+(a::AbstractFill, b::AbstractArray) = fill_add(b, a)
242-
+(a::AbstractArray, b::AbstractFill) = fill_add(a, b)
243251
-(a::AbstractFill, b::AbstractFill) = a + (-b)
244-
-(a::AbstractFill, b::AbstractArray) = a + (-b)
245-
-(a::AbstractArray, b::AbstractFill) = a + (-b)
246252

247253
@inline function fill_add(a, b::AbstractFill)
248254
promote_shape(a, b)

test/runtests.jl

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,26 +308,30 @@ as_array(x::AbstractArray) = Array(x)
308308
as_array(x::UniformScaling) = x
309309
function test_addition_and_subtraction(As, Bs, Tout::Type)
310310
for A in As, B in Bs
311-
@test A + B isa Tout{promote_type(eltype(A), eltype(B))}
312-
@test as_array(A + B) == as_array(A) + as_array(B)
311+
@testset "$A ± $B" begin
312+
@test A + B isa Tout{promote_type(eltype(A), eltype(B))}
313+
@test as_array(A + B) == as_array(A) + as_array(B)
313314

314-
@test A - B isa Tout{promote_type(eltype(A), eltype(B))}
315-
@test as_array(A - B) == as_array(A) - as_array(B)
315+
@test A - B isa Tout{promote_type(eltype(A), eltype(B))}
316+
@test as_array(A - B) == as_array(A) - as_array(B)
316317

317-
@test B + A isa Tout{promote_type(eltype(B), eltype(A))}
318-
@test as_array(B + A) == as_array(B) + as_array(A)
318+
@test B + A isa Tout{promote_type(eltype(B), eltype(A))}
319+
@test as_array(B + A) == as_array(B) + as_array(A)
319320

320-
@test B - A isa Tout{promote_type(eltype(B), eltype(A))}
321-
@test as_array(B - A) == as_array(B) - as_array(A)
321+
@test B - A isa Tout{promote_type(eltype(B), eltype(A))}
322+
@test as_array(B - A) == as_array(B) - as_array(A)
323+
end
322324
end
323325
end
324326

325327
# Check that all permutations of + / - throw a `DimensionMismatch` exception.
326328
function test_addition_and_subtraction_dim_mismatch(a, b)
327-
@test_throws DimensionMismatch a + b
328-
@test_throws DimensionMismatch a - b
329-
@test_throws DimensionMismatch b + a
330-
@test_throws DimensionMismatch b - a
329+
@testset "$a ± $b" begin
330+
@test_throws DimensionMismatch a + b
331+
@test_throws DimensionMismatch a - b
332+
@test_throws DimensionMismatch b + a
333+
@test_throws DimensionMismatch b - a
334+
end
331335
end
332336

333337
@testset "FillArray addition and subtraction" begin
@@ -368,6 +372,10 @@ end
368372
test_addition_and_subtraction_dim_mismatch(A, B)
369373
end
370374

375+
# FillArray + StaticArray should not have ambiguities
376+
A_svec, B_svec = SVector{5}(rand(5)), SVector(1, 2, 3, 4, 5)
377+
test_addition_and_subtraction((A_fill, B_fill, Zeros(5)), (A_svec, B_svec), SVector{5})
378+
371379
# Optimizations for Zeros and RectOrDiagonal{<:Any, <:AbstractFill}
372380
As_special_square = (
373381
Zeros(3, 3), Zeros{Int}(4, 4),

0 commit comments

Comments
 (0)