From a0b3eabd7418135124858002170cd15d66b61e93 Mon Sep 17 00:00:00 2001 From: piever Date: Mon, 5 Sep 2022 18:15:19 +0200 Subject: [PATCH 1/4] unexport lag and lead --- NEWS.md | 24 ++++++++++++++++-------- docs/src/api.md | 4 ++-- src/ShiftedArrays.jl | 1 - test/runtests.jl | 16 ++++++++-------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index d653b72..1fc241d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,22 +1,30 @@ +## ShiftedArrays 2.0.0 release notes + +### Breaking changes + +- Indexing out of bounds now gives a `BoundsError` (instead of returning the default value for `ShiftedArray` or the circularly shifted value for `CircShiftedArray`). +- `lag` and `lead` are no longer exported (but still public API). +- Calling `ShiftedArray(v::ShiftedArray, n)` or `CircShiftedArray(v::CircShiftedArray, n)` does not nest but rather combines the shifts additively. + ## ShiftedArrays 1.0.0 release notes ### Breaking changes -- Removed special `reduce, reduce_vec, mapreduce, mapreduce_vec` methods -- Removed `to_array, to_offsetarray` methods +- Removed special `reduce, reduce_vec, mapreduce, mapreduce_vec` methods. +- Removed `to_array, to_offsetarray` methods. ## ShiftedArrays 0.5.1 release notes ### New features -- Support for OffsetArrays v0.11 -- Support for RecursiveArrayTools v1 +- Support for OffsetArrays v0.11. +- Support for RecursiveArrayTools v1. ## ShiftedArrays 0.5.0 release notes ### New features -- Support for Julia 1 +- Support for Julia 1. ## ShiftedArrays 0.4 release notes @@ -29,8 +37,8 @@ ### New features -- Allow custom default value with `default` keyword -- Allow filtering in reduce-like functions with `filter` keyword +- Allow custom default value with `default` keyword. +- Allow filtering in reduce-like functions with `filter` keyword. ## ShiftedArrays 0.3 release notes @@ -42,4 +50,4 @@ ### New features - `CircShiftedArray` type to shift arrays circularly. -- A lazy version of `circshift`: `ShiftedArray.circshift` +- A lazy version of `circshift`: `ShiftedArray.circshift`. diff --git a/docs/src/api.md b/docs/src/api.md index 0ce5e97..d2bd672 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -12,8 +12,8 @@ CircShiftedVector ## Shifting operations ```@docs -lag -lead +ShiftedArrays.lag +ShiftedArrays.lead ShiftedArrays.circshift ``` diff --git a/src/ShiftedArrays.jl b/src/ShiftedArrays.jl index fe1c765..20b6f82 100644 --- a/src/ShiftedArrays.jl +++ b/src/ShiftedArrays.jl @@ -3,7 +3,6 @@ module ShiftedArrays import Base: checkbounds, getindex, setindex!, parent, size, axes export ShiftedArray, ShiftedVector, shifts, default export CircShiftedArray, CircShiftedVector -export lag, lead include("shiftedarray.jl") include("circshiftedarray.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 78288e4..4415ed5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -163,22 +163,22 @@ end @testset "laglead" begin v = [1, 3, 8, 12] - diff = v .- lag(v) + diff = v .- ShiftedArrays.lag(v) @test isequal(diff, [missing, 2, 5, 4]) - diff2 = v .- lag(v, 2) + diff2 = v .- ShiftedArrays.lag(v, 2) @test isequal(diff2, [missing, missing, 7, 9]) - @test all(lag(v, 2, default = -100) .== coalesce.(lag(v, 2), -100)) + @test all(ShiftedArrays.lag(v, 2, default = -100) .== coalesce.(ShiftedArrays.lag(v, 2), -100)) - diff = v .- lead(v) + diff = v .- ShiftedArrays.lead(v) @test isequal(diff, [-2, -5, -4, missing]) - diff2 = v .- lead(v, 2) + diff2 = v .- ShiftedArrays.lead(v, 2) @test isequal(diff2, [-7, -9, missing, missing]) - @test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100)) + @test all(ShiftedArrays.lead(v, 2, default = -100) .== coalesce.(ShiftedArrays.lead(v, 2), -100)) - @test lag(lag(v, 1), 2) === lag(v, 3) - @test lead(lead(v, 1), 2) === lead(v, 3) + @test ShiftedArrays.lag(ShiftedArrays.lag(v, 1), 2) === ShiftedArrays.lag(v, 3) + @test ShiftedArrays.lead(ShiftedArrays.lead(v, 1), 2) === ShiftedArrays.lead(v, 3) end From 681db7fa8073047fff57e34ed5a49b440fc7690f Mon Sep 17 00:00:00 2001 From: piever Date: Mon, 5 Sep 2022 18:17:25 +0200 Subject: [PATCH 2/4] updated doctests --- src/lag.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 899f384..6dde9f3 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -12,7 +12,7 @@ remaining dimensions is assumed to be `0`. ```jldoctest lag julia> v = [1, 3, 5, 4]; -julia> lag(v) +julia> ShiftedArrays.lag(v) 4-element ShiftedVector{Int64, Missing, Vector{Int64}}: missing 1 @@ -22,7 +22,7 @@ julia> lag(v) julia> w = 1:2:9 1:2:9 -julia> s = lag(w, 2) +julia> s = ShiftedArrays.lag(w, 2) 5-element ShiftedVector{Int64, Missing, StepRange{Int64, Int64}}: missing missing @@ -40,7 +40,7 @@ julia> copy(s) julia> v = reshape(1:16, 4, 4); -julia> s = lag(v, (0, 2)) +julia> s = ShiftedArrays.lag(v, (0, 2)) 4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}: missing missing 1 5 missing missing 2 6 @@ -66,7 +66,7 @@ remaining dimensions is assumed to be `0`. ```jldoctest lead julia> v = [1, 3, 5, 4]; -julia> lead(v) +julia> ShiftedArrays.lead(v) 4-element ShiftedVector{Int64, Missing, Vector{Int64}}: 3 5 @@ -76,7 +76,7 @@ julia> lead(v) julia> w = 1:2:9 1:2:9 -julia> s = lead(w, 2) +julia> s = ShiftedArrays.lead(w, 2) 5-element ShiftedVector{Int64, Missing, StepRange{Int64, Int64}}: 5 7 @@ -94,7 +94,7 @@ julia> copy(s) julia> v = reshape(1:16, 4, 4); -julia> s = lead(v, (0, 2)) +julia> s = ShiftedArrays.lead(v, (0, 2)) 4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}: 9 13 missing missing 10 14 missing missing From fd1a4c794e49a4bcd0a5a002c2023b9cec2ec489 Mon Sep 17 00:00:00 2001 From: piever Date: Mon, 5 Sep 2022 18:20:36 +0200 Subject: [PATCH 3/4] update docs and remove unneeded section --- docs/src/index.md | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 2245371..4870231 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -70,21 +70,6 @@ julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN) 3.1 ``` -### Out of bound indexes - -The bound check is performed only on the parent `Array`, not on the `ShiftedArray`, so for example: - -```julia -julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN)[-2:3] -6-element Array{Float64,1}: - NaN - NaN - NaN - NaN - 1.2 - 3.1 -``` - ## Shifting the data Using the `ShiftedArray` type, this package provides two operations for lazily shifting vectors: `lag` and `lead`. @@ -92,21 +77,21 @@ Using the `ShiftedArray` type, this package provides two operations for lazily s ```julia julia> v = [1, 3, 5, 4]; -julia> lag(v) +julia> ShiftedArrays.lag(v) 4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: missing 1 3 5 -julia> v .- lag(v) # compute difference from previous element without unnecessary allocations +julia> v .- ShiftedArrays.lag(v) # compute difference from previous element without unnecessary allocations 4-element Array{Any,1}: missing 2 2 -1 -julia> s = lag(v, 2) # shift by more than one element +julia> s = ShiftedArrays.lag(v, 2) # shift by more than one element 4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: missing missing @@ -119,7 +104,7 @@ julia> s = lag(v, 2) # shift by more than one element ```julia julia> v = [1, 3, 5, 4]; -julia> lead(v) +julia> ShiftedArrays.lead(v) 4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: 3 5 From 42ad99dce225201fee486cb251cf26a0873269bc Mon Sep 17 00:00:00 2001 From: piever Date: Mon, 5 Sep 2022 18:29:42 +0200 Subject: [PATCH 4/4] doc fixes --- docs/src/index.md | 61 +++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 4870231..bc1d27d 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -8,25 +8,25 @@ A `ShiftedArray` is a lazy view of an Array, shifted on some or all of his index ```julia julia> v = reshape(1:16, 4, 4) -4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}: +4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64: 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 - julia> s = ShiftedArray(v, (2, 0)) - 4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: - missing missing missing missing - missing missing missing missing - 1 5 9 13 - 2 6 10 14 +julia> s = ShiftedArray(v, (2, 0)) +4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}: + missing missing missing missing + missing missing missing missing + 1 5 9 13 + 2 6 10 14 ``` The parent Array as well as the amount of shifting can be recovered with `parent` and `shifts` respectively. ```julia julia> parent(s) -4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}: +4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64: 1 5 9 13 2 6 10 14 3 7 11 15 @@ -42,10 +42,10 @@ Use `copy` to collect the shifted data into an `Array`: ```julia julia> copy(s) -4×4 Array{Union{Int64, Missing},2}: +4×4 Matrix{Union{Missing, Int64}}: missing missing missing missing missing missing missing missing - 1 5 9 13 + 1 5 9 13 2 6 10 14 ``` @@ -53,7 +53,7 @@ If you pass an integer, it will shift in the first dimension: ```julia julia> ShiftedArray(v, 1) -4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: +4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}: missing missing missing missing 1 5 9 13 2 6 10 14 @@ -64,12 +64,21 @@ A custom default value (other than `missing`) can be provided with the `default` ```julia julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN) -3-element ShiftedArrays.ShiftedArray{Float64,Float64,1,Array{Float64,1}}: +3-element ShiftedVector{Float64, Float64, Vector{Float64}}: NaN 1.2 3.1 ``` +### Out of bound indexes + +Accessing indexes outside the `ShiftedArray` give a `BoundsError`, even if the shifted index would have been valid in the parent array. + +```julia +julia> ShiftedArray([1, 2, 3], 1)[4] +ERROR: BoundsError: attempt to access 3-element ShiftedVector{Int64, Missing, Vector{Int64}} at index [4] +``` + ## Shifting the data Using the `ShiftedArray` type, this package provides two operations for lazily shifting vectors: `lag` and `lead`. @@ -78,24 +87,24 @@ Using the `ShiftedArray` type, this package provides two operations for lazily s julia> v = [1, 3, 5, 4]; julia> ShiftedArrays.lag(v) -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: +4-element ShiftedVector{Int64, Missing, Vector{Int64}}: missing - 1 - 3 + 1 + 3 5 julia> v .- ShiftedArrays.lag(v) # compute difference from previous element without unnecessary allocations -4-element Array{Any,1}: +4-element Vector{Union{Missing, Int64}}: missing - 2 - 2 + 2 + 2 -1 julia> s = ShiftedArrays.lag(v, 2) # shift by more than one element -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: +4-element ShiftedVector{Int64, Missing, Vector{Int64}}: missing missing - 1 + 1 3 ``` @@ -105,10 +114,10 @@ julia> s = ShiftedArrays.lag(v, 2) # shift by more than one element julia> v = [1, 3, 5, 4]; julia> ShiftedArrays.lead(v) -4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}: - 3 - 5 - 4 +4-element ShiftedVector{Int64, Missing, Vector{Int64}}: + 3 + 5 + 4 missing ``` @@ -125,7 +134,7 @@ Our implementation of `circshift` relies on them to avoid copying: julia> w = reshape(1:16, 4, 4); julia> s = ShiftedArrays.circshift(w, (1, -1)) -4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}: +4×4 CircShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}: 8 12 16 4 5 9 13 1 6 10 14 2 @@ -136,7 +145,7 @@ As usual, you can `copy` the result to have a normal `Array`: ```julia julia> copy(s) -4×4 Array{Int64,2}: +4×4 Matrix{Int64}: 8 12 16 4 5 9 13 1 6 10 14 2