@@ -8,7 +8,7 @@ SubArrays
8
8
9
9
Julia's ``SubArray `` type is a container encoding a "view" of a parent
10
10
``AbstractArray ``. This page documents some of the design principles
11
- and implementation of ``SubArrays `` .
11
+ and implementation of ``SubArray `` \s .
12
12
13
13
Indexing: cartesian vs. linear indexing
14
14
---------------------------------------
@@ -25,14 +25,14 @@ example, a 3d array ``A3`` can be indexed as ``A3[i,j]``, in which
25
25
case ``i `` is interpreted as a cartesian index for the first
26
26
dimension, and ``j `` is a linear index over dimensions 2 and 3.
27
27
28
- For ``Arrays `` , linear indexing appeals to the underlying storage
28
+ For ``Array `` \s , linear indexing appeals to the underlying storage
29
29
format: an array is laid out as a contiguous block of memory, and
30
30
hence the linear index is just the offset (+1) of the corresponding
31
31
entry relative to the beginning of the array. However, this is not
32
- true for many other ``AbstractArrays `` : examples include
32
+ true for many other ``AbstractArray `` types : examples include
33
33
``SparseMatrixCSC ``, arrays that require some kind of computation
34
34
(such as interpolation), and the type under discussion here,
35
- ``SubArrays ``. For these types, the underlying information is more
35
+ ``SubArray ``. For these types, the underlying information is more
36
36
naturally described in terms of cartesian indexes.
37
37
38
38
You can manually convert from a cartesian index to a linear index with
@@ -45,7 +45,7 @@ While converting from a cartesian index to a linear index is fast
45
45
index to a cartesian index is very slow: it relies on the ``div ``
46
46
operation, which is one of the slowest low-level operations you can
47
47
perform with a CPU. For this reason, any code that deals with
48
- ``AbstractArrays `` is best designed in terms of cartesian, rather than
48
+ ``AbstractArray `` types is best designed in terms of cartesian, rather than
49
49
linear, indexing.
50
50
51
51
Index replacement
@@ -57,7 +57,7 @@ Consider making 2d slices of a 3d array::
57
57
S2 = slice(A, 5, :, 2:6)
58
58
59
59
``slice `` drops "singleton" dimensions (ones that are specified by an
60
- ``Int ``), so both ``S1 `` and ``S2 `` are two-dimensional ``SubArrays `` .
60
+ ``Int ``), so both ``S1 `` and ``S2 `` are two-dimensional ``SubArray `` \s .
61
61
Consequently, the natural way to index these is with ``S1[i,j] ``. To
62
62
extract the value from the parent array ``A ``, the natural approach is
63
63
to replace ``S1[i,j] `` with ``A[i,5,(2:6)[j]] `` and ``S2[i,j] `` with
@@ -83,7 +83,7 @@ of the type::
83
83
stride1::Int # used only for linear indexing
84
84
end
85
85
86
- ``SubArrays `` have 5 type parameters. The first two are the
86
+ ``SubArray `` has 5 type parameters. The first two are the
87
87
standard element type and dimensionality. The next is the type of the
88
88
parent ``AbstractArray ``. The most heavily-used is the fourth
89
89
parameter, a ``tuple `` of the types of the indexes for each dimension.
@@ -120,7 +120,7 @@ Because of this conversion, ``S3`` is three-dimensional.
120
120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121
121
122
122
Performing index translation requires that you do different things for
123
- different types of `` SubArrays `` . For example, for ``S1 ``, one needs
123
+ different concrete `` SubArray `` types . For example, for ``S1 ``, one needs
124
124
to apply the ``i,j `` indexes to the first and third dimensions of the
125
125
parent array, whereas for ``S2 `` one needs to apply them to the
126
126
second and third. The simplest approach to indexing would be to do
@@ -148,7 +148,7 @@ The better approach is to dispatch to specific methods to handle each
148
148
type of input. Note, however, that the number of distinct methods
149
149
needed grows exponentially in the number of dimensions, and since
150
150
Julia supports arrays of any dimension the number of methods required
151
- is in fact infinite. Fortunately, ``stagedfunctions `` allow one to
151
+ is in fact infinite. Fortunately, ``stagedfunction `` \s allow one to
152
152
generate the necessary methods quite straightforwardly. The resulting
153
153
code looks quite a lot like the runtime approach above, but all of the
154
154
type analysis is performed at the time of method instantiation. For a
@@ -162,7 +162,7 @@ Linear indexing
162
162
163
163
Linear indexing can be implemented efficiently when the entire array
164
164
has a single stride that separates successive elements. For
165
- ``SubArrays `` , the availability of efficient linear indexing is based
165
+ ``SubArray `` types , the availability of efficient linear indexing is based
166
166
purely on the types of the indexes, and does not depend on values like
167
167
the size of the array. It therefore can miss some cases in which the
168
168
stride happens to be uniform::
@@ -205,7 +205,7 @@ decision based purely on types encoded in the parameters of the
205
205
``SubArray ``, ``S = sub(A, 2:2:4, :) `` cannot implement efficient
206
206
linear indexing.
207
207
208
- The last parameter of ``SubArrays ``, ``LD ``, encodes the highest
208
+ The last parameter of ``SubArray ``, ``LD ``, encodes the highest
209
209
dimension up to which elements are guaranteed to have uniform stride.
210
210
When ``LD == length(I) ``, the length of the ``indexes `` tuple,
211
211
efficient linear indexing becomes possible.
@@ -240,7 +240,7 @@ indexing.
240
240
241
241
The main reason ``LD `` cannot always be inferred from the ``indexes `` tuple
242
242
is because ``sub `` converts internal ``Int `` indexes into
243
- ``UnitRanges `` . Consequently it is important to encode "safe"
243
+ ``UnitRange `` \s . Consequently it is important to encode "safe"
244
244
dimensions of size 1 prior to conversion. Up to the ``LDth `` entry,
245
245
we can be sure that any ``UnitRange `` was, in fact, an ``Integer ``
246
246
prior to conversion.
0 commit comments