Skip to content

Commit 19b71ed

Browse files
expand AbstractVectorOfArray interface
1 parent d10bfba commit 19b71ed

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ arrays of arrays. The current functionality includes:
1313
#### VectorOfArray
1414

1515
```julia
16-
VectorOfArray(vec::AbstractVector)
16+
VectorOfArray(u::AbstractVector)
1717
```
1818

1919
A `VectorOfArray` is an array which has the underlying data structure `Vector{AbstractArray{T}}`
@@ -35,7 +35,20 @@ which act appropriate. Points to note are:
3535
- Iteration follows the linear index and goes over the vectors
3636

3737
Additionally, the `vecarr_to_arr(VA::AbstractVectorOfArray)` function is provided which transforms
38-
the `VectorOfArray` into a matrix/tensor.
38+
the `VectorOfArray` into a matrix/tensor. Also, `vecarr_to_vectors(VA::AbstractVectorOfArray)`
39+
returns a vector of the series for each component, that is `A[i,:]` for each `i`.
40+
A plot recipe is provided which plots the `A[i,:]` series.
41+
42+
#### DiffEqArray
43+
44+
Related to the `VectorOfArray` is the `DiffEqArray`
45+
46+
```julia
47+
DiffEqArray(u::AbstractVector,t::AbstractVector)
48+
```
49+
50+
This is a `VectorOfArray` which stores `A.t` which matches `A.u`. This will plot
51+
`(A.t[i],A[i,:])`. The function `tuples(diffeq_arr)` returns tuples of `(t,u)`.
3952

4053
#### ArrayPartition
4154

REQUIRE

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
julia 0.5
22
Iterators
33
Compat 0.17.0
4+
Juno
5+
RecipesBase 0.1.0

src/RecursiveArrayTools.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ __precompile__()
22

33
module RecursiveArrayTools
44

5-
using Iterators, Compat
5+
using Iterators, Compat, Juno, RecipesBase
66

77
@compat abstract type AbstractVectorOfArray{T, N} <: AbstractArray{T, N} end
88

99
include("utils.jl")
1010
include("vector_of_array.jl")
1111
include("array_partition.jl")
1212

13-
export VectorOfArray, AbstractVectorOfArray, vecarr_to_arr
13+
export VectorOfArray, DiffEqArray, AbstractVectorOfArray, vecarr_to_arr,
14+
vecarr_to_vectors, tuples
1415

15-
export recursivecopy!, vecvecapply, copyat_or_push!, vecvec_to_mat, recursive_one,
16-
recursive_mean, recursive_eltype
16+
export recursivecopy!, vecvecapply, copyat_or_push!, vecvec_to_mat,
17+
recursive_one, recursive_mean, recursive_eltype
1718

1819
export ArrayPartition
1920

src/vector_of_array.jl

+28
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
type VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N}
33
u::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
44
end
5+
# VectorOfArray with an added series for time
6+
type DiffEqArray{T, N, A, B} <: AbstractVectorOfArray{T, N}
7+
u::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
8+
t::B
9+
end
510

611
VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec)
712
# Assume that the first element is representative all all other elements
813
VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length(vec)))
914

15+
DiffEqArray{T, N}(vec::AbstractVector{T}, ts, dims::NTuple{N}) = DiffEqArray{eltype(T), N, typeof(vec), typeof(ts)}(vec, ts)
16+
# Assume that the first element is representative all all other elements
17+
DiffEqArray(vec::AbstractVector,ts::AbstractVector) = DiffEqArray(vec, ts, (size(vec[1])..., length(vec)))
18+
19+
1020
# Interface for the linear indexing. This is just a view of the underlying nested structure
1121
@inline Base.endof(VA::AbstractVectorOfArray) = endof(VA.u)
1222
@inline Base.length(VA::AbstractVectorOfArray) = length(VA.u)
@@ -16,6 +26,7 @@ VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length
1626
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::Int) = VA.u[I]
1727
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::Colon) = VA.u[I]
1828
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) = VA.u[I]
29+
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, i::Int,::Colon) = [VA.u[j][i] for j in 1:length(VA)]
1930

2031
# Interface for the two dimensional indexing, a more standard AbstractArray interface
2132
@inline Base.size(VA::AbstractVectorOfArray) = (size(VA.u[1])..., length(VA.u))
@@ -26,6 +37,7 @@ VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length
2637
Base.start{T, N}(VA::AbstractVectorOfArray{T, N}) = 1
2738
Base.next{T, N}(VA::AbstractVectorOfArray{T, N}, state) = (VA[state], state + 1)
2839
Base.done{T, N}(VA::AbstractVectorOfArray{T, N}, state) = state >= length(VA.u) + 1
40+
tuples(VA::DiffEqArray) = tuple.(VA.t,VA.u)
2941

3042
# Growing the array simply adds to the container vector
3143
Base.copy(VA::AbstractVectorOfArray) = typeof(VA)(copy(VA.u))
@@ -41,7 +53,23 @@ end
4153

4254
# conversion tools
4355
vecarr_to_arr(VA::AbstractVectorOfArray) = cat(length(size(VA)), VA.u...)
56+
vecarr_to_vectors(VA::AbstractVectorOfArray) = [VA[i,:] for i in eachindex(VA[1])]
4457

4558
# make it show just like its data
4659
Base.show(io::IO, x::AbstractVectorOfArray) = show(io, x.u)
4760
Base.show(io::IO, m::MIME"text/plain", x::AbstractVectorOfArray) = show(io, m, x.u)
61+
62+
# restore the type rendering in Juno
63+
Juno.@render Juno.Inline x::AbstractVectorOfArray begin
64+
fields = fieldnames(typeof(x))
65+
Juno.LazyTree(typeof(x), () -> [Juno.SubTree(Juno.Text("$f"), Juno.getfield′(x, f)) for f in fields])
66+
end
67+
68+
# plot recipes
69+
@recipe function f(VA::AbstractVectorOfArray)
70+
vecarr_to_vectors(VA)
71+
end
72+
@recipe function f(VA::DiffEqArray)
73+
A = vecarr_to_vectors(VA)
74+
VA.t,A
75+
end

0 commit comments

Comments
 (0)