Skip to content

Commit 11b787a

Browse files
authored
RFC: Support multidimensional reconstructions #28 from halleysfifthinc/extendreconstruct
2 parents 6b5118f + 8d0fb9b commit 11b787a

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/reconstruction.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ created from a timeseries `s` with `T` type numbers.
1212
1313
Use `Reconstruction(s::AbstractVector{T}, D, τ)` to create an instance.
1414
15+
Use `Reconstruction(s::SizedAray{S1, S2}, D, τ)` to create a reconstruction using
16+
a multi-dimensional timeseries. Note that a reconstruction created this way will
17+
have `S2*D` total dimensions and *not* `D`, as a result of each dimension of
18+
`s` having `D` delayed dimensions.
19+
1520
## Description
1621
The ``n``th row of a `Reconstruction` is the `D`-dimensional vector
1722
```math
@@ -48,6 +53,9 @@ end
4853
Reconstruction(s::AbstractVector{T}, D, τ) where {T} =
4954
Reconstruction{D, T, τ}(reconstruct(s, Val{D}(), τ))
5055

56+
Reconstruction(s::SizedArray{Tuple{S1, S2}, T, 2, M}, D, τ) where {S1, S2, T, M} =
57+
Reconstruction{S2*D, T, τ}(reconstruct(s, Val{D}(), τ))
58+
5159
@inline delay(::Reconstruction{D, T, t}) where {T,D,t} = t
5260

5361
function reconstruct_impl(::Type{Val{D}}) where D
@@ -66,10 +74,29 @@ function reconstruct_impl(::Type{Val{D}}) where D
6674
end
6775
end
6876

77+
function reconstructmat_impl(::Type{Val{S2}}, ::Type{Val{D}}) where {S2, D}
78+
gens = [:(s[i + $k*τ, $d]) for k=0:D-1 for d=1:S2]
79+
80+
quote
81+
L = size(s,1) - ($(D-1))*τ;
82+
T = eltype(s)
83+
data = Vector{SVector{$D*$S2, T}}(L)
84+
for i in 1:L
85+
data[i] = SVector{$D*$S2,T}($(gens...))
86+
end
87+
V = typeof(s)
88+
T = eltype(s)
89+
data
90+
end
91+
end
92+
6993
@generated function reconstruct(s::AbstractVector{T}, ::Val{D}, τ) where {D, T}
7094
reconstruct_impl(Val{D})
7195
end
7296

97+
@generated function reconstruct(s::SizedArray{Tuple{S1, S2}, T, 2, M}, ::Val{D}, τ) where {S1, S2, T, M, D}
98+
reconstructmat_impl(Val{S2}, Val{D})
99+
end
73100

74101
# Pretty print:
75102
matname(d::Reconstruction{D, T, τ}) where {D, T, τ} =

test/reconstruction_tests.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ end
55
using Base.Test, StaticArrays
66

77
@testset "Reconstruction" begin
8-
ds = Systems.towel()
9-
data = trajectory(ds, 10000)
10-
s = data[:, 1]; N = length(s)
11-
@testset "D = $(D), τ = $(τ)" for D in [2,3], τ in [2,3]
8+
ds = Systems.towel()
9+
data = trajectory(ds, 10000)
10+
s = data[:, 1]; N = length(s)
11+
@testset "D = $(D), τ = $(τ)" for D in [2,3], τ in [2,3]
1212

13-
R = Reconstruction(s, D, τ)
13+
R = Reconstruction(s, D, τ)
1414

15-
@test R[(1+τ):end, 1] == R[1:end-τ, 2]
16-
@test size(R) == (length(s) - τ*(D - 1), D)
17-
end
15+
@test R[(1+τ):end, 1] == R[1:end-τ, 2]
16+
@test size(R) == (length(s) - τ*(D - 1), D)
17+
end
18+
19+
@testset "D = $(D), τ = $(τ), basedim = $(basedim)" for D in [2,3], τ in [2,3], basedim in [2,3]
20+
21+
si = Matrix(data[:,1:basedim])
22+
s = Size(10001,basedim)(si)
23+
R = Reconstruction(s, D, τ)
24+
25+
for dim in 1:basedim
26+
@test R[(1+τ):end, dim] == R[1:end-τ, dim+basedim]
27+
end
28+
@test size(R) == (size(s,1) - τ*(D - 1), D*basedim)
29+
end
1830
end

0 commit comments

Comments
 (0)