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 2 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
39 changes: 31 additions & 8 deletions doc/src/devdocs/subarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,37 @@ 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(6, 6, 6);
Copy link
Member

Choose a reason for hiding this comment

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

Does the matrix really have to be so big?


julia> S1 = view(A, :, 5, 2:6)
6×5 SubArray{Float64,2,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,UnitRange{Int64}},false}:
0.643704 0.386568 0.146333 0.34124 0.344757
0.401421 0.330579 0.647078 0.52376 0.929902
0.525057 0.748041 0.998546 0.661835 0.520697
0.61201 0.265595 0.495394 0.833012 0.613
0.432577 0.291069 0.654293 0.498856 0.144613
0.082207 0.612628 0.834909 0.937867 0.465207

julia> S2 = view(A, 5, :, 2:6)
6×5 SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},UnitRange{Int64}},true}:
0.0462887 0.775194 0.727763 0.603624 0.481834
0.147329 0.60808 0.80743 0.140473 0.324266
0.260715 0.110096 0.883704 0.682579 0.778229
0.753508 0.838042 0.500725 0.494824 0.369212
0.432577 0.291069 0.654293 0.498856 0.144613
0.204728 0.192634 0.689977 0.677079 0.274452
```
```@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]`
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]]`.

The key feature of the design of SubArrays is that this index replacement can be performed without
Expand Down Expand Up @@ -71,13 +94,13 @@ 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,

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

Storing these values allows index replacement, and having the types encoded as parameters allows
Expand Down Expand Up @@ -138,7 +161,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