From dc8aa6e449773e9640979f4d4e4b3d95ee5e4eb9 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 22 Mar 2023 17:46:35 +0000 Subject: [PATCH] Fix #224 (#225) * fix #224 * bump version --- Project.toml | 2 +- src/fillalgebra.jl | 8 ++++++-- test/runtests.jl | 9 +++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 496208c0..1ca89b29 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FillArrays" uuid = "1a297f60-69ca-5386-bcde-b61e274b549b" -version = "0.13.9" +version = "0.13.10" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index fb70fadd..300e44d5 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -247,10 +247,14 @@ for TYPE in (:Array, :AbstractRange) -(a::AbstractFill, b::$TYPE) = a + (-b) end end -+(a::AbstractFill, b::AbstractFill) = fill_add(a, b) ++(a::AbstractFill, b::AbstractFill) = Fill(getindex_value(a) + getindex_value(b), promote_shape(a,b)) -(a::AbstractFill, b::AbstractFill) = a + (-b) -@inline function fill_add(a, b::AbstractFill) +@inline function fill_add(a::AbstractArray, b::AbstractFill) + promote_shape(a, b) + a .+ [getindex_value(b)] +end +@inline function fill_add(a::AbstractArray{<:Number}, b::AbstractFill) promote_shape(a, b) a .+ getindex_value(b) end diff --git a/test/runtests.jl b/test/runtests.jl index f4dcede8..9b2cfb19 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -308,7 +308,7 @@ as_array(x::AbstractArray) = Array(x) as_array(x::UniformScaling) = x function test_addition_and_subtraction(As, Bs, Tout::Type) for A in As, B in Bs - @testset "$A ± $B" begin + @testset "$(typeof(A)) ± $(typeof(B))" begin @test A + B isa Tout{promote_type(eltype(A), eltype(B))} @test as_array(A + B) == as_array(A) + as_array(B) @@ -326,7 +326,7 @@ end # Check that all permutations of + / - throw a `DimensionMismatch` exception. function test_addition_and_subtraction_dim_mismatch(a, b) - @testset "$a ± $b" begin + @testset "$(typeof(a)) ± $(typeof(b))" begin @test_throws DimensionMismatch a + b @test_throws DimensionMismatch a - b @test_throws DimensionMismatch b + a @@ -376,6 +376,11 @@ end A_svec, B_svec = SVector{5}(rand(5)), SVector(1, 2, 3, 4, 5) test_addition_and_subtraction((A_fill, B_fill, Zeros(5)), (A_svec, B_svec), SVector{5}) + # Issue #224 + A_matmat, B_matmat = Fill(rand(3,3),5), [rand(3,3) for n=1:5] + test_addition_and_subtraction((A_matmat,), (A_matmat,), Fill) + test_addition_and_subtraction((B_matmat,), (A_matmat,), Vector) + # Optimizations for Zeros and RectOrDiagonal{<:Any, <:AbstractFill} As_special_square = ( Zeros(3, 3), Zeros{Int}(4, 4),