33 Origin(origin::Tuple)
44 Origin(origin::CartesianIndex)
55
6- A helper type to construct OffsetArray with given origin.
6+ A helper type to construct OffsetArray with a given origin. This is not exported .
77
8- The `origin` of an array is defined as the index of its first element , i.e., `first.(axes(A))`.
8+ The `origin` of an array is defined as the tuple of the first index along each axis , i.e., `first.(axes(A))`.
99
1010# Example
1111
12- ```jldoctest; setup=:(using OffsetArrays)
12+ ```jldoctest origin ; setup=:(using OffsetArrays)
1313julia> a = [1 2; 3 4];
1414
15- julia> OffsetArray(a, OffsetArrays.Origin(0, 1))
15+ julia> using OffsetArrays: Origin
16+
17+ julia> OffsetArray(a, Origin(0, 1))
16182×2 OffsetArray(::$(Array{Int,2 }) , 0:1, 1:2) with eltype $Int with indices 0:1×1:2:
1719 1 2
1820 3 4
1921
20- julia> OffsetArray(a, OffsetArrays.Origin(0)) # short notation for `Origin(0, 0)`
22+ julia> OffsetArray(a, Origin(0)) # short notation for `Origin(0, 0)`
23+ 2×2 OffsetArray(::$(Array{Int, 2 }) , 0:1, 0:1) with eltype $Int with indices 0:1×0:1:
24+ 1 2
25+ 3 4
26+ ```
27+
28+ An `Origin` object is callable, and it may shift the origin of an array to the specified point.
29+
30+ ```jldoctest origin
31+ julia> b = Origin(0)(a) # shift the origin of the array to (0,0)
21322×2 OffsetArray(::$(Array{Int, 2 }) , 0:1, 0:1) with eltype $Int with indices 0:1×0:1:
2233 1 2
2334 3 4
2435```
36+
37+ The type `Origin`, when called with an `AbstractArray` as the argument, will return an instance
38+ corresponding ot the origin of the array.
39+
40+ ```jldoctest origin
41+ julia> origin_b = Origin(b) # retrieve the origin of the array as an Origin instance
42+ Origin(0, 0)
43+
44+ julia> origin_b(ones(2,2)) # shift the origin of another array to that of b, in this case to (0,0)
45+ 2×2 OffsetArray(::$(Array{Float64, 2 }) , 0:1, 0:1) with eltype Float64 with indices 0:1×0:1:
46+ 1.0 1.0
47+ 1.0 1.0
48+ ```
49+
50+ !!! tip
51+ One may broadcast an `Origin` instance over multiple arrays to shift them all to the same origin.
52+ ```jldoctest
53+ julia> using OffsetArrays: Origin
54+
55+ julia> a = [1 2; 3 4]; # origin at (1,1)
56+
57+ julia> b = Origin(2,3)(a); # origin at (2,3)
58+
59+ julia> c = Origin(4)(a); # origin at (4,4)
60+
61+ julia> ao, bo, co = Origin(0).((a, b, c)); # shift all origins to (0,0)
62+
63+ julia> first.(axes(ao)) == first.(axes(bo)) == first.(axes(co)) == (0,0)
64+ true
65+
66+ julia> ao, bo, co = Origin(b).((a, b, c)); # shift all origins to that of b
67+
68+ julia> first.(axes(ao)) == first.(axes(bo)) == first.(axes(co)) == (2,3)
69+ true
70+
71+ julia> ao, bo, co = OffsetArray.((a, b, c), Origin(b)); # another way to do the same
72+
73+ julia> first.(axes(ao)) == first.(axes(bo)) == first.(axes(co)) == (2,3)
74+ true
75+ ```
2576"""
2677struct Origin{T<: Union{Tuple{Vararg{Int}}, Int} }
2778 index:: T
@@ -33,6 +84,8 @@ Origin(I::Number...) = Origin(I)
3384# Origin(0) != Origin((0, )) but they work the same with broadcasting
3485Origin (n:: Number ) = Origin {Int} (Int (n))
3586
36- (o:: Origin )(A:: AbstractArray ) = o. index .- first .(axes (A))
37-
3887Base. Broadcast. broadcastable (o:: Origin ) = Ref (o)
88+
89+ _showidx (index:: Integer ) = " (" * string (index) * " )"
90+ _showidx (index:: Tuple ) = string (index)
91+ Base. show (io:: IO , o:: Origin ) = print (io, " Origin" , _showidx (o. index))
0 commit comments