Skip to content

Commit

Permalink
Merge pull request #409 from AayushSabharwal/as/immutable=zero
Browse files Browse the repository at this point in the history
fix: do not assume `AbstractVectorOfArray` is mutable in `Base.zero`
  • Loading branch information
ChrisRackauckas authored Nov 4, 2024
2 parents 600a9b5 + 439354c commit caa8d9f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ end

function Base.zero(VA::AbstractVectorOfArray)
val = copy(VA)
val.u = zero.(VA.u)
val.u .= zero.(VA.u)
return val
end

Expand Down
10 changes: 10 additions & 0 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,13 @@ end
f3!(z, zz)
@test z == VectorOfArray([fill(4, SVector{2, Float64}), fill(2, SVector{2, Float64})])
@test (@allocated f3!(z, zz)) == 0

struct ImmutableVectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N, A}
u::A # A <: AbstractArray{<: AbstractArray{T, N - 1}}
end

@testset "Base.zero does not assume mutable struct" begin
voa = ImmutableVectorOfArray{Float64, 2, Vector{Vector{Float64}}}([ones(3), 2ones(3)])
zvoa = zero(voa)
@test zvoa.u[1] == zvoa.u[2] == zeros(3)
end
2 changes: 1 addition & 1 deletion test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ mat = Array(va)
@test all(va'[i] == mat'[i] for i in eachindex(mat'))
@test Array(va') == mat'

@test !ArrayInterface.issingular(VectorOfArray([rand(2),rand(2)]))
@test !ArrayInterface.issingular(VectorOfArray([rand(2), rand(2)]))
1 change: 0 additions & 1 deletion test/partitions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ _scalar_op(y) = y + 1
_broadcast_wrapper(y) = _scalar_op.(y)
# Issue #8
@inferred _broadcast_wrapper(x)
@test_broken @inferred _broadcast_wrapper(y)

# Testing map
@test map(x -> x^2, x) == ArrayPartition(x.x[1] .^ 2, x.x[2] .^ 2)
Expand Down

0 comments on commit caa8d9f

Please sign in to comment.