Skip to content

Commit 85ec999

Browse files
committed
Improve the docstrings for broadcast_getindex and broadcast_setindex!
1 parent 09a632b commit 85ec999

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

0 commit comments

Comments
 (0)