Skip to content
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: julia
os:
- linux
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.6
julia 0.7-alpha
Requires
RecipesBase 0.1.0
StaticArrays
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
matrix:
Expand Down
4 changes: 2 additions & 2 deletions src/RecursiveArrayTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ __precompile__()

module RecursiveArrayTools

using Requires, RecipesBase, StaticArrays
using Requires, RecipesBase, StaticArrays, Statistics

abstract type AbstractVectorOfArray{T, N} <: AbstractArray{T, N} end
abstract type AbstractDiffEqArray{T, N} <: AbstractVectorOfArray{T, N} end
Expand All @@ -11,7 +11,7 @@ module RecursiveArrayTools
include("vector_of_array.jl")
include("array_partition.jl")
include("juno_rendering.jl")

export VectorOfArray, DiffEqArray, AbstractVectorOfArray, AbstractDiffEqArray,
vecarr_to_arr, vecarr_to_vectors, tuples

Expand Down
47 changes: 25 additions & 22 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ end

## constructors

ArrayPartition(x...) = ArrayPartition((x...))
ArrayPartition(x...) = ArrayPartition((x...,))

function ArrayPartition(x::S, ::Type{Val{copy_x}}=Val{false}) where {S<:Tuple,copy_x}
T = promote_type(eltype.(x)...)
Expand Down Expand Up @@ -124,7 +124,7 @@ end

Base.mapreduce(f,op,A::ArrayPartition) = mapreduce(f,op,(mapreduce(f,op,x) for x in A.x))
Base.any(f,A::ArrayPartition) = any(f,(any(f,x) for x in A.x))
function Base.copy!(dest::Array,A::ArrayPartition)
function Base.copyto!(dest::Array,A::ArrayPartition)
@assert length(dest) == length(A)
cur = 1
@inbounds for i in 1:length(A.x)
Expand All @@ -133,7 +133,7 @@ function Base.copy!(dest::Array,A::ArrayPartition)
end
end

function Base.copy!(A::ArrayPartition,src::ArrayPartition)
function Base.copyto!(A::ArrayPartition,src::ArrayPartition)
@assert length(src) == length(A)
cur = 1
@inbounds for i in 1:length(A.x)
Expand All @@ -145,12 +145,8 @@ end
## indexing

# Interface for the linear indexing. This is just a view of the underlying nested structure
@static if VERSION >= v"0.7-"
@inline Base.firstindex(A::ArrayPartition) = 1
@inline Base.lastindex(A::ArrayPartition) = length(A)
else
@inline Base.endof(A::ArrayPartition) = length(A)
end
@inline Base.firstindex(A::ArrayPartition) = 1
@inline Base.lastindex(A::ArrayPartition) = length(A)

@inline function Base.getindex(A::ArrayPartition, i::Int)
@boundscheck checkbounds(A, i)
Expand Down Expand Up @@ -220,9 +216,8 @@ recursive_eltype(A::ArrayPartition) = recursive_eltype(first(A.x))

## iteration

Base.start(A::ArrayPartition) = start(Chain(A.x))
Base.next(A::ArrayPartition,state) = next(Chain(A.x),state)
Base.done(A::ArrayPartition,state) = done(Chain(A.x),state)
Base.iterate(A::ArrayPartition) = iterate(Chain(A.x))
Base.iterate(A::ArrayPartition,state) = iterate(Chain(A.x),state)

Base.length(A::ArrayPartition) = sum((length(x) for x in A.x))
Base.size(A::ArrayPartition) = (length(A),)
Expand All @@ -238,14 +233,19 @@ Base.show(io::IO, m::MIME"text/plain", A::ArrayPartition) = show(io, m, A.x)

## broadcasting

