Skip to content

Commit a38c1d5

Browse files
Merge pull request #195 from SciML/recursivefill
Add recursivefill!
2 parents 3aacba1 + 15df32b commit a38c1d5

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RecursiveArrayTools"
22
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "2.25.2"
4+
version = "2.26.0"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/RecursiveArrayTools.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Base.show(io::IO, x::Union{ArrayPartition,AbstractVectorOfArray}) = invoke(show,
2828
export VectorOfArray, DiffEqArray, AbstractVectorOfArray, AbstractDiffEqArray,
2929
AllObserved, vecarr_to_arr, vecarr_to_vectors, tuples
3030

31-
export recursivecopy, recursivecopy!, vecvecapply, copyat_or_push!,
31+
export recursivecopy, recursivecopy!, recursivefill!, vecvecapply, copyat_or_push!,
3232
vecvec_to_mat, recursive_one, recursive_mean, recursive_bottom_eltype,
3333
recursive_unitless_bottom_eltype, recursive_unitless_eltype
3434

src/utils.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ function recursivecopy!(b::AbstractArray{T,N},a::AbstractArray{T2,N}) where {T<:
4141
return b
4242
end
4343

44+
function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:StaticArray,T2<:StaticArray,N}
45+
@inbounds for i in eachindex(b)
46+
b[i] = copy(a)
47+
end
48+
end
49+
50+
function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:Enum,T2<:Enum,N}
51+
fill!(b,a)
52+
end
53+
54+
function recursivefill!(b::AbstractArray{T,N},a::T2) where {T<:Number,T2<:Number,N}
55+
fill!(b, a)
56+
end
57+
58+
function recursivefill!(b::AbstractArray{T,N},a) where {T<:AbstractArray,N}
59+
@inbounds for i in eachindex(b)
60+
recursivefill!(b[i], a)
61+
end
62+
return b
63+
end
64+
4465
function vecvec_to_mat(vecvec)
4566
mat = Matrix{eltype(eltype(vecvec))}(undef, length(vecvec),length(vecvec[1]))
4667
for i in 1:length(vecvec)

test/utils_test.jl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ using RecursiveArrayTools, StaticArrays
22
using Test
33

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

99
## Test means
10-
A = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
10+
A = [[1 2; 3 4], [1 3; 4 6], [5 6; 7 8]]
1111
@test recursive_mean(A) [2.33333333 3.666666666
12-
4.6666666666 6.0]
12+
4.6666666666 6.0]
1313

14-
A = zeros(5,5)
14+
A = zeros(5, 5)
1515
@test recursive_unitless_eltype(A) == Float64
1616

1717
using Unitful
18-
A = zeros(5,5)*1u"kg"
18+
A = zeros(5, 5) * 1u"kg"
1919
@test recursive_unitless_eltype(A) == Float64
20-
AA = [zeros(5,5) for i in 1:5]
20+
AA = [zeros(5, 5) for i in 1:5]
2121
@test recursive_unitless_eltype(AA) == Array{Float64,2}
2222
AofA = [copy(A) for i in 1:5]
2323
@test recursive_unitless_eltype(AofA) == Array{Float64,2}
24-
AofSA = [@SVector [2.0,3.0] for i in 1:5]
24+
AofSA = [@SVector [2.0, 3.0] for i in 1:5]
2525
@test recursive_unitless_eltype(AofSA) == SVector{2,Float64}
26-
AofuSA = [@SVector [2.0u"kg",3.0u"kg"] for i in 1:5]
26+
AofuSA = [@SVector [2.0u"kg", 3.0u"kg"] for i in 1:5]
2727
@test recursive_unitless_eltype(AofuSA) == SVector{2,Float64}
2828

29-
A = [ArrayPartition(ones(1),ones(1)),]
29+
A = [ArrayPartition(ones(1), ones(1)),]
3030

3131
function test_recursive_bottom_eltype()
3232
function test_value(val::Any, expected_type::Type)
@@ -70,3 +70,22 @@ test_recursive_bottom_eltype()
7070
using RecursiveArrayTools: issymbollike
7171
@test !issymbollike(1)
7272
@test issymbollike(:a)
73+
74+
x = zeros(10)
75+
recursivefill!(x, 1.0)
76+
@test x == ones(10)
77+
78+
x = [zeros(10), zeros(10)]
79+
recursivefill!(x, 1.0)
80+
@test x[1] == ones(10)
81+
@test x[2] == ones(10)
82+
83+
x = [SVector{10}(zeros(10)), SVector{10}(zeros(10))]
84+
recursivefill!(x, SVector{10}(ones(10)))
85+
@test x[1] == SVector{10}(ones(10))
86+
@test x[2] == SVector{10}(ones(10))
87+
88+
x = [MVector{10}(zeros(10)), MVector{10}(zeros(10))]
89+
recursivefill!(x, 1.0)
90+
@test x[1] == MVector{10}(ones(10))
91+
@test x[2] == MVector{10}(ones(10))

0 commit comments

Comments
 (0)