Skip to content

Commit b0ca50b

Browse files
authored
Merge pull request #20712 from JuliaLang/teh/doc_cartesian
Add CartesianIndex & CartesianRange docstrings, misc doc improvements
2 parents 0dca594 + 603726c commit b0ca50b

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

base/multidimensional.jl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ module IteratorsMD
1212

1313
export CartesianIndex, CartesianRange
1414

15-
# CartesianIndex
15+
"""
16+
CartesianIndex(i, j, k...) -> I
17+
CartesianIndex((i, j, k...)) -> I
18+
19+
Create a multidimensional index `I`, which can be used for
20+
indexing a multidimensional array `A`. In particular, `A[I]` is
21+
equivalent to `A[i,j,k...]`. One can freely mix integer and
22+
`CartesianIndex` indices; for example, `A[Ipre, i, Ipost]` (where
23+
`Ipre` and `Ipost` are `CartesianIndex` indices and `i` is an
24+
`Int`) can be a useful expression when writing algorithms that
25+
work along a single dimension of an array of arbitrary
26+
dimensionality.
27+
28+
A `CartesianIndex` is sometimes produced by [`eachindex`](@ref), and
29+
always when iterating with an explicit [`CartesianRange`](@ref).
30+
"""
1631
struct CartesianIndex{N} <: AbstractCartesianIndex{N}
1732
I::NTuple{N,Int}
1833
CartesianIndex{N}(index::NTuple{N,Integer}) where {N} = new(index)
@@ -77,6 +92,25 @@ module IteratorsMD
7792
icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))
7893

7994
# Iteration
95+
"""
96+
CartesianRange(Istart::CartesianIndex, Istop::CartesianIndex) -> R
97+
CartesianRange(sz::Dims) -> R
98+
CartesianRange(istart:istop, jstart:jstop, ...) -> R
99+
100+
Define a region `R` spanning a multidimensional rectangular range
101+
of integer indices. These are most commonly encountered in the
102+
context of iteration, where `for I in R ... end` will return
103+
[`CartesianIndex`](@ref) indices `I` equivalent to the nested loops
104+
105+
for j = jstart:jstop
106+
for i = istart:istop
107+
...
108+
end
109+
end
110+
111+
Consequently these can be useful for writing algorithms that
112+
work in arbitrary dimensions.
113+
"""
80114
struct CartesianRange{I<:CartesianIndex}
81115
start::I
82116
stop::I

base/reshapedarray.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ end
3737
length(R::ReshapedArrayIterator) = length(R.iter)
3838

3939
"""
40-
reshape(A, dims...)
41-
reshape(A, dims)
42-
43-
Return an array with the same data as the given array, but with different dimensions.
44-
45-
The new dimensions may be specified either as a list of arguments or as a shape
46-
tuple. At most one dimension may be specified with a `:`, in which case its
47-
length is computed such that its product with all the specified dimensions is
48-
equal to the length of the original array A.
40+
reshape(A, dims...) -> R
41+
reshape(A, dims) -> R
42+
43+
Return an array `R` with the same data as `A`, but with different
44+
dimension sizes or number of dimensions. The two arrays share the same
45+
underlying data, so that setting elements of `R` alters the values of
46+
`A` and vice versa.
47+
48+
The new dimensions may be specified either as a list of arguments or
49+
as a shape tuple. At most one dimension may be specified with a `:`,
50+
in which case its length is computed such that its product with all
51+
the specified dimensions is equal to the length of the original array
52+
`A`. The total number of elements must not change.
4953
5054
```jldoctest
5155
julia> A = collect(1:16)

doc/src/stdlib/arrays.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ Base.trues
3232
Base.falses
3333
Base.fill
3434
Base.fill!
35-
Base.reshape
3635
Base.similar(::AbstractArray)
3736
Base.similar(::Any, ::Tuple)
38-
Base.reinterpret
3937
Base.eye
4038
Base.linspace
4139
Base.logspace
40+
Base.Random.randsubseq
41+
Base.Random.randsubseq!
4242
```
4343

4444
## Broadcast and vectorization
@@ -56,20 +56,38 @@ Base.Broadcast.broadcast_getindex
5656
Base.Broadcast.broadcast_setindex!
5757
```
5858

59-
## Indexing, Assignment, and Concatenation
59+
## Indexing and assignment
6060

6161
```@docs
6262
Base.getindex(::AbstractArray, ::Any...)
63+
Base.setindex!(::AbstractArray, ::Any, ::Any...)
64+
Base.isassigned
65+
Base.Colon
66+
Base.CartesianIndex
67+
Base.CartesianRange
68+
Base.to_indices
69+
Base.checkbounds
70+
Base.checkindex
71+
```
72+
73+
## Views (SubArrays and other view types)
74+
75+
```@docs
6376
Base.view
6477
Base.@view
6578
Base.@views
66-
Base.to_indices
67-
Base.Colon
6879
Base.parent
6980
Base.parentindexes
7081
Base.slicedim
71-
Base.setindex!(::AbstractArray, ::Any, ::Any...)
72-
Base.isassigned
82+
Base.reinterpret
83+
Base.reshape
84+
Base.squeeze
85+
Base.vec
86+
```
87+
88+
## Concatenation and permutation
89+
90+
```@docs
7391
Base.cat
7492
Base.vcat
7593
Base.hcat
@@ -98,13 +116,7 @@ Base.findprev(::Any, ::Any, ::Integer)
98116
Base.permutedims
99117
Base.permutedims!
100118
Base.PermutedDimsArray
101-
Base.squeeze
102-
Base.vec
103119
Base.promote_shape
104-
Base.checkbounds
105-
Base.checkindex
106-
Base.Random.randsubseq
107-
Base.Random.randsubseq!
108120
```
109121

110122
## Array functions

0 commit comments

Comments
 (0)