Base.Broadcast._containertype(::Type{<:ArrayPartition}) = ArrayPartition
Base.Broadcast.promote_containertype(::Type{ArrayPartition}, ::Type) = ArrayPartition
Base.Broadcast.promote_containertype(::Type, ::Type{ArrayPartition}) = ArrayPartition
Base.Broadcast.promote_containertype(::Type{ArrayPartition}, ::Type{ArrayPartition}) = ArrayPartition
Base.Broadcast.promote_containertype(::Type{ArrayPartition}, ::Type{Array}) = ArrayPartition
Base.Broadcast.promote_containertype(::Type{Array}, ::Type{ArrayPartition}) = ArrayPartition
struct APStyle <: Broadcast.BroadcastStyle end
Base.BroadcastStyle(::Type{<:ArrayPartition}) = Broadcast.ArrayStyle{ArrayPartition}()
Base.BroadcastStyle(::Broadcast.ArrayStyle{ArrayPartition},::Broadcast.ArrayStyle) = Broadcast.Style{ArrayPartition}()
Base.BroadcastStyle(::Broadcast.ArrayStyle,::Broadcast.ArrayStyle{ArrayPartition}) = Broadcast.Style{ArrayPartition}()
Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{ArrayPartition}},::Type{ElType}) where ElType = similar(bc)

function Base.copy(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{ArrayPartition}})
ret = Broadcast.flatten(bc)
__broadcast(ret.f,ret.args...)
end

@generated function __broadcast(f,as...)

@generated function Base.Broadcast.broadcast_c(f, ::Type{ArrayPartition}, as...)
# common number of partitions
N = npartitions(as...)

Expand All @@ -254,12 +254,15 @@ Base.Broadcast.promote_containertype(::Type{Array}, ::Type{ArrayPartition}) = Ar
# index partitions
$((as[d] <: ArrayPartition ? :(as[$d].x[i]) : :(as[$d])
for d in 1:length(as))...)))

build_arraypartition(N, expr)
end

@generated function Base.Broadcast.broadcast_c!(f, ::Type{ArrayPartition}, ::Type,
dest::ArrayPartition, as...)
function Base.copyto!(dest::AbstractArray,bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{ArrayPartition}})
ret = Broadcast.flatten(bc)
__broadcast!(ret.f,dest,ret.args...)
end

@generated function __broadcast!(f, dest, as...)
# common number of partitions
N = npartitions(dest, as...)

