|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/piever/ShiftedArrays.jl)
|
4 | 4 | [](http://codecov.io/github/piever/ShiftedArrays.jl?branch=master)
|
| 5 | +[](https://piever.github.io/ShiftedArrays.jl/latest/) |
5 | 6 |
|
6 | 7 | Implementation of shifted arrays.
|
7 | 8 |
|
8 |
| -## Shifted Arrays |
9 |
| - |
10 |
| -A `ShiftedArray` is a lazy view of an Array, shifted on some or all of his indexing dimensions by some constant values. |
11 |
| - |
12 |
| -```julia |
13 |
| -julia> v = reshape(1:16, 4, 4) |
14 |
| -4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}: |
15 |
| - 1 5 9 13 |
16 |
| - 2 6 10 14 |
17 |
| - 3 7 11 15 |
18 |
| - 4 8 12 16 |
19 |
| - |
20 |
| - julia> s = ShiftedArray(v, (2, 0)) |
21 |
| - 4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: |
22 |
| - missing missing missing missing |
23 |
| - missing missing missing missing |
24 |
| - 1 5 9 13 |
25 |
| - 2 6 10 14 |
26 |
| -``` |
27 |
| - |
28 |
| -The parent Array as well as the amount of shifting can be recovered with `parent` and `shifts` respectively. |
29 |
| - |
30 |
| -```julia |
31 |
| -julia> parent(s) |
32 |
| -4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}: |
33 |
| - 1 5 9 13 |
34 |
| - 2 6 10 14 |
35 |
| - 3 7 11 15 |
36 |
| - 4 8 12 16 |
37 |
| - |
38 |
| -julia> shifts(s) |
39 |
| -(2, 0) |
40 |
| -``` |
41 |
| - |
42 |
| -`shifts` returns a `Tuple`, where the n-th element corresponds to the shift on the n-th dimension of the parent `Array`. |
43 |
| - |
44 |
| -Use `copy` to collect the shifted data into an `Array`: |
45 |
| - |
46 |
| -```julia |
47 |
| -julia> copy(s) |
48 |
| -4×4 Array{Union{Int64, Missing},2}: |
49 |
| - missing missing missing missing |
50 |
| - missing missing missing missing |
51 |
| - 1 5 9 13 |
52 |
| - 2 6 10 14 |
53 |
| -``` |
54 |
| - |
55 |
| -If you pass an integer, it will shift in the first dimension: |
56 |
| - |
57 |
| -```julia |
58 |
| -julia> ShiftedArray(v, 1) |
59 |
| -4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: |
60 |
| - missing missing missing missing |
61 |
| - 1 5 9 13 |
62 |
| - 2 6 10 14 |
63 |
| - 3 7 11 15 |
64 |
| -``` |
65 |
| - |
66 |
| -A custom default value (other than `missing`) can be provided with the `default` keyword: |
67 |
| - |
68 |
| -```julia |
69 |
| -julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN) |
70 |
| -3-element ShiftedArrays.ShiftedArray{Float64,Float64,1,Array{Float64,1}}: |
71 |
| - NaN |
72 |
| - 1.2 |
73 |
| - 3.1 |
74 |
| -``` |
75 |
| - |
76 |
| -### Out of bound indexes |
77 |
| - |
78 |
| -The bound check is performed only on the parent `Array`, not on the `ShiftedArray`, so for example: |
79 |
| - |
80 |
| -```julia |
81 |
| -julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN)[-2:3] |
82 |
| -6-element Array{Float64,1}: |
83 |
| - NaN |
84 |
| - NaN |
85 |
| - NaN |
86 |
| - NaN |
87 |
| - 1.2 |
88 |
| - 3.1 |
89 |
| -``` |
90 |
| - |
91 |
| -## Shifting the data |
92 |
| - |
93 |
| -Using the `ShiftedArray` type, this package provides two operations for lazily shifting vectors: `lag` and `lead`. |
94 |
| - |
95 |
| -```julia |
96 |
| -julia> v = [1, 3, 5, 4]; |
97 |
| - |
98 |
| -julia> lag(v) |
99 |
| -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: |
100 |
| - missing |
101 |
| - 1 |
102 |
| - 3 |
103 |
| - 5 |
104 |
| - |
105 |
| -julia> v .- lag(v) # compute difference from previous element without unnecessary allocations |
106 |
| -4-element Array{Any,1}: |
107 |
| - missing |
108 |
| - 2 |
109 |
| - 2 |
110 |
| - -1 |
111 |
| - |
112 |
| -julia> s = lag(v, 2) # shift by more than one element |
113 |
| -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: |
114 |
| - missing |
115 |
| - missing |
116 |
| - 1 |
117 |
| - 3 |
118 |
| -``` |
119 |
| - |
120 |
| -`lead` is the analogous of `lag` but shifts in the opposite direction: |
121 |
| - |
122 |
| -```julia |
123 |
| -julia> v = [1, 3, 5, 4]; |
124 |
| - |
125 |
| -julia> lead(v) |
126 |
| -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: |
127 |
| - 3 |
128 |
| - 5 |
129 |
| - 4 |
130 |
| - missing |
131 |
| -``` |
132 |
| - |
133 |
| -## Shifting the data circularly |
134 |
| - |
135 |
| -Julia Base provides a function `circshift` to shift the data circularly. However this function |
136 |
| -creates a copy of the vector, which may be unnecessary if the rotated vector is to be used only once. |
137 |
| -This package provides the `CircShiftedArray` type, which is a lazy view of an array |
138 |
| -shifted on some or all of his indexing dimensions by some constant values. |
139 |
| - |
140 |
| -Our implementation of `circshift` relies on them to avoid copying: |
141 |
| - |
142 |
| -```julia |
143 |
| -julia> w = reshape(1:16, 4, 4); |
144 |
| - |
145 |
| -julia> s = ShiftedArrays.circshift(w, (1, -1)) |
146 |
| -4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: |
147 |
| - 8 12 16 4 |
148 |
| - 5 9 13 1 |
149 |
| - 6 10 14 2 |
150 |
| - 7 11 15 3 |
151 |
| -``` |
152 |
| - |
153 |
| -As usual, you can `copy` the result to have a normal `Array`: |
154 |
| - |
155 |
| -```julia |
156 |
| -julia> copy(s) |
157 |
| -4×4 Array{Int64,2}: |
158 |
| - 8 12 16 4 |
159 |
| - 5 9 13 1 |
160 |
| - 6 10 14 2 |
161 |
| - 7 11 15 3 |
162 |
| -``` |
163 |
| - |
0 commit comments