@@ -626,6 +626,75 @@ _no_offset_view(::Tuple{<:Base.OneTo,Vararg{<:Base.OneTo}}, A::AbstractUnitRange
626
626
_no_offset_view (:: Any , A:: AbstractArray ) = OffsetArray (A, Origin (1 ))
627
627
_no_offset_view (:: Any , A:: AbstractUnitRange ) = UnitRange (A)
628
628
629
+ # ####
630
+ # center/centered
631
+ # These two helpers are deliberately not exported; their meaning can be very different in
632
+ # other scenarios and will be very likely to cause name conflicts if exported.
633
+ # ####
634
+ """
635
+ center(A, [r::RoundingMode=RoundDown])::Dims
636
+
637
+ Return the center coordinate of given array `A`. If `size(A, k)` is even,
638
+ a rounding procedure will be applied with mode `r`.
639
+
640
+ !!! compat "OffsetArrays 1.9"
641
+ This method requires at least OffsetArrays 1.9.
642
+
643
+ # Examples
644
+
645
+ ```jldoctest; setup=:(using OffsetArrays)
646
+ A = reshape(collect(1:9), 3, 3)
647
+ c = OffsetArrays.center(A) # (2, 2)
648
+ A[c...] == 5 # true
649
+
650
+ Ao = OffsetArray(A, -2, -2)
651
+ c = OffsetArrays.center(Ao) # (0, 0)
652
+ Ao[c...] == 5 # true
653
+
654
+ # output
655
+ true
656
+ ```
657
+
658
+ To shift the center coordinate of the given array to `(0, 0, ...)`, you
659
+ can use [`centered`](@ref OffsetArrays.centered).
660
+ """
661
+ function center (A:: AbstractArray , r:: RoundingMode = RoundDown)
662
+ map (axes (A)) do inds
663
+ round (Int, (length (inds)- 1 )/ 2 , r) + first (inds)
664
+ end
665
+ end
666
+
667
+ """
668
+ centered(A, r::RoundingMode=RoundDown) -> Ao
669
+
670
+ Shift the center coordinate of array `A` to `(0, 0, ...)`. If `size(A, k)`
671
+ is even, a rounding procedure will be applied with mode `r`.
672
+
673
+ !!! compat "OffsetArrays 1.9"
674
+ This method requires at least OffsetArrays 1.9.
675
+
676
+ # Examples
677
+
678
+ ```jldoctest; setup=:(using OffsetArrays)
679
+ A = reshape(collect(1:9), 3, 3)
680
+ Ao = OffsetArrays.centered(A)
681
+ Ao[0, 0] == 5 # true
682
+
683
+ A = reshape(collect(1:9), 3, 3)
684
+ Ao = OffsetArray(A, OffsetArrays.Origin(0))
685
+ Aoo = OffsetArrays.centered(Ao)
686
+ Aoo[0, 0] == 5 # true
687
+
688
+ # output
689
+ true
690
+ ```
691
+
692
+ To query the center coordinate of the given array, you can
693
+ instead use [`center`](@ref OffsetArrays.center).
694
+ """
695
+ centered (A:: AbstractArray , r:: RoundingMode = RoundDown) = OffsetArray (A, .- center (A, r))
696
+
697
+
629
698
# ###
630
699
# work around for segfault in searchsorted*
631
700
# https://github.com/JuliaLang/julia/issues/33977
0 commit comments