Skip to content

Commit 7f487b6

Browse files
committed
Improve the docstrings for broadcast_getindex and broadcast_setindex!
1 parent 7c7f6ef commit 7f487b6

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

base/broadcast.jl

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -662,41 +662,56 @@ tuplebroadcast_getargs(::Tuple{}, k) = ()
662662
"""
663663
broadcast_getindex(A, inds...)
664664
665-
Broadcasts the `inds` arrays to a common size like [`broadcast`](@ref)
666-
and returns an array of the results `A[ks...]`,
667-
where `ks` goes over the positions in the broadcast result `A`.
665+
Equivalent to [`broadcast`](@ref)ing the `inds` arrays to a common size
666+
and returning an array `[A[ks...] for ks in zip(indsb...)]` (where `indsb`
667+
would be the broadcast `inds`). The shape of the output is equal to the shape of each
668+
element of `indsb`.
668669
669670
# Examples
670671
```jldoctest
671-
julia> A = [1, 2, 3, 4, 5]
672-
5-element Array{Int64,1}:
673-
1
674-
2
675-
3
676-
4
677-
5
672+
julia> A = [11 12; 21 22]
673+
2×2 Array{Int64,2}:
674+
11 12
675+
21 22
678676
679-
julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
680-
5×2 Array{Int64,2}:
681-
1 2
682-
3 4
683-
5 6
684-
7 8
685-
9 10
677+
julia> A[1:2, 1:2]
678+
2×2 Array{Int64,2}:
679+
11 12
680+
21 22
686681
687-
julia> C = broadcast(+,A,B)
688-
5×2 Array{Int64,2}:
689-
2 3
690-
5 6
691-
8 9
682+
julia> broadcast_getindex(A, 1:2, 1:2)
683+
2-element Array{Int64,1}:
684+
11
685+
22
686+
687+
julia> A[1:2, 2:-1:1]
688+
2×2 Array{Int64,2}:
689+
12 11
690+
22 21
691+
692+
julia> broadcast_getindex(A, 1:2, 2:-1:1)
693+
2-element Array{Int64,1}:
694+
12
695+
21
696+
```
697+
Because the indices are all vectors, these calls are like `[A[i[k], j[k]] for k = 1:2]`
698+
where `i` and `j` are the two index vectors.
699+
700+
```jldoctest
701+
julia> broadcast_getindex(A, 1:2, (1:2)')
702+
2×2 Array{Int64,2}:
692703
11 12
693-
14 15
704+
21 22
705+
706+
julia> broadcast_getindex(A, (1:2)', 1:2)
707+
2×2 Array{Int64,2}:
708+
11 21
709+
12 22
694710
695-
julia> broadcast_getindex(C,[1,2,10])
696-
3-element Array{Int64,1}:
697-
2
698-
5
699-
15
711+
julia> broadcast_getindex(A, [1 2 1; 1 2 2], [1, 2])
712+
2×3 Array{Int64,2}:
713+
11 21 11
714+
12 22 22
700715
```
701716
"""
702717
broadcast_getindex(src::AbstractArray, I::AbstractArray...) =
@@ -723,8 +738,16 @@ end
723738
"""
724739
broadcast_setindex!(A, X, inds...)
725740
726-
Broadcasts the `X` and `inds` arrays to a common size and stores the value from each
727-
position in `X` at the indices in `A` given by the same positions in `inds`.
741+
Efficient element-by-element setting of the values of `A` in a pattern established by `inds`.
742+
Equivalent to broadcasting the `X` and `inds` arrays to a common size, and then executing
743+
744+
for (is, js) in zip(zip(indsb), eachindex(Xb))
745+
A[is...] = Xb[js...]
746+
end
747+
748+
where `Xb` and `indsb` are the broadcast `X` and `inds`.
749+
750+
See [`broadcast_getindex`](@ref) for examples of the treatment of `inds`.
728751
"""
729752
@generated function broadcast_setindex!(A::AbstractArray, x, I::AbstractArray...)
730753
N = length(I)

0 commit comments

Comments
 (0)