Expand Down
34 changes: 14 additions & 20 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function recursivecopy!(b::AbstractArray{T,N},a::AbstractArray{T2,N}) where {T<:
end

function recursivecopy!(b::AbstractArray{T,N},a::AbstractArray{T2,N}) where {T<:Number,T2<:Number,N}
copy!(b,a)
copyto!(b,a)
end

function recursivecopy!(b::AbstractArray{T,N},a::AbstractArray{T2,N}) where {T<:AbstractArray,T2<:AbstractArray,N}
Expand Down Expand Up @@ -100,9 +100,9 @@ recursive_unitless_bottom_eltype(a) = recursive_unitless_bottom_eltype(eltype(a)
recursive_unitless_bottom_eltype(a::Type{T}) where {T<:Number} = typeof(one(eltype(a)))

Base.@pure recursive_unitless_eltype(a) = recursive_unitless_eltype(eltype(a))
Base.@pure recursive_unitless_eltype{T<:StaticArray}(a::Type{T}) = similar_type(a,recursive_unitless_eltype(eltype(a)))
Base.@pure recursive_unitless_eltype{T<:Array}(a::Type{T}) = Array{recursive_unitless_eltype(eltype(a)),ndims(a)}
Base.@pure recursive_unitless_eltype{T<:Number}(a::Type{T}) = typeof(one(eltype(a)))
Base.@pure recursive_unitless_eltype(a::Type{T}) where {T<:StaticArray} = similar_type(a,recursive_unitless_eltype(eltype(a)))
Base.@pure recursive_unitless_eltype(a::Type{T}) where {T<:Array} = Array{recursive_unitless_eltype(eltype(a)),ndims(a)}
Base.@pure recursive_unitless_eltype(a::Type{T}) where {T<:Number} = typeof(one(eltype(a)))

recursive_mean(x...) = mean(x...)
function recursive_mean(vecvec::Vector{T}) where T<:AbstractArray
Expand Down Expand Up @@ -145,30 +145,24 @@ Base.length(it::Chain) = sum(length, it.xss)

Base.eltype(::Type{Chain{T}}) where {T} = typejoin([eltype(t) for t in T.parameters]...)

function Base.start(it::Chain)
function Base.iterate(it::Chain)
i = 1
xs_state = nothing
while i <= length(it.xss)
xs_state = start(it.xss[i])
if !done(it.xss[i], xs_state)
break
end
xs_state = iterate(it.xss[i])
xs_state !== nothing && return xs_state[1], (i, xs_state[2])
i += 1
end
return i, xs_state
return nothing
end

function Base.next(it::Chain, state)
function Base.iterate(it::Chain, state)
i, xs_state = state
v, xs_state = next(it.xss[i], xs_state)
while done(it.xss[i], xs_state)
xs_state = iterate(it.xss[i], xs_state)
while xs_state == nothing
i += 1
if i > length(it.xss)
break
end
xs_state = start(it.xss[i])
i > length(it.xss) && return nothing
xs_state = iterate(it.xss[i])
end
return v, (i, xs_state)
return xs_state[1], (i, xs_state[2])
end

Base.done(it::Chain, state) = state[1] > length(it.xss)
12 changes: 5 additions & 7 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ DiffEqArray(vec::AbstractVector,ts::AbstractVector) = DiffEqArray(vec, ts, (size


# Interface for the linear indexing. This is just a view of the underlying nested structure
@static if VERSION >= v"0.7-"
@inline Base.firstindex(VA::AbstractVectorOfArray) = firstindex(VA.u)
@inline Base.lastindex(VA::AbstractVectorOfArray) = lastindex(VA.u)
else
@inline Base.endof(VA::AbstractVectorOfArray) = endof(VA.u)
end
@inline Base.firstindex(VA::AbstractVectorOfArray) = firstindex(VA.u)
@inline Base.lastindex(VA::AbstractVectorOfArray) = lastindex(VA.u)

@inline Base.length(VA::AbstractVectorOfArray) = length(VA.u)
@inline Base.eachindex(VA::AbstractVectorOfArray) = Base.OneTo(length(VA.u))
@inline Base.iteratorsize(VA::AbstractVectorOfArray) = Base.HasLength()
@inline Base.IteratorSize(VA::AbstractVectorOfArray) = Base.HasLength()
# Linear indexing will be over the container elements, not the individual elements
# unlike an true AbstractArray
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int) where {T, N} = VA.u[I]
Expand Down Expand Up @@ -83,6 +79,8 @@ end
# Need this for ODE_DEFAULT_UNSTABLE_CHECK from DiffEqBase to work properly
@inline Base.any(f, VA::AbstractVectorOfArray) = any(any(f,VA[i]) for i in eachindex(VA))
@inline Base.all(f, VA::AbstractVectorOfArray) = all(all(f,VA[i]) for i in eachindex(VA))
@inline Base.any(f::Function, VA::AbstractVectorOfArray) = any(any(f,VA[i]) for i in eachindex(VA))
@inline Base.all(f::Function, VA::AbstractVectorOfArray) = all(all(f,VA[i]) for i in eachindex(VA))

# conversion tools
@deprecate vecarr_to_arr(VA::AbstractVectorOfArray) convert(Array,VA)
Expand Down
10 changes: 5 additions & 5 deletions test/basic_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using RecursiveArrayTools

# Example Problem
recs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
testa = cat(2, recs...)
testa = cat(recs..., dims=2)
testva = VectorOfArray(recs)
t = [1,2,3]
diffeq = DiffEqArray(recs,t)
Expand All @@ -13,7 +13,7 @@ testa[1:2, 1:2] == [1 4; 2 5]

# # ndims == 2
recs = [rand(8) for i in 1:10]
testa = cat(2, recs...)
testa = cat(recs...,dims=2)
testva = VectorOfArray(recs)

# ## Linear indexing
Expand All @@ -36,7 +36,7 @@ testva = VectorOfArray(recs)

# # ndims == 3
recs = recs = [rand(10, 8) for i in 1:15]
testa = cat(3, recs...)
testa = cat(recs...,dims=3)
testva = VectorOfArray(recs)

# ## (Int, Int, Int)
Expand Down Expand Up @@ -65,11 +65,11 @@ testva = VectorOfArray(recs)

# ## Test ragged arrays work, or give errors as needed
#TODO: I am not really sure what the behavior of this is, what does Mathematica do?
recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
recs = [[1, 2, 3], [3, 5, 6, 7], [8, 9, 10, 11]]
testva = VectorOfArray(recs) #TODO: clearly this printed form is nonsense
@test testva[:, 1] == recs[1]
testva[1:2, 1:2]

# Test broadcast
a = testva .+ rand(3,3)
a.= testva
@test_broken a.= testva
10 changes: 5 additions & 5 deletions test/copy_static_array_test.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base.Test, RecursiveArrayTools, StaticArrays
using Test, RecursiveArrayTools, StaticArrays

struct ImmutableFV <: FieldVector{2,Float64}
a::Float64
Expand All @@ -13,7 +13,7 @@ end
# Immutable FieldVector
vec = ImmutableFV(1.,2.)
a = [vec]
b = zeros(a)
b = zero(a)
recursivecopy!(b, a)
@test a[1] == b[1]
copyat_or_push!(a, 2, b[1])
Expand All @@ -25,7 +25,7 @@ copyat_or_push!(a, 2, b[1])
# Mutable FieldVector
vec = MutableFV(1.,2.)
a = [vec]
b = zeros(a)
b = zero(a)
recursivecopy!(b, a)
@test a[1] == b[1]
a[1][1] *= 5
Expand All @@ -41,7 +41,7 @@ copyat_or_push!(a, 2, b[1])
# SArray
vec = @SArray [1., 2.]
a = [vec]
b = zeros(a)
b = zero(a)
recursivecopy!(b, a)
@test a[1] == b[1]
copyat_or_push!(a, 2, b[1])
Expand All @@ -53,7 +53,7 @@ copyat_or_push!(a, 2, b[1])
# MArray
vec = @MArray [1., 2.]
a = [vec]
b = zeros(a)
b = zero(a)
recursivecopy!(b, a)
a[1][1] *= 5
@test a[1] != b[1]
Expand Down
6 changes: 3 additions & 3 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RecursiveArrayTools, Base.Test
using RecursiveArrayTools, Test

recs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
testva = VectorOfArray(recs)
Expand Down Expand Up @@ -36,7 +36,7 @@ testva[4, 9] # == testva.data[9][4]
# convert array from VectorOfArray
recs = [rand(10, 7) for i = 1:8]
testva = VectorOfArray(recs)
testa = cat(3, recs...)
testa = cat(recs...,dims=3)
@test convert(Array,testva) == testa

recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
Expand All @@ -54,7 +54,7 @@ testva2 = similar(testva)
testval = 3.0
fill!(testva2, testval)
@test all(x->(x==testval), testva2)
testts = rand(size(testva.u))
testts = rand(Float64, size(testva.u))
testda = DiffEqArray(recursivecopy(testva.u), testts)
fill!(testda, testval)
@test all(x->(x==testval), testda)
Expand Down
18 changes: 9 additions & 9 deletions test/partitions_test.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RecursiveArrayTools, Base.Test
using RecursiveArrayTools, Test, Statistics
A = (rand(5),rand(5))
p = ArrayPartition(A)
@test (p.x[1][1],p.x[2][1]) == (p[1],p[6])
Expand All @@ -11,7 +11,7 @@ p2[1] = 1
@test p2.x[1] != p.x[1]

C = rand(10)
p3 = similar(p,indices(p))
p3 = similar(p,axes(p))
@test length(p3.x[1]) == length(p3.x[2]) == 5
@test length(p.x) == length(p2.x) == length(p3.x) == 2

Expand All @@ -21,20 +21,20 @@ B = (rand(5),rand(5))
p2 = ArrayPartition(B)
a = 5

p .= (*).(p,5)
p .= (*).(p,a)
p .= (*).(p,p2)
K = (*).(p,p2)
@. p = p*5
@. p = p*a
@. p = p*p2
K = p.*p2

p.*rand(5)
@test_broken p.*rand(10)
b = rand(10)
c = rand(10)
copy!(b,p)
copyto!(b,p)

@test b[1:5] == p.x[1]
@test b[6:10] == p.x[2]

copy!(p,c)
copyto!(p,c)
@test c[1:5] == p.x[1]
@test c[6:10] == p.x[2]

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using RecursiveArrayTools
using Base.Test
using Test

tic()
@time @testset "Utils Tests" begin include("utils_test.jl") end
Expand Down
Loading