Skip to content

Commit 47a906c

Browse files
committed
Improve the docstrings for broadcast_getindex and broadcast_setindex!
1 parent b19768f commit 47a906c

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
@@ -616,41 +616,56 @@ tuplebroadcast_getargs(::Tuple{}, k) = ()
616616
"""
617617
broadcast_getindex(A, inds...)
618618
619-
Broadcasts the `inds` arrays to a common size like [`broadcast`](@ref)
620-
and returns an array of the results `A[ks...]`,
621-
where `ks` goes over the positions in the broadcast result `A`.
619+
Equivalent to [`broadcast`](@ref)ing the `inds` arrays to a common size
620+
and returning an array `[A[ks...] for ks in zip(indsb...)]` (where `indsb`
621+
would be the broadcast `inds`). The shape of the output is equal to the shape of each
622+
element of `indsb`.
622623
623624
# Examples
624625
```jldoctest
625-
julia> A = [1, 2, 3, 4, 5]
626-
5-element Array{Int64,1}:
627-
1
628-
2
629-
3
630-
4
631-
5
626+
julia> A = [11 12; 21 22]
627+
2×2 Array{Int64,2}:
628+
11 12
629+
21 22
632630
633-
julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
634-
5×2 Array{Int64,2}:
635-
1 2
636-
3 4
637-
5 6
638-
7 8
639-
9 10
631+
julia> A[1:2, 1:2]
632+
2×2 Array{Int64,2}:
633+
11 12
634+
21 22
640635
641-
julia> C = broadcast(+,A,B)
642-
5×2 Array{Int64,2}:
643-
2 3
644-
5 6
645-
8 9
636+
julia> broadcast_getindex(A, 1:2, 1:2)
637+
2-element Array{Int64,1}:
638+
11
639+
22
640+
641+
julia> A[1:2, 2:-1:1]
642+
2×2 Array{Int64,2}:
643+
12 11
644+
22 21
645+
646+
julia> broadcast_getindex(A, 1:2, 2:-1:1)
647+
2-element Array{Int64,1}:
648+
12
649+
21
650+
```
651+
Because the indices are all vectors, these calls are like `[A[i[k], j[k]] for k = 1:2]`
652+
where `i` and `j` are the two index vectors.
653+
654+
```jldoctest
655+
julia> broadcast_getindex(A, 1:2, (1:2)')
656+
2×2 Array{Int64,2}:
646657
11 12
647-
14 15
658+
21 22
659+
660+
julia> broadcast_getindex(A, (1:2)', 1:2)
661+
2×2 Array{Int64,2}:
662+
11 21
663+
12 22
648664
649-
julia> broadcast_getindex(C,[1,2,10])
650-
3-element Array{Int64,1}:
651-
2
652-
5
653-
15
665+
julia> broadcast_getindex(A, [1 2 1; 1 2 2], [1, 2])
666+
2×3 Array{Int64,2}:
667+
11 21 11
668+
12 22 22
654669
```
655670
"""
656671
broadcast_getindex(src::AbstractArray, I::AbstractArray...) =
@@ -677,8 +692,16 @@ end
677692
"""
678693
broadcast_setindex!(A, X, inds...)
679694
680-
Broadcasts the `X` and `inds` arrays to a common size and stores the value from each
681-
position in `X` at the indices in `A` given by the same positions in `inds`.
695+
Efficient element-by-element setting of the values of `A` in a pattern established by `inds`.
696+
Equivalent to broadcasting the `X` and `inds` arrays to a common size, and then executing
697+
698+
for (is, js) in zip(zip(indsb), eachindex(Xb))
699+
A[is...] = Xb[js...]
700+
end
701+
702+
where `Xb` and `indsb` are the broadcast `X` and `inds`.
703+
704+
See [`broadcast_getindex`](@ref) for examples of the treatment of `inds`.
682705
"""
683706
@generated function broadcast_setindex!(A::AbstractArray, x, I::AbstractArray...)
684707
N = length(I)

0 commit comments

Comments
 (0)