Skip to content

Add recursivefill! #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RecursiveArrayTools"
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
authors = ["Chris Rackauckas <[email protected]>"]
version = "2.25.2"
version = "2.26.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion src/RecursiveArrayTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Base.show(io::IO, x::Union{ArrayPartition,AbstractVectorOfArray}) = invoke(show,
export VectorOfArray, DiffEqArray, AbstractVectorOfArray, AbstractDiffEqArray,
AllObserved, vecarr_to_arr, vecarr_to_vectors, tuples

export recursivecopy, recursivecopy!, vecvecapply, copyat_or_push!,
export recursivecopy, recursivecopy!, recursivefill!, vecvecapply, copyat_or_push!,
vecvec_to_mat, recursive_one, recursive_mean, recursive_bottom_eltype,
recursive_unitless_bottom_eltype, recursive_unitless_eltype

Expand Down
21 changes: 21 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ function recursivecopy!(b::AbstractArray{T,N},a::AbstractArray{T2,N}) where {T<:
return b
end

function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:StaticArray,T2<:StaticArray,N}
@inbounds for i in eachindex(b)
b[i] = copy(a)
end
end

function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:Enum,T2<:Enum,N}
fill!(b,a)
end

function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:Number,T2<:Number,N}
fill!(b, a)
end

function recursivefill!(b::AbstractArray{T,N},a) where {T<:AbstractArray,N}
@inbounds for i in eachindex(b)
recursivefill!(b[i], a)
end
return b
end

function vecvec_to_mat(vecvec)
mat = Matrix{eltype(eltype(vecvec))}(undef, length(vecvec),length(vecvec[1]))
for i in 1:length(vecvec)
Expand Down
39 changes: 29 additions & 10 deletions test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ using RecursiveArrayTools, StaticArrays
using Test

t = collect(range(0, stop=10, length=200))
randomized = VectorOfArray([.01randn(2) for i in 1:10])
data = convert(Array,randomized)
randomized = VectorOfArray([0.01randn(2) for i in 1:10])
data = convert(Array, randomized)
@test typeof(data) <: Matrix{Float64}

## Test means
A = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
A = [[1 2; 3 4], [1 3; 4 6], [5 6; 7 8]]
@test recursive_mean(A) ≈ [2.33333333 3.666666666
4.6666666666 6.0]
4.6666666666 6.0]

A = zeros(5,5)
A = zeros(5, 5)
@test recursive_unitless_eltype(A) == Float64

using Unitful
A = zeros(5,5)*1u"kg"
A = zeros(5, 5) * 1u"kg"
@test recursive_unitless_eltype(A) == Float64
AA = [zeros(5,5) for i in 1:5]
AA = [zeros(5, 5) for i in 1:5]
@test recursive_unitless_eltype(AA) == Array{Float64,2}
AofA = [copy(A) for i in 1:5]
@test recursive_unitless_eltype(AofA) == Array{Float64,2}
AofSA = [@SVector [2.0,3.0] for i in 1:5]
AofSA = [@SVector [2.0, 3.0] for i in 1:5]
@test recursive_unitless_eltype(AofSA) == SVector{2,Float64}
AofuSA = [@SVector [2.0u"kg",3.0u"kg"] for i in 1:5]
AofuSA = [@SVector [2.0u"kg", 3.0u"kg"] for i in 1:5]
@test recursive_unitless_eltype(AofuSA) == SVector{2,Float64}

A = [ArrayPartition(ones(1),ones(1)),]
A = [ArrayPartition(ones(1), ones(1)),]

function test_recursive_bottom_eltype()
function test_value(val::Any, expected_type::Type)
Expand Down Expand Up @@ -70,3 +70,22 @@ test_recursive_bottom_eltype()
using RecursiveArrayTools: issymbollike
@test !issymbollike(1)
@test issymbollike(:a)

x = zeros(10)
recursivefill!(x, 1.0)
@test x == ones(10)

x = [zeros(10), zeros(10)]
recursivefill!(x, 1.0)
@test x[1] == ones(10)
@test x[2] == ones(10)

x = [SVector{10}(zeros(10)), SVector{10}(zeros(10))]
recursivefill!(x, SVector{10}(ones(10)))
@test x[1] == SVector{10}(ones(10))
@test x[2] == SVector{10}(ones(10))

x = [MVector{10}(zeros(10)), MVector{10}(zeros(10))]
recursivefill!(x, 1.0)
@test x[1] == MVector{10}(ones(10))
@test x[2] == MVector{10}(ones(10))