Skip to content

Commit 0b08011

Browse files
author
Pietro Vertechi
authored
Remove dim (#11)
* remove kw * docs * docstring * clean tests * added news
1 parent 3240521 commit 0b08011

File tree

8 files changed

+93
-110
lines changed

8 files changed

+93
-110
lines changed

NEWS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## ShiftedArrays 0.3 release notes
2+
3+
### Breaking changes
4+
5+
- Removed the `dim` keyword: instead of e.g. `lag(v, 3, dim = 2)` write `lag(v, (0, 3))` (in agreement with `circshift` from Julia Base)
6+
- Changed direction of shifting for `ShiftedArray` and `CircShiftedArray` constructors. For example `CircShiftedArray(v, n)` with a positive `n` shifts to the right (in agreement with `circshift(v, n)` from Julia Base).
7+
8+
## New features
9+
10+
- `CircShiftedArray` type to shift arrays circularly.
11+
- A lazy version of `circshift`: `ShiftedArray.circshift`

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ julia> copy(s)
5353
2 6 10 14
5454
```
5555

56-
If you only need to shift in one dimension, you can use the commodity method:
56+
If you pass an integer, it will shift in the first dimension:
5757

5858
```julia
59-
ShiftedArray(v, n; dim = 1)
59+
julia> ShiftedArray(v, 1)
60+
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
61+
missing missing missing missing
62+
1 5 9 13
63+
2 6 10 14
64+
3 7 11 15
6065
```
6166

6267
## Shifting the data

deps/build.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ if !isfile(joinpath(@__DIR__, "already_showed"))
44
The shift index convention has changed to be in agreement with Julia's function
55
`circshift`. Now a positive shift shits to the right, i.e. `ShiftedVector(v, 1)[2] == v[2-1] == v[1]`.
66
Similarly `copy(CircShiftedVector(v, 1)) == circshift(v, 1)`.
7+
In agreement with the Base function `circshift`, the `dim` keyword argument is no longer
8+
supported. If the shift vector is too short (or if you input an integer) only the first
9+
few dimensions will be shifted.
710
For more details see https://github.com/piever/ShiftedArrays.jl
811
""")
912
touch("already_showed")

src/circshift.jl

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""
2-
circshift(v::AbstractArray, n::Int = 1; dim = 1)
2+
circshift(v::AbstractArray, n)
33
4-
Return a `ShiftedArray` object, with underlying data `v`, circularly shifted by `n` steps
5-
along dimension `dim`
4+
Return a `CircShiftedArray` object, with underlying data `v`. The second argument gives the amount
5+
to circularly shift in each dimension. If it is an integer, it is assumed to refer to the
6+
first dimension.
67
78
# Examples
89
910
```jldoctest circshift
1011
julia> v = [1, 3, 5, 4];
1112
12-
julia> ShiftedArrays.circshift(v)
13+
julia> ShiftedArrays.circshift(v, 1)
1314
4-element ShiftedArrays.CircShiftedArray{Int64,1,Array{Int64,1}}:
1415
4
1516
1
@@ -18,27 +19,6 @@ julia> ShiftedArrays.circshift(v)
1819
1920
julia> w = reshape(1:16, 4, 4);
2021
21-
julia> ShiftedArrays.circshift(w, -1, dim = 2)
22-
4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
23-
5 9 13 1
24-
6 10 14 2
25-
7 11 15 3
26-
8 12 16 4
27-
```
28-
"""
29-
circshift(v::AbstractArray, n::Int = 1; dim = 1) = CircShiftedArray(v, n; dim = dim)
30-
31-
"""
32-
circshift(v::AbstractArray{T, N}, n::NTuple{N, Int}) where {T, N}
33-
34-
Return a `ShiftedArray` object, with underlying data `v`, circularly shifted by `n` steps,
35-
where `n` is a `Tuple` denoting the shift in each dimension.
36-
37-
# Examples
38-
39-
```jldoctest circshift
40-
julia> w = reshape(1:16, 4, 4);
41-
4222
julia> ShiftedArrays.circshift(w, (1, -1))
4323
4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
4424
8 12 16 4
@@ -47,4 +27,4 @@ julia> ShiftedArrays.circshift(w, (1, -1))
4727
7 11 15 3
4828
```
4929
"""
50-
circshift(v::AbstractArray{T, N}, n::NTuple{N, Int}) where {T, N} = CircShiftedArray(v, n)
30+
circshift(v::AbstractArray, n) = CircShiftedArray(v, n)

src/circshiftedarray.jl

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,11 @@ struct CircShiftedArray{T, N, S<:AbstractArray} <: AbstractArray{T, N}
3030
shifts::NTuple{N, Int64}
3131
end
3232

33-
CircShiftedArray(v::AbstractArray{T, N}, n = Tuple(0 for i in 1:N)) where {T, N} = CircShiftedArray{T, N, typeof(v)}(v, n)
33+
CircShiftedArray(v::AbstractArray{T, N}, n::NTuple{N, Int} = Tuple(0 for i in 1:N)) where {T, N} =
34+
CircShiftedArray{T, N, typeof(v)}(v, n)
3435

35-
"""
36-
CircShiftedArray(parent::AbstractArray, n::Int; dim = 1)
37-
38-
Auxiliary method to create a `CircShiftedArray` shifted of `n` steps on dimension `dim`.
39-
40-
# Examples
41-
42-
```jldoctest circshiftedarray
43-
julia> v = reshape(1:16, 4, 4);
44-
45-
julia> s = CircShiftedArray(v, 2; dim = 1)
46-
4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
47-
3 7 11 15
48-
4 8 12 16
49-
1 5 9 13
50-
2 6 10 14
51-
52-
julia> shifts(s)
53-
(2, 0)
54-
```
55-
"""
56-
function CircShiftedArray(v::AbstractArray{T, N}, n::Int; dim = 1) where {T, N}
57-
tup = Tuple(i == dim ? n : 0 for i in 1:N)
58-
CircShiftedArray(v, tup)
59-
end
36+
CircShiftedArray(v::AbstractArray{T, N}, n) where {T, N} =
37+
CircShiftedArray(v, _padded_tuple(n, N))
6038

6139
"""
6240
CircShiftedVector{T, S<:AbstractArray}
@@ -65,9 +43,7 @@ Shorthand for `CircShiftedArray{T, 1, S}`.
6543
"""
6644
const CircShiftedVector{T, S<:AbstractArray} = CircShiftedArray{T, 1, S}
6745

68-
CircShiftedVector(v::AbstractVector{T}, n = (0,)) where {T} = CircShiftedArray(v, n)
69-
CircShiftedVector(v::AbstractVector{T}, n::Int; dim = 1) where {T} =
70-
CircShiftedArray(v, n::Int; dim = 1)
46+
CircShiftedVector(v::AbstractVector, n = (0,)) = CircShiftedArray(v, n)
7147

7248
Base.size(s::CircShiftedArray) = Base.size(parent(s))
7349

src/lag.jl

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
lag(v::AbstractArray, n = 1; dim = 1)
2+
lag(v::AbstractArray, n = 1)
33
4-
Return a `ShiftedArray` object, with underlying data `v`, shifted by `n` steps
5-
along dimension `dim`
4+
Return a `ShiftedArray` object, with underlying data `v`. The second argument gives the amount
5+
to shift in each dimension. If it is an integer, it is assumed to refer to the first dimension.
66
77
# Examples
88
@@ -34,14 +34,26 @@ julia> copy(s)
3434
1
3535
3
3636
5
37+
38+
julia> v = reshape(1:16, 4, 4);
39+
40+
julia> s = lag(v, (0, 2))
41+
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
42+
missing missing 1 5
43+
missing missing 2 6
44+
missing missing 3 7
45+
missing missing 4 8
3746
```
3847
"""
39-
lag(v::AbstractArray, n = 1; dim = 1) = ShiftedArray(v, n; dim = dim)
48+
lag(v::AbstractArray, n = 1) = ShiftedArray(v, n)
4049

4150
"""
42-
lead(v::AbstractArray, n = 1; dim = 1)
51+
lead(v::AbstractArray, n = 1)
52+
53+
Return a `ShiftedArray` object, with underlying data `v`. The second argument gives the amount
54+
to shift negatively in each dimension. If it is an integer, it is assumed to refer
55+
to the first dimension.
4356
44-
Return a `ShiftedArray` object, with underlying data `v`, shifted by `-n` steps.
4557
4658
# Examples
4759
@@ -73,6 +85,15 @@ julia> copy(s)
7385
9
7486
missing
7587
missing
88+
89+
julia> v = reshape(1:16, 4, 4);
90+
91+
julia> s = lag(v, (0, 2))
92+
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
93+
missing missing 1 5
94+
missing missing 2 6
95+
missing missing 3 7
96+
missing missing 4 8
7697
```
7798
"""
78-
lead(v::AbstractArray, n = 1; dim = 1) = ShiftedArray(v, -n; dim = dim)
99+
lead(v::AbstractArray, n = 1) = ShiftedArray(v, map(-, n))

src/shiftedarray.jl

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
_padded_tuple(n, N) = Tuple(i <= length(n) ? n[i] : 0 for i in 1:N)
2+
_padded_tuple(n::Int, N) = _padded_tuple((n,), N)
3+
14
"""
25
ShiftedArray(parent::AbstractArray, shifts)
36
@@ -34,51 +37,39 @@ julia> copy(s)
3437
1
3538
3
3639
5
37-
```
38-
"""
39-
struct ShiftedArray{T, N, S<:AbstractArray} <: AbstractArray{Union{T, Missing}, N}
40-
parent::S
41-
shifts::NTuple{N, Int64}
42-
end
43-
44-
ShiftedArray(v::AbstractArray{T, N}, n = Tuple(0 for i in 1:N)) where {T, N} = ShiftedArray{T, N, typeof(v)}(v, n)
45-
46-
"""
47-
ShiftedArray(parent::AbstractArray, n::Int; dim = 1)
4840
49-
Auxiliary method to create a `ShiftedArray` shifted of `n` steps on dimension `dim`.
50-
51-
# Examples
52-
53-
```jldoctest shiftedarray
5441
julia> v = reshape(1:16, 4, 4);
5542
56-
julia> s = ShiftedArray(v, 2; dim = 1)
43+
julia> s = ShiftedArray(v, (0, 2))
5744
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
58-
missing missing missing missing
59-
missing missing missing missing
60-
1 5 9 13
61-
2 6 10 14
45+
missing missing 1 5
46+
missing missing 2 6
47+
missing missing 3 7
48+
missing missing 4 8
6249
6350
julia> shifts(s)
64-
(2, 0)
51+
(0, 2)
6552
```
6653
"""
67-
function ShiftedArray(v::AbstractArray{T, N}, n::Int; dim = 1) where {T, N}
68-
tup = Tuple(i == dim ? n : 0 for i in 1:N)
69-
ShiftedArray(v, tup)
54+
struct ShiftedArray{T, N, S<:AbstractArray} <: AbstractArray{Union{T, Missing}, N}
55+
parent::S
56+
shifts::NTuple{N, Int64}
7057
end
7158

59+
ShiftedArray(v::AbstractArray{T, N}, n::NTuple{N, Int} = Tuple(0 for i in 1:N)) where {T, N} =
60+
ShiftedArray{T, N, typeof(v)}(v, n)
61+
62+
ShiftedArray(v::AbstractArray{T, N}, n) where {T, N} =
63+
ShiftedArray(v, _padded_tuple(n, N))
64+
7265
"""
7366
ShiftedVector{T, S<:AbstractArray}
7467
7568
Shorthand for `ShiftedArray{T, 1, S}`.
7669
"""
7770
const ShiftedVector{T, S<:AbstractArray} = ShiftedArray{T, 1, S}
7871

79-
ShiftedVector(v::AbstractVector{T}, n = (0,)) where {T} = ShiftedArray(v, n)
80-
ShiftedVector(v::AbstractVector{T}, n::Int; dim = 1) where {T} =
81-
ShiftedArray(v, n::Int; dim = 1)
72+
ShiftedVector(v::AbstractVector, n = (0,)) = ShiftedArray(v, n)
8273

8374
Base.size(s::ShiftedArray) = Base.size(parent(s))
8475

test/runtests.jl

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ using Compat.Test
44
@testset "ShiftedVector" begin
55
v = [1, 3, 5, 4]
66
sv = ShiftedVector(v, -1)
7-
@test isequal(sv, ShiftedVector(v, -1; dim = 1))
87
@test isequal(sv, ShiftedVector(v, (-1,)))
98
@test length(sv) == 4
10-
@test sv[2] == 5
9+
@test all(sv[1:3] .== [3, 5, 4])
1110
@test ismissing(sv[4])
1211
diff = v .- sv
13-
@test diff[1:3] == [-2, -2, 1]
14-
@test ismissing(diff[4])
12+
@test isequal(diff, [-2, -2, 1, missing])
1513
@test shifts(sv) == (-1,)
1614
end
1715

@@ -23,7 +21,7 @@ end
2321
@test ismissing(sv[3,3])
2422
@test shifts(sv) == (-2,0)
2523
@test isequal(sv, ShiftedArray(v, -2))
26-
@test isequal(ShiftedArray(v, (0, 2)), ShiftedArray(v, 2; dim = 2))
24+
@test isequal(ShiftedArray(v, (2,)), ShiftedArray(v, 2))
2725
s = ShiftedArray(v, (0, -2))
2826
@test isequal(collect(s), [ 9 13 missing missing;
2927
10 14 missing missing;
@@ -34,18 +32,19 @@ end
3432
@testset "CircShiftedVector" begin
3533
v = [1, 3, 5, 4]
3634
sv = CircShiftedVector(v, -1)
37-
@test isequal(sv, CircShiftedVector(v, -1; dim = 1))
3835
@test isequal(sv, CircShiftedVector(v, (-1,)))
3936
@test length(sv) == 4
40-
@test sv[2] == 5
41-
@test sv[4] == 1
37+
@test all(sv .== [3, 5, 4, 1])
4238
diff = v .- sv
4339
@test diff == [-2, -2, 1, 3]
4440
@test shifts(sv) == (-1,)
4541
sv2 = CircShiftedVector(v, 1)
4642
diff = v .- sv2
4743
@test copy(sv2) == [4, 1, 3, 5]
4844
@test all(CircShiftedVector(v, 1) .== circshift(v,1))
45+
sv[2] = 0
46+
@test collect(sv) == [3, 0, 4, 1]
47+
@test v == [1, 3, 0, 4]
4948
end
5049

5150
@testset "CircShiftedArray" begin
@@ -55,7 +54,7 @@ end
5554
@test sv[1, 3] == 11
5655
@test shifts(sv) == (-2,0)
5756
@test isequal(sv, CircShiftedArray(v, -2))
58-
@test isequal(CircShiftedArray(v, (0, 2)), CircShiftedArray(v, 2; dim = 2))
57+
@test isequal(CircShiftedArray(v, 2), CircShiftedArray(v, (2,)))
5958
s = CircShiftedArray(v, (0, 2))
6059
@test isequal(collect(s), [ 9 13 1 5;
6160
10 14 2 6;
@@ -66,26 +65,23 @@ end
6665
@testset "circshift" begin
6766
v = reshape(1:16, 4, 4)
6867
@test all(circshift(v, (1, -1)) .== ShiftedArrays.circshift(v, (1, -1)))
69-
@test all(circshift(v, (0, -1)) .== ShiftedArrays.circshift(v, -1, dim = 2))
68+
@test all(circshift(v, (1,)) .== ShiftedArrays.circshift(v, (1,)))
69+
@test all(circshift(v, 3) .== ShiftedArrays.circshift(v, 3))
7070
end
7171

7272
@testset "laglead" begin
7373
v = [1, 3, 8, 12]
7474
diff = v .- lag(v)
75-
@test diff[2:4] == [2, 5, 4]
76-
@test ismissing(diff[1])
75+
@test isequal(diff, [missing, 2, 5, 4])
7776

7877
diff2 = v .- lag(v, 2)
79-
@test diff2[3:4] == [7, 9]
80-
@test ismissing(diff2[1]) && ismissing(diff2[2])
78+
@test isequal(diff2, [missing, missing, 7, 9])
8179

8280
diff = v .- lead(v)
83-
@test diff[1:3] == [-2, -5, -4]
84-
@test ismissing(diff[4])
81+
@test isequal(diff, [-2, -5, -4, missing])
8582

8683
diff2 = v .- lead(v, 2)
87-
@test diff2[1:2] == [-7, -9]
88-
@test ismissing(diff2[3]) && ismissing(diff2[4])
84+
@test isequal(diff2, [-7, -9, missing, missing])
8985
end
9086

9187
@testset "reduce" begin

0 commit comments

Comments
 (0)