Skip to content

Commit f976781

Browse files
partitions
1 parent 0c3431d commit f976781

File tree

7 files changed

+194
-123
lines changed

7 files changed

+194
-123
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.5
2+
Iterators

src/RecursiveArrayTools.jl

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,15 @@ __precompile__()
22

33
module RecursiveArrayTools
44

5-
import Base: mean
6-
7-
function recursivecopy!{T<:Number,N}(b::Array{T,N},a::Array{T,N})
8-
@inbounds copy!(b,a)
9-
end
10-
11-
function recursivecopy!{T<:AbstractArray,N}(b::Array{T,N},a::Array{T,N})
12-
@inbounds for i in eachindex(a)
13-
recursivecopy!(b[i],a[i])
14-
end
15-
end
16-
17-
function vecvec_to_mat(vecvec)
18-
mat = Matrix{eltype(eltype(vecvec))}(length(vecvec),length(vecvec[1]))
19-
for i in 1:length(vecvec)
20-
mat[i,:] = vecvec[i]
21-
end
22-
mat
23-
end
24-
25-
26-
function vecvecapply(f,v)
27-
sol = Vector{eltype(eltype(v))}(0)
28-
for i in eachindex(v)
29-
for j in eachindex(v[i])
30-
push!(sol,v[i][j])
31-
end
32-
end
33-
f(sol)
34-
end
5+
using Iterators
356

36-
function vecvecapply{T<:Number}(f,v::Array{T})
37-
f(v)
38-
end
39-
40-
function vecvecapply{T<:Number}(f,v::T)
41-
f(v)
42-
end
43-
44-
@inline function copyat_or_push!{T,perform_copy}(a::AbstractVector{T},i::Int,x,nc::Type{Val{perform_copy}}=Val{true})
45-
@inbounds if length(a) >= i
46-
if T <: Number || !perform_copy
47-
a[i] = x
48-
else
49-
recursivecopy!(a[i],x)
50-
end
51-
else
52-
if eltype(x) <: Number && (typeof(x) <: Array || typeof(x) <: Number)
53-
# Have to check that it's <: Array or can have problems
54-
# with abstract arrays like MultiScaleModels.
55-
# Have to check <: Number since it could just be a number...
56-
if perform_copy
57-
push!(a,copy(x))
58-
else
59-
push!(a,x)
60-
end
61-
else
62-
if perform_copy
63-
push!(a,deepcopy(x))
64-
else
65-
push!(a,x)
66-
end
67-
end
68-
end
69-
nothing
70-
end
71-
72-
recursive_one(a) = recursive_one(a[1])
73-
recursive_one{T<:Number}(a::T) = one(a)
74-
75-
function mean{T<:AbstractArray}(vecvec::Vector{T})
76-
out = zeros(vecvec[1])
77-
for i in eachindex(vecvec)
78-
out+= vecvec[i]
79-
end
80-
out/length(vecvec)
81-
end
7+
import Base: mean
828

