Skip to content

Commit

Permalink
cleaned up code and fixed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerHeintzmann committed May 17, 2023
1 parent b4dd2c9 commit 05a78f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 169 deletions.
27 changes: 10 additions & 17 deletions src/circshiftedarray.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
ShiftedArray(parent::AbstractArray, shifts)
CircShiftedArray(parent::AbstractArray, shifts)
Custom `AbstractArray` object to store an `AbstractArray` `parent` circularly shifted
by `shifts` steps (where `shifts` is a `Tuple` with one `shift` value per dimension of `parent`).
Expand All @@ -10,24 +10,24 @@ Use `copy` or `collect` to collect the values of a `ShiftedArray` into a normal
but instead a nonnegative number which leads to an equivalent shift.
!!! note
If `parent` is itself a `ShiftedArray`, the constructor does not nest
`ShiftedArray` objects but rather combines the shifts additively.
If `parent` is itself a `CircShiftedArray`, the constructor does not nest
`CircShiftedArray` objects but rather combines the shifts additively.
# Examples
```jldoctest shiftedarray
```jldoctest circshiftedarray
julia> v = [1, 3, 5, 4];
julia> s = ShiftedArray(v, (1,))
4-element ShiftedVector{Int64, Vector{Int64}, Tuple{1}, Missing}:
missing
julia> s = CircShiftedArray(v, (1,))
4-element ShiftedArray{Int64, 1, Vector{Int64}, CircShift}:
4
1
3
5
julia> copy(s)
4-element Vector{Union{Missing, Int64}}:
missing
4-element Vector{Int64}:
4
1
3
5
Expand All @@ -47,20 +47,17 @@ end
"""
CircShiftedVector{T, A<:AbstractArray}
Shorthand for `ShiftedArray{T, 1, A}`.
Shorthand for `CircShiftedArray{T, 1, A}`.
"""
const CircShiftedVector{T, A<:AbstractArray} = ShiftedVector{T, A, CircShift}

CircShiftedVector(v::AbstractVector, n = ()) = CircShiftedArray(v, n)
# CircShiftedVector(v::AbstractVector, s = ()) = ShiftedArray(v, s)
# CircShiftedVector(v::AbstractVector, s::Number) = ShiftedArray(v, (s,))

has_circ_type(a::CircShiftedArray) = true


# mod1 avoids first subtracting one and then adding one
@inline function Base.getindex(csa::CircShiftedArray{T,N,A}, i::Vararg{Int,N}) where {T,N,A}
# @show "gi circ"
getindex(csa.parent, (mod1(i[j]-shifts(csa)[j], size(csa.parent, j)) for j in 1:N)...)
end

Expand All @@ -70,8 +67,6 @@ end
end

@inline function Base.setindex!(csa::CircShiftedArray{T,N,A}, v, i::Vararg{Int,N}) where {T,N,A}
# @show "si circ"
#(setindex!(csa.parent, v, (mod1(i[j]-to_tuple(S)[j], size(csa.parent, j)) for j in 1:N)...); v)
setindex!(csa.parent, v, (mod1(i[j]-shifts(csa)[j], size(csa.parent, j)) for j in 1:N)...)
csa
end
Expand All @@ -86,5 +81,3 @@ function Base.reverse!(csa::CircShiftedArray; dims=:)
Base.circshift!(csa.parent, tmp, -2 .* shifts(csa))
return csa
end

# CircShiftedArray(v::AbstractArray, s::Number) = CircShiftedArray(v, map(mod, padded_tuple(v, s), size(v)))
12 changes: 6 additions & 6 deletions src/fftshift.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ that dimension.
```jldoctest
julia> ShiftedArrays.fftshift([1 0 0 0])
1×4 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{0, 2}}:
1×4 CircShiftedArray{Int64, 2, Matrix{Int64}}:
0 0 1 0
julia> ShiftedArrays.fftshift([1 0 0; 0 0 0; 0 0 0])
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{1, 1}}:
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}}:
0 0 0
0 1 0
0 0 0
julia> ShiftedArrays.fftshift([1 0 0; 0 0 0; 0 0 0], (1,))
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{1, 0}}:
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}}:
0 0 0
1 0 0
0 0 0
Expand All @@ -62,17 +62,17 @@ that dimension.
```jldoctest
julia> ShiftedArrays.ifftshift([0 0 1 0])
1×4 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{0, 2}}:
1×4 CircShiftedArray{Int64, 2, Matrix{Int64}}:
1 0 0 0
julia> ShiftedArrays.ifftshift([0 0 0; 0 1 0; 0 0 0])
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{2, 2}}:
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}}:
1 0 0
0 0 0
0 0 0
julia> ShiftedArrays.ifftshift([0 1 0; 0 0 0; 0 0 0], (2,))
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}, Tuple{0, 2}}:
3×3 CircShiftedArray{Int64, 2, Matrix{Int64}}:
1 0 0
0 0 0
0 0 0
Expand Down
12 changes: 6 additions & 6 deletions src/lag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ remaining dimensions is assumed to be `0`.
julia> v = [1, 3, 5, 4];
julia> ShiftedArrays.lag(v)
4-element ShiftedVector{Int64, Vector{Int64}, Tuple{1}, Missing}:
4-element ShiftedVector{Int64, Vector{Int64}, Missing}:
missing
1
3
Expand All @@ -23,7 +23,7 @@ julia> w = 1:2:9
1:2:9
julia> s = ShiftedArrays.lag(w, 2)
5-element ShiftedVector{Int64, StepRange{Int64, Int64}, Tuple{2}, Missing}:
5-element ShiftedVector{Int64, StepRange{Int64, Int64}, Missing}:
missing
missing
1
Expand All @@ -41,7 +41,7 @@ julia> copy(s)
julia> v = reshape(1:16, 4, 4);
julia> s = ShiftedArrays.lag(v, (0, 2))
4×4 ShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Tuple{0, 2}, Missing}:
4×4 ShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Missing}:
missing missing 1 5
missing missing 2 6
missing missing 3 7
Expand All @@ -67,7 +67,7 @@ remaining dimensions is assumed to be `0`.
julia> v = [1, 3, 5, 4];
julia> ShiftedArrays.lead(v)
4-element ShiftedVector{Int64, Vector{Int64}, Tuple{-1}, Missing}:
4-element ShiftedVector{Int64, Vector{Int64}, Missing}:
3
5
4
Expand All @@ -77,7 +77,7 @@ julia> w = 1:2:9
1:2:9
julia> s = ShiftedArrays.lead(w, 2)
5-element ShiftedVector{Int64, StepRange{Int64, Int64}, Tuple{-2}, Missing}:
5-element ShiftedVector{Int64, StepRange{Int64, Int64}, Missing}:
5
7
9
Expand All @@ -95,7 +95,7 @@ julia> copy(s)
julia> v = reshape(1:16, 4, 4);
julia> s = ShiftedArrays.lead(v, (0, 2))
4×4 ShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Tuple{0, -2}, Missing}:
4×4 ShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Missing}:
9 13 missing missing
10 14 missing missing
11 15 missing missing
Expand Down
Loading

0 comments on commit 05a78f4

Please sign in to comment.