Skip to content

Commit d66df9a

Browse files
committed
speedup of convertions Dataset
1 parent 6b9a7db commit d66df9a

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v0.3.2
2+
* Orders of magnitude speed-up in conversions between `Matrix` and `Dataset`,
3+
because now methods use `transpose` internally and only `reinterpret`.
4+
15
# v0.3.1
26
* Added `jacobian` function
37
* Removed `EomVector` nonsense.

src/dataset.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ data[1] == data[1, :] # this is the first datapoint (D-dimensional)
6464
data[5, 3] # value of the third variable, at the 5th timepoint
6565
```
6666
67-
Use `Matrix(dataset)` to create a `Matrix`, and `Dataset(matrix)`
68-
to create a `Dataset` from a matrix. Notice: `Dataset(matrix)` assumes
69-
that each column of the matrix represents one dynamic variable. If instead each
70-
column of the matrix represents a datapoint, use `reinterpret(Dataset, matrix)`.
71-
72-
If you have various timeseries vectors `x, y, z, ...` pass them like
67+
Use `Matrix(dataset)` or `reinterpret(Matrix, dataset) to create a `Matrix`
68+
from a `dataset`, the first method returning a matrix where each column is a
69+
a timeseries while the second returning a matrix where each column is
70+
a datapoint. Similarly, use
71+
`Dataset(matrix)` or `reinterpret(Dataset, matrix)` to create a `Dataset` from
72+
a `matrix` that has structure as noted by the `Matrix` methods. Notice that the 2
73+
matrix versions are just the transpose of each other.
74+
75+
If you have various timeseries `Vector`s `x, y, z, ...` pass them like
7376
`Dataset(x, y, z, ...)`.
7477
7578
See also [`read_dataset`](@ref), [`write_dataset`](@ref) and [`minmaxima`](@ref).
@@ -113,22 +116,20 @@ end
113116

114117

115118
# Conversions:
116-
@inbounds function Base.convert(::Type{Matrix}, d::AbstractDataset{D,T}) where {D, T}
117-
mat = Matrix{T}(length(d), D)
118-
for i in 1:length(d)
119-
mat[i,:] .= d.data[i]
120-
end
121-
mat
119+
function Base.convert(::Type{Matrix}, d::AbstractDataset{D,T}) where {D, T}
120+
L = length(d)
121+
m = reinterpret(T, d.data, (D,L))
122+
transpose(m)
123+
end
124+
125+
function Base.reinterpret(::Type{M}, d::AbstractDataset{D,T}) where {M<:Matrix, D, T}
126+
L = length(d)
127+
reinterpret(T, d.data, (D,L))
122128
end
123129

124130
function Base.convert(::Type{Dataset}, mat::AbstractMatrix)
125-
D = size(mat, 2); T = eltype(mat)
126-
L = size(mat, 1)
127-
d = Vector{SVector{D, T}}(L)
128-
for i in 1:L
129-
d[i] = SVector{D, T}(view(mat, i, :))
130-
end
131-
return Dataset(d)
131+
m = transpose(mat)
132+
reinterpret(Dataset, m)
132133
end
133134

134135
function Base.reinterpret(::Type{Dataset}, mat::Array{T,2}) where {T<:Real}
@@ -142,8 +143,6 @@ function Base.convert(::Type{Dataset}, y::Vector{T}) where {T}
142143
return Dataset(data)
143144
end
144145

145-
146-
147146
### Pretty printing
148147
function matname(d::Dataset{D, T}) where {D, T}
149148
N = length(d)

src/discrete.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function Base.show(io::IO, ds::DiscreteDS{N, S, F, J}) where
254254
{N<:ANY, S<:ANY, F<:ANY, J<:ANY}
255255
text = "$(dimension(ds))-dimensional discrete system"
256256
print(io, text*"\n",
257-
" state: $(state(ds))\n", " eom: $F\n")
257+
" state: $(state(ds))\n", " e.o.m.: $F\n")
258258
end
259259

260260
@require Juno begin
@@ -274,7 +274,7 @@ function Base.show(io::IO, ds::BigDiscreteDS{T, F, J}) where
274274
{T, F<:ANY, J<:ANY}
275275
text = "$(dimension(ds))-dimensional Big discrete system"
276276
print(io, text*"\n",
277-
" state: $(state(ds))\n", " eom!: $F\n")
277+
" state: $(state(ds))\n", " e.o.m.: $F\n")
278278
end
279279

280280
@require Juno begin
@@ -291,7 +291,7 @@ end
291291
### 1-D
292292
function Base.show(io::IO, s::DiscreteDS1D{S, F, J}) where {S<:ANY, F<:ANY, J<:ANY}
293293
print(io, "1-dimensional discrete dynamical system:\n",
294-
"state: $(s.state)\n", "eom: $F\n")
294+
"state: $(s.state)\n", "e.o.m.: $F\n")
295295
end
296296
@require Juno begin
297297
function Juno.render(i::Juno.Inline, s::DiscreteDS1D{S, F, J}) where

todo.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)