Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] Fix small typo in subarray docs #23298

Merged
merged 4 commits into from
Aug 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions doc/src/devdocs/subarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,31 @@ cartesian, rather than linear, indexing.

Consider making 2d slices of a 3d array:

```julia
S1 = view(A, :, 5, 2:6)
S2 = view(A, 5, :, 2:6)
```@meta
DocTestSetup = :(srand(1234))
```
```jldoctest subarray
Copy link
Contributor Author

@iglpdc iglpdc Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 175, should jldoctest -> jldoctest subarray like here? (I couldn't figure out what this really does from the Documenter.jl docs)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we don't use anything (eg. variables, functions etc) from these blocks in that block below, see https://juliadocs.github.io/Documenter.jl/latest/man/doctests/#Preserving-definitions-between-blocks-1 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, @fredrikekre

julia> A = rand(2,3,4);

julia> S1 = view(A, :, 1, 2:3)
2×2 SubArray{Float64,2,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,UnitRange{Int64}},false}:
0.200586 0.066423
0.298614 0.956753

julia> S2 = view(A, 1, :, 2:3)
3×2 SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},UnitRange{Int64}},true}:
0.200586 0.066423
0.246837 0.646691
0.648882 0.276021
```
```@meta
DocTestSetup = nothing
```

`view` drops "singleton" dimensions (ones that are specified by an `Int`), so both `S1` and `S2`
are two-dimensional `SubArray`s. Consequently, the natural way to index these is with `S1[i,j]`.
To extract the value from the parent array `A`, the natural approach is to replace `S1[i,j]`
with `A[i,5,(2:6)[j]]` and `S2[i,j]` with `A[5,i,(2:6)[j]]`.
To extract the value from the parent array `A`, the natural approach is to replace `S1[i,j]`
with `A[i,1,(2:3)[j]]` and `S2[i,j]` with `A[1,i,(2:3)[j]]`.

The key feature of the design of SubArrays is that this index replacement can be performed without
any runtime overhead.
Expand All @@ -71,13 +87,14 @@ a `Tuple` of the types of the indices for each dimension. The final one, `L`, is
as a convenience for dispatch; it's a boolean that represents whether the index types support
fast linear indexing. More on that later.

If in our example above `A` is a `Array{Float64, 3}`, our `S1` case above would be a `SubArray{Int64,2,Array{Int64,3},Tuple{Colon,Int64,UnitRange{Int64}},false}`.
If in our example above `A` is a `Array{Float64, 3}`, our `S1` case above would be a
`SubArray{Float64,2,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,UnitRange{Int64}},false}`.
Note in particular the tuple parameter, which stores the types of the indices used to create
`S1`. Likewise,
`S1`. Likewise,

```julia-repl
```jldoctest subarray
julia> S1.indexes
(Colon(),5,2:6)
(Base.Slice(Base.OneTo(2)), 1, 2:3)
```

Storing these values allows index replacement, and having the types encoded as parameters allows
Expand Down Expand Up @@ -138,7 +155,7 @@ For `SubArray` types, the availability of efficient linear indexing is based pur
of the indices, and does not depend on values like the size of the parent array. You can ask whether
a given set of indices supports fast linear indexing with the internal `Base.viewindexing` function:

```julia-repl
```jldoctest subarray
julia> Base.viewindexing(S1.indexes)
IndexCartesian()

Expand Down