1
1
# OffsetArrays.jl
2
2
3
- [ ![ ] [ action-img ]] [ action-url ]
4
- [ ![ ] [ pkgeval-img ]] [ pkgeval-url ]
5
- [ ![ ] [ codecov-img ]] [ codecov-url ]
6
- [ ![ ] [ docs-stable-img ]] [ docs-stable-url ]
7
- [ ![ ] [ docs-dev-img ]] [ docs-dev-url ]
3
+ | Documentation (stable) | Documentation (dev) | Unit Tests | Code coverage | PkgEval |
4
+ | :-: | :-: | :-: | :-: | :-: |
5
+ | [ ![ ] [ docs-stable-img ]] [ docs-stable-url ] | [ ![ ] [ docs-dev-img ]] [ docs-dev-url ] | [ ![ ] [ action-img ]] [ action-url ] | [ ![ ] [ codecov-img ]] [ codecov-url ] | [ ![ ] [ pkgeval-img ]] [ pkgeval-url ] |
8
6
7
+ ## Introduction
9
8
10
9
OffsetArrays provides Julia users with arrays that have arbitrary
11
10
indices, similar to those found in some other programming languages
12
11
like Fortran.
13
12
13
+ An ` OffsetArray ` is a lightweight wrapper around an ` AbstractArray ` that shifts its indices.
14
+ Generally, indexing into an ` OffsetArray ` should be as performant as the parent array.
15
+
14
16
## Usage
15
17
16
- You can construct such arrays as follows:
18
+ There are two ways to construct ` OffsetArray ` s: by specifying the axes of the array, or
19
+ by specifying its origin.
20
+
21
+ The first way to construct an ` OffsetArray ` by specifying its axes is:
17
22
18
23
``` julia
19
24
OA = OffsetArray (A, axis1, axis2, ... )
@@ -30,20 +35,101 @@ julia> A = Float64.(reshape(1:15, 3, 5))
30
35
2.0 5.0 8.0 11.0 14.0
31
36
3.0 6.0 9.0 12.0 15.0
32
37
33
- julia> OA = OffsetArray (A, - 1 : 1 , 0 : 4 ) # OA will have axes (-1:1, 0:4)
38
+ julia> axes (A) # indices of a Matrix start from 1 along each axis
39
+ (Base. OneTo (3 ), Base. OneTo (5 ))
40
+
41
+ julia> OA = OffsetArray (A, - 1 : 1 , 0 : 4 ) # OA will have the axes (-1:1, 0:4)
34
42
3 × 5 OffsetArray (:: Matrix{Float64} , - 1 : 1 , 0 : 4 ) with eltype Float64 with indices - 1 : 1 × 0 : 4 :
35
43
1.0 4.0 7.0 10.0 13.0
36
44
2.0 5.0 8.0 11.0 14.0
37
45
3.0 6.0 9.0 12.0 15.0
38
46
39
- julia> OA = OffsetArray (A, CartesianIndex (- 1 , 0 ): CartesianIndex (1 , 4 ))
40
- 3 × 5 OffsetArray (:: Matrix{Float64} , - 1 : 1 , 0 : 4 ) with eltype Float64 with indices - 1 : 1 × 0 : 4 :
41
- [... ]
47
+ julia> OA[- 1 ,0 ]
48
+ 1.0
49
+
50
+ julia> OA[1 ,4 ]
51
+ 15.0
52
+ ```
53
+
54
+ The second way to construct an ` OffsetArray ` is by specifying the origin, that is, the first index
55
+ along each axis. This is particularly useful if one wants, eg., arrays that are 0-indexed as opposed
56
+ to 1-indexed.
57
+
58
+ A convenient way to construct an ` OffsetArray ` this way is by using ` OffsetArrays.Origin ` :
59
+
60
+ ``` julia
61
+ julia> using OffsetArrays: Origin
62
+
63
+ julia> Origin (0 )(A) # indices begin at 0 along all axes
64
+ 3 × 5 OffsetArray (:: Matrix{Float64} , 0 : 2 , 0 : 4 ) with eltype Float64 with indices 0 : 2 × 0 : 4 :
65
+ 1.0 4.0 7.0 10.0 13.0
66
+ 2.0 5.0 8.0 11.0 14.0
67
+ 3.0 6.0 9.0 12.0 15.0
68
+
69
+ julia> Origin (2 , 3 )(A) # indices begin at 2 along the first axis and 3 along the second
70
+ 3 × 5 OffsetArray (:: Matrix{Float64} , 2 : 4 , 3 : 7 ) with eltype Float64 with indices 2 : 4 × 3 : 7 :
71
+ 1.0 4.0 7.0 10.0 13.0
72
+ 2.0 5.0 8.0 11.0 14.0
73
+ 3.0 6.0 9.0 12.0 15.0
74
+ ```
75
+
76
+ While the examples here refer to the common case where the parent arrays have indices starting at 1,
77
+ this is not necessary. An ` OffsetArray ` may wrap any array that has integer indices, irrespective of
78
+ where the indices begin.
42
79
43
- julia> OA[- 1 ,0 ], OA[1 ,4 ]
44
- (1.0 , 15.0 )
80
+ ## How to go back to 1-indexed arrays
81
+
82
+ Certain libraries, such as ` LinearAlgebra ` , require arrays to be indexed from 1. Passing an ` OffsetArray `
83
+ with shifted indices would lead to an error here.
84
+
85
+ ``` julia
86
+ julia> A = Float64 .(reshape (1 : 16 , 4 , 4 ));
87
+
88
+ julia> AO = Origin (0 )(A);
89
+
90
+ julia> using LinearAlgebra
91
+
92
+ julia> Diagonal (AO)
93
+ ERROR: ArgumentError: offset arrays are not supported but got an array with index other than 1
94
+ ```
95
+
96
+ The way to obtain a ` 1 ` -indexed array from an ` OffsetArray ` is by using ` OffsetArrays.no_offset_view ` .
97
+
98
+ An example of this is:
99
+
100
+ ``` julia
101
+ julia> OffsetArrays. no_offset_view (AO)
102
+ 4 × 4 Matrix{Float64}:
103
+ 1.0 5.0 9.0 13.0
104
+ 2.0 6.0 10.0 14.0
105
+ 3.0 7.0 11.0 15.0
106
+ 4.0 8.0 12.0 16.0
107
+ ```
108
+
109
+ This may now be passed to ` LinearAlgebra ` :
110
+
111
+ ``` julia
112
+ julia> D = Diagonal (OffsetArrays. no_offset_view (AO))
113
+ 4 × 4 Diagonal{Float64, Vector{Float64}}:
114
+ 1.0 ⋅ ⋅ ⋅
115
+ ⋅ 6.0 ⋅ ⋅
116
+ ⋅ ⋅ 11.0 ⋅
117
+ ⋅ ⋅ ⋅ 16.0
45
118
```
46
119
120
+ If we want to restore the original indices of ` AO ` , we may wrap an ` OffsetArray ` around the ` Diagonal ` as:
121
+
122
+ ``` julia
123
+ julia> Origin (AO)(D)
124
+ 4 × 4 OffsetArray (:: Diagonal{Float64, Vector{Float64}} , 0 : 3 , 0 : 3 ) with eltype Float64 with indices 0 : 3 × 0 : 3 :
125
+ 1.0 ⋅ ⋅ ⋅
126
+ ⋅ 6.0 ⋅ ⋅
127
+ ⋅ ⋅ 11.0 ⋅
128
+ ⋅ ⋅ ⋅ 16.0
129
+ ```
130
+
131
+ Here, ` Origin(AO) ` is able to automatically infer and use the indices of ` AO ` .
132
+
47
133
<!-- badges -->
48
134
49
135
[ pkgeval-img ] : https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg
0 commit comments