Skip to content

Commit f3fad96

Browse files
authored
use REPL doctest for center/centered (#254)
1 parent 96fd74e commit f3fad96

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

src/OffsetArrays.jl

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,25 @@ a rounding procedure will be applied with mode `r`.
672672
# Examples
673673
674674
```jldoctest; setup=:(using OffsetArrays)
675-
A = reshape(collect(1:9), 3, 3)
676-
c = OffsetArrays.center(A) # (2, 2)
677-
A[c...] == 5 # true
675+
julia> A = reshape(collect(1:9), 3, 3)
676+
3×3 $(Matrix{Int}):
677+
1 4 7
678+
2 5 8
679+
3 6 9
678680
679-
Ao = OffsetArray(A, -2, -2)
680-
c = OffsetArrays.center(Ao) # (0, 0)
681-
Ao[c...] == 5 # true
681+
julia> c = OffsetArrays.center(A)
682+
(2, 2)
682683
683-
# output
684-
true
684+
julia> A[c...]
685+
5
686+
687+
julia> Ao = OffsetArray(A, -2, -2); # axes (-1:1, -1:1)
688+
689+
julia> c = OffsetArrays.center(Ao)
690+
(0, 0)
691+
692+
julia> Ao[c...]
693+
5
685694
```
686695
687696
To shift the center coordinate of the given array to `(0, 0, ...)`, you
@@ -696,26 +705,55 @@ end
696705
"""
697706
centered(A, cp=center(A)) -> Ao
698707
699-
Shift the center coordinate of array `A` to `(0, 0, ...)`. If `size(A, k)`
700-
is even, a rounding procedure will be applied with mode `r`.
708+
Shift the center coordinate/point `cp` of array `A` to `(0, 0, ..., 0)`. Internally, this is
709+
equivalent to `OffsetArray(A, .-cp)`.
701710
702711
!!! compat "OffsetArrays 1.9"
703712
This method requires at least OffsetArrays 1.9.
704713
705714
# Examples
706715
707716
```jldoctest; setup=:(using OffsetArrays)
708-
A = reshape(collect(1:9), 3, 3)
709-
Ao = OffsetArrays.centered(A)
710-
Ao[0, 0] == 5 # true
717+
julia> A = reshape(collect(1:9), 3, 3)
718+
3×3 $(Matrix{Int}):
719+
1 4 7
720+
2 5 8
721+
3 6 9
722+
723+
julia> Ao = OffsetArrays.centered(A); # axes (-1:1, -1:1)
724+
725+
julia> Ao[0, 0]
726+
5
727+
728+
julia> Ao = OffsetArray(A, OffsetArrays.Origin(0)); # axes (0:2, 0:2)
711729
712-
A = reshape(collect(1:9), 3, 3)
713-
Ao = OffsetArray(A, OffsetArrays.Origin(0))
714-
Aoo = OffsetArrays.centered(Ao)
715-
Aoo[0, 0] == 5 # true
730+
julia> Aoo = OffsetArrays.centered(Ao); # axes (-1:1, -1:1)
716731
717-
# output
718-
true
732+
julia> Aoo[0, 0]
733+
5
734+
```
735+
736+
Users are allowed to pass `cp` to change how "center point" is interpreted, but the meaning of the
737+
output array should be reinterpreted as well. For instance, if `cp = map(last, axes(A))` then this
738+
function no longer shifts the center point but instead the bottom-right point to `(0, 0, ..., 0)`.
739+
A commonly usage of `cp` is to change the rounding behavior when the array is of even size at some
740+
dimension:
741+
742+
```jldoctest; setup=:(using OffsetArrays)
743+
julia> A = reshape(collect(1:4), 2, 2) # Ideally the center should be (1.5, 1.5) but OffsetArrays only support integer offsets
744+
2×2 $(Matrix{Int}):
745+
1 3
746+
2 4
747+
748+
julia> OffsetArrays.centered(A, OffsetArrays.center(A, RoundUp)) # set (2, 2) as the center point
749+
2×2 OffsetArray(::$(Matrix{Int}), -1:0, -1:0) with eltype $(Int) with indices -1:0×-1:0:
750+
1 3
751+
2 4
752+
753+
julia> OffsetArrays.centered(A, OffsetArrays.center(A, RoundDown)) # set (1, 1) as the center point
754+
2×2 OffsetArray(::$(Matrix{Int}), 0:1, 0:1) with eltype $(Int) with indices 0:1×0:1:
755+
1 3
756+
2 4
719757
```
720758
721759
See also [`center`](@ref OffsetArrays.center).

0 commit comments

Comments
 (0)