Skip to content

Commit

Permalink
fix subtracting Zeros from AbstractVector (#264)
Browse files Browse the repository at this point in the history
* fix subtracting Zeros from AbstractVector

* bump version to v1.1.1

* broadcasting tests
  • Loading branch information
jishnub authored Jun 4, 2023
1 parent c9d7d98 commit 80b1f5c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.1.0"
version = "1.1.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
2 changes: 1 addition & 1 deletion src/fillbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ for op in (:+, :-)
function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::ZerosVector{V}) where {T,V}
broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first."))
TT = promote_type(T,V)
broadcasted(TT$op, a)
broadcasted(TT, a)
end

broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFillVector{T}, b::ZerosVector) where T =
Expand Down
51 changes: 46 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,16 @@ equal_or_undef(a, b) = all(equal_or_undef.(a, b))
function test_addition_subtraction_dot(As, Bs, Tout::Type)
for A in As, B in Bs
@testset "$(typeof(A)) and $(typeof(B))" begin
@test A + B isa Tout{promote_type(eltype(A), eltype(B))}
@test @inferred(A + B) isa Tout{promote_type(eltype(A), eltype(B))}
@test equal_or_undef(as_array(A + B), as_array(A) + as_array(B))

@test A - B isa Tout{promote_type(eltype(A), eltype(B))}
@test @inferred(A - B) isa Tout{promote_type(eltype(A), eltype(B))}
@test equal_or_undef(as_array(A - B), as_array(A) - as_array(B))

@test B + A isa Tout{promote_type(eltype(B), eltype(A))}
@test @inferred(B + A) isa Tout{promote_type(eltype(B), eltype(A))}
@test equal_or_undef(as_array(B + A), as_array(B) + as_array(A))

@test B - A isa Tout{promote_type(eltype(B), eltype(A))}
@test @inferred(B - A) isa Tout{promote_type(eltype(B), eltype(A))}
@test equal_or_undef(as_array(B - A), as_array(B) - as_array(A))

# Julia 1.6 doesn't support dot(UniformScaling)
Expand Down Expand Up @@ -439,6 +439,21 @@ end
for A in As_special_nonsquare, B in Bs_us
test_addition_and_subtraction_dim_mismatch(A, B)
end

@testset "Zeros" begin
As = ([1,2], Float64[1,2], Int8[1,2], ComplexF16[2,4])
Zs = (TZ -> Zeros{TZ}(2)).((Int, Float64, Int8, ComplexF64))
test_addition_subtraction_dot(As, Zs, Vector)
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
test_addition_and_subtraction_dim_mismatch(A, Z)
end

As = (@SArray([1,2]), @SArray(Float64[1,2]), @SArray(Int8[1,2]), @SArray(ComplexF16[2,4]))
test_addition_subtraction_dot(As, Zs, SVector{2})
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
test_addition_and_subtraction_dim_mismatch(A, Z)
end
end
end

@testset "Other matrix types" begin
Expand Down Expand Up @@ -871,13 +886,39 @@ end
@test Zeros{Int}(5,6) ./ Zeros{Int}(5) Zeros{Int}(5) .\ Zeros{Int}(5,6) Fill(NaN,5,6)
end

@testset "Addition" begin
@testset "Addition/Subtraction" begin
@test Zeros{Int}(5) .+ (1:5) (1:5) .+ Zeros{Int}(5) (1:5) .- Zeros{Int}(5) 1:5
@test Zeros{Int}(1) .+ (1:5) (1:5) .+ Zeros{Int}(1) (1:5) .- Zeros{Int}(1) 1:5
@test Zeros(5) .+ (1:5) == (1:5) .+ Zeros(5) == (1:5) .- Zeros(5) == 1:5
@test Zeros{Int}(5) .+ Fill(1,5) Fill(1,5) .+ Zeros{Int}(5) Fill(1,5) .- Zeros{Int}(5) Fill(1,5)
@test_throws DimensionMismatch Zeros{Int}(2) .+ (1:5)
@test_throws DimensionMismatch (1:5) .+ Zeros{Int}(2)

for v in ([1:5;], SVector{5}(1:5), SVector{5,ComplexF16}(1:5))
a = Zeros{Int}(5) .+ v
b = v .+ Zeros{Int}(5)
c = v .- Zeros{Int}(5)
@test a == b == c == v
@test all(x -> x isa AbstractVector{promote_type(eltype(v), Int)}, (a,b,c))

a = Zeros{Int}(1) .+ v
b = v .+ Zeros{Int}(1)
c = v .- Zeros{Int}(1)
@test a == b == c == 1:5
@test all(x -> x isa AbstractVector{promote_type(eltype(v), Int)}, (a,b,c))

a = Zeros{Float64}(5) .+ v
b = v .+ Zeros{Float64}(5)
c = v .- Zeros{Float64}(5)
@test a == b == c == v
@test all(x -> x isa AbstractVector{promote_type(eltype(v), Float64)}, (a,b,c))

a = Zeros{Float64}(1) .+ v
b = v .+ Zeros{Float64}(1)
c = v .- Zeros{Float64}(1)
@test a == b == c == v
@test all(x -> x isa AbstractVector{promote_type(eltype(v), Float64)}, (a,b,c))
end
end
end

Expand Down

2 comments on commit 80b1f5c

@dlfivefifty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/84851

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.1 -m "<description of version>" 80b1f5ca742ebbbc834b40caabaa39364c64e5b7
git push origin v1.1.1

Please sign in to comment.