83-
function mean{T<:AbstractArray}(matarr::Matrix{T},region=0)
84-
if region == 0
85-
return mean(vec(matarr))
86-
elseif region == 1
87-
out = [zeros(matarr[1,i]) for i in 1:size(matarr,2)]
88-
for j in 1:size(matarr,2), i in 1:size(matarr,1)
89-
out[j] += matarr[i,j]
90-
end
91-
return out/size(matarr,1)
92-
elseif region == 2
93-
return mean(matarr',1)
94-
end
95-
end
9+
include("utils.jl")
10+
include("array_partition.jl")
9611

9712
export recursivecopy!, vecvecapply, copyat_or_push!, vecvec_to_mat, recursive_one,mean
9813

14+
export ArrayPartition
15+
9916
end # module

src/array_partition.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
immutable ArrayPartition{T}
2+
x::T
3+
end
4+
ArrayPartition(x...) = ArrayPartition((x...))
5+
function ArrayPartition{T}(x,::Type{Val{T}}=Val{false})
6+
if T
7+
return ArrayPartition(((copy(a) for a in x)...))
8+
else
9+
return ArrayPartition((x...))
10+
end
11+
end
12+
Base.similar(A::ArrayPartition) = ArrayPartition((similar(x) for x in A.x)...)
13+
Base.similar(A::ArrayPartition,dims::Tuple) = ArrayPartition((similar(x,dim) for (x,dim) in zip(A.x,dims))...)
14+
Base.copy(A::ArrayPartition) = Base.similar(A)
15+
Base.zeros(A::ArrayPartition) = ArrayPartition((zeros(x) for x in A.x)...)
16+
17+
Base.:*(A::Number, B::ArrayPartition) = ArrayPartition((A .* x for x in B.x)...)
18+
Base.:*(A::ArrayPartition, B::Number) = ArrayPartition((x .* B for x in A.x)...)
19+
Base.:/(A::ArrayPartition, B::Number) = ArrayPartition((x ./ B for x in A.x)...)
20+
Base.:\(A::Number, B::ArrayPartition) = ArrayPartition((x ./ A for x in B.x)...)
21+
22+
Base.getindex( A::ArrayPartition, i::Int) = ArrayPartition((x[i] for x in A.x)...)
23+
Base.setindex!(A::ArrayPartition, v, i::Int) = ArrayPartition((x[i]=v for x in A.x)...)
24+
Base.getindex( A::ArrayPartition, i::Int...) = ArrayPartition((x[i...] for x in A.x)...)
25+
Base.setindex!(A::ArrayPartition, v, i::Int...) = ArrayPartition((x[i...]=v for x in A.x)...)
26+
27+
function recursivecopy!(A::ArrayPartition,B::ArrayPartition)
28+
for (a,b) in zip(A.x,B.x)
29+
copy!(a,b)
30+
end
31+
end
32+
33+
recursive_one(A::ArrayPartition) = recursive_one(first(A.x))
34+
Base.zero(A::ArrayPartition) = zero(first(A.x))
35+
Base.first(A::ArrayPartition) = first(A.x)
36+
37+
Base.start(A::ArrayPartition) = chain(A.x...)
38+
Base.next(iter::ArrayPartition,state) = next(state,state)
39+
Base.done(iter::ArrayPartition,state) = done(state,state)
40+
41+
Base.length(A::ArrayPartition) = ((length(x) for x in A.x)...)
42+
Base.indices(A::ArrayPartition) = ((indices(x) for x in A.x)...)
43+
Base.eachindex(A::ArrayPartition) = ((indices(x) for x in A.x)...)

src/utils.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
function recursivecopy!{T<:Number,N}(b::Array{T,N},a::Array{T,N})
2+
@inbounds copy!(b,a)
3+
end
4+
5+
function recursivecopy!{T<:AbstractArray,N}(b::Array{T,N},a::Array{T,N})
6+
@inbounds for i in eachindex(a)
7+
recursivecopy!(b[i],a[i])
8+
end
9+
end
10+
11+
function vecvec_to_mat(vecvec)
12+
mat = Matrix{eltype(eltype(vecvec))}(length(vecvec),length(vecvec[1]))
13+
for i in 1:length(vecvec)
14+
mat[i,:] = vecvec[i]
15+
end
16+
mat
17+
end
18+
19+
20+
function vecvecapply(f,v)
21+
sol = Vector{eltype(eltype(v))}(0)
22+
for i in eachindex(v)
23+
for j in eachindex(v[i])
24+
push!(sol,v[i][j])
25+
end
26+
end
27+
f(sol)
28+
end
29+
30+
function vecvecapply{T<:Number}(f,v::Array{T})
31+
f(v)
32+
end
33+
34+
function vecvecapply{T<:Number}(f,v::T)
35+
f(v)
36+
end
37+
38+
@inline function copyat_or_push!{T,perform_copy}(a::AbstractVector{T},i::Int,x,nc::Type{Val{perform_copy}}=Val{true})
39+
@inbounds if length(a) >= i
40+
if T <: Number || !perform_copy
41+
a[i] = x
42+
else
43+
recursivecopy!(a[i],x)
44+
end
45+
else
46+
if eltype(x) <: Number && (typeof(x) <: Array || typeof(x) <: Number)
47+
# Have to check that it's <: Array or can have problems
48+
# with abstract arrays like MultiScaleModels.
49+
# Have to check <: Number since it could just be a number...
50+
if perform_copy
51+
push!(a,copy(x))
52+
else
53+
push!(a,x)
54+
end
55+
else
56+
if perform_copy
57+
push!(a,deepcopy(x))
58+
else
59+
push!(a,x)
60+
end
61+
end
62+
end
63+
nothing
64+
end
65+
66+
recursive_one(a) = recursive_one(a[1])
67+
recursive_one{T<:Number}(a::T) = one(a)
68+
69+
function mean{T<:AbstractArray}(vecvec::Vector{T})
70+
out = zeros(vecvec[1])
71+
for i in eachindex(vecvec)
72+
out+= vecvec[i]
73+
end
74+
out/length(vecvec)
75+
end
76+
77+
function mean{T<:AbstractArray}(matarr::Matrix{T},region=0)
78+
if region == 0
79+
return mean(vec(matarr))
80+
elseif region == 1
81+
out = [zeros(matarr[1,i]) for i in 1:size(matarr,2)]
82+
for j in 1:size(matarr,2), i in 1:size(matarr,1)
83+
out[j] += matarr[i,j]
84+
end
85+
return out/size(matarr,1)
86+
elseif region == 2
87+
return mean(matarr',1)
88+
end
89+
end

test/partitions_test.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using RecursiveArrayTools, Base.Test
2+
3+
A = (rand(5),rand(5))
4+
p = ArrayPartition(A)
5+
6+
@test (p[1].x[1],p[1].x[2]) == (p.x[1][1],p.x[2][1])
7+
p2 = similar(p)
8+
p2[1] = 1
9+
@test p2.x[1] != p.x[1]
10+
11+
C = rand(10)
12+
p3 = similar(p,indices(C))
13+
@test length(p3.x[1]) == length(p3.x[2]) == 10
14+
@test length(p.x) == length(p2.x) == length(p3.x) == 2

test/runtests.jl

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,7 @@ using OrdinaryDiffEq, ParameterizedFunctions,
22
DiffEqBase, RecursiveArrayTools
33
using Base.Test
44

5-
6-
# Here's the problem to solve
7-
8-
f = @ode_def_nohes LotkaVolterraTest begin
9-
dx = a*x - b*x*y
10-
dy = -c*y + d*x*y
11-
end a=>1.5 b=1.0 c=3.0 d=1.0
12-
13-
u0 = [1.0;1.0]
14-
tspan = (0.0,10.0)
15-
prob = ODEProblem(f,u0,tspan)
16-
sol = solve(prob,Tsit5()) # this uses most of the tools
17-
18-
t = collect(linspace(0,10,200))
19-
randomized = [(sol(t[i]) + .01randn(2)) for i in 1:length(t)]
20-
data = vecvec_to_mat(randomized)
21-
@test typeof(data) <: Matrix{Float64}
22-
23-
## Test means
24-
A = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
25-
@test mean(A) [2.33333333 3.666666666
26-
4.6666666666 6.0]
27-
B = Matrix{Matrix{Int64}}(2,3)
28-
B[1,:] = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
29-
B[2,:] = [[1 2; 3 4],[1 5;4 3],[5 8;2 1]]
30-
31-
ans = [[1 2; 3 4],[1 4; 4 4.5],[5 7; 4.5 4.5]]
32-
@test mean(B,1)[1] ans[1]
33-
@test mean(B,1)[2] ans[2]
34-
@test mean(B,1)[3] ans[3]
35-
36-
ans = [[2.333333333333 4.666666666666; 3.6666666666666 6.0], [2.3333333 3.0; 5.0 2.6666666]]
37-
@test mean(B,2)[1] ans[1]
38-
@test mean(B,2)[2] ans[2]
5+
tic()
6+
@time @testset "Utils Tests" begin include("utils_test.jl.jl") end
7+
@time @testset "Partitions Tests" begin include("partitions_test.jl.jl") end
8+
toc()

test/utils_test.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using OrdinaryDiffEq, ParameterizedFunctions,
2+
DiffEqBase, RecursiveArrayTools
3+
using Base.Test
4+
5+
# Here's the problem to solve
6+
7+
f = @ode_def_nohes LotkaVolterraTest begin
8+
dx = a*x - b*x*y
9+
dy = -c*y + d*x*y
10+
end a=>1.5 b=1.0 c=3.0 d=1.0
11+
12+
u0 = [1.0;1.0]
13+
tspan = (0.0,10.0)
14+
prob = ODEProblem(f,u0,tspan)
15+
sol = solve(prob,Tsit5()) # this uses most of the tools
16+
17+
t = collect(linspace(0,10,200))
18+
randomized = [(sol(t[i]) + .01randn(2)) for i in 1:length(t)]
19+
data = vecvec_to_mat(randomized)
20+
@test typeof(data) <: Matrix{Float64}
21+
22+
## Test means
23+
A = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
24+
@test mean(A) [2.33333333 3.666666666
25+
4.6666666666 6.0]
26+
B = Matrix{Matrix{Int64}}(2,3)
27+
B[1,:] = [[1 2; 3 4],[1 3;4 6],[5 6;7 8]]
28+
B[2,:] = [[1 2; 3 4],[1 5;4 3],[5 8;2 1]]
29+
30+
ans = [[1 2; 3 4],[1 4; 4 4.5],[5 7; 4.5 4.5]]
31+
@test mean(B,1)[1] ans[1]
32+
@test mean(B,1)[2] ans[2]
33+
@test mean(B,1)[3] ans[3]
34+
35+
ans = [[2.333333333333 4.666666666666; 3.6666666666666 6.0], [2.3333333 3.0; 5.0 2.6666666]]
36+
@test mean(B,2)[1] ans[1]
37+
@test mean(B,2)[2] ans[2]

0 commit comments

Comments
 (0)