Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CircShiftedVector
lag
lead
ShiftedArrays.circshift
ShiftedArrays.diff
```

## FFT shifts
Expand Down
17 changes: 16 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN)[-2:3]

## Shifting the data

Using the `ShiftedArray` type, this package provides two operations for lazily shifting vectors: `lag` and `lead`.
Using the `ShiftedArray` type, this package provides two operations for lazily shifting arrays: `lag` and `lead`. It also provides
the function `ShiftedArrays.diff` for calculating the differences
between elements in arrays.

```julia
julia> v = [1, 3, 5, 4];
Expand Down Expand Up @@ -127,6 +129,19 @@ julia> lead(v)
missing
```

`diff` is an analogue for `v .- lag(v)`

```julia
julia> v = [1, 3, 5, 4];

julia> ShiftedArrays.diff(v)
4-element Vector{Union{Missing, Int64}}:
missing
2
2
-1
```

## Shifting the data circularly

Julia Base provides a function `circshift` to shift the data circularly. However this function
Expand Down
1 change: 1 addition & 0 deletions src/ShiftedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base: checkbounds, getindex, setindex!, parent, size
export ShiftedArray, ShiftedVector, shifts, default
export CircShiftedArray, CircShiftedVector
export lag, lead
# ShiftedArrays.diff unexported due to collision
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not be exported - no need to comment I think


include("shiftedarray.jl")
include("circshiftedarray.jl")
Expand Down
46 changes: 46 additions & 0 deletions src/lag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,49 @@ julia> s = lead(v, (0, 2))
```
"""
lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); default = default)


"""
ShiftedArrays.diff(v::AbstractArray, n = 1; default = missing)

Return a freshly allocated array of the differences between elements
in the array. The second argument gives the amount to shift in each dimension.
If it is an integer, it is assumed to refer to the first dimension.
`default` specifies a default value when you are out of bounds.

## Examples

```jldoctest diff
julia> v = [1, 3, 5, 4];

julia> ShiftedArrays.diff(v)
4-element Vector{Union{Missing, Int64}}:
missing
2
2
-1

julia> w = 1:2:9
1:2:9

julia> s = ShiftedArrays.diff(w, 2)
5-element Vector{Union{Missing, Int64}}:
missing
missing
4
4
4

julia> v = reshape(1:16, 4, 4);

julia> s = ShiftedArrays.diff(v, (0, 2))
4×4 Matrix{Union{Missing, Int64}}:
missing missing 8 8
missing missing 8 8
missing missing 8 8
missing missing 8 8
"""
function diff(v::AbstractArray, n = 1; default = missing)
l = lag(v, n; default = default)
v .- l
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ end
diff = v .- lag(v)
@test isequal(diff, [missing, 2, 5, 4])

@test isequal(diff, ShiftedArrays.diff(v))

diff2 = v .- lag(v, 2)
@test isequal(diff2, [missing, missing, 7, 9])

@test isequal(diff2, ShiftedArrays.diff(v))

@test all(lag(v, 2, default = -100) .== coalesce.(lag(v, 2), -100))

diff = v .- lead(v)
Expand Down