Skip to content

Commit 4ac2424

Browse files
committed
Improve the docstrings for broadcast_getindex and broadcast_setindex!
1 parent 395e4b0 commit 4ac2424

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
@@ -567,41 +567,56 @@ tuplebroadcast_getargs(As, k) =
567567
"""
568568
broadcast_getindex(A, inds...)
569569
570-
Broadcasts the `inds` arrays to a common size like [`broadcast`](@ref)
571-
and returns an array of the results `A[ks...]`,
572-
where `ks` goes over the positions in the broadcast result `A`.
570+
Equivalent to [`broadcast`](@ref)ing the `inds` arrays to a common size
571+
and returning an array `[A[ks...] for ks in zip(indsb...)]` (where `indsb`
572+
would be the broadcast `inds`). The shape of the output is equal to the shape of each
573+
element of `indsb`.
573574
574575
# Examples
575576
```jldoctest
576-
julia> A = [1, 2, 3, 4, 5]
577-
5-element Array{Int64,1}:
578-
1
579-
2
580-
3
581-
4
582-
5
577+
julia> A = [11 12; 21 22]
578+
2×2 Array{Int64,2}:
579+
11 12
580+
21 22
583581
584-
julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
585-
5×2 Array{Int64,2}:
586-
1 2
587-
3 4
588-
5 6
589-
7 8
590-
9 10
582+
julia> A[1:2, 1:2]
583+
2×2 Array{Int64,2}:
584+
11 12
585+
21 22
591586
592-
julia> C = broadcast(+,A,B)
593-
5×2 Array{Int64,2}:
594-
2 3
595-
5 6
596-
8 9
587+
julia> broadcast_getindex(A, 1:2, 1:2)
588+
2-element Array{Int64,1}:
589+
11
590+
22
591+
592+
julia> A[1:2, 2:-1:1]
593+
2×2 Array{Int64,2}:
594+
12 11
595+
22 21
596+
597+
julia> broadcast_getindex(A, 1:2, 2:-1:1)
598+
2-element Array{Int64,1}:
599+
12
600+
21
601+
```
602+
Because the indices are all vectors, these calls are like `[A[i[k], j[k]] for k = 1:2]`
603+
where `i` and `j` are the two index vectors.
604+
605+
```jldoctest
606+
julia> broadcast_getindex(A, 1:2, (1:2)')
607+
2×2 Array{Int64,2}:
597608
11 12
598-
14 15
609+
21 22
610+
611+
julia> broadcast_getindex(A, (1:2)', 1:2)
612+
2×2 Array{Int64,2}:
613+
11 21
614+
12 22
599615
600-
julia> broadcast_getindex(C,[1,2,10])
601-
3-element Array{Int64,1}:
602-
2
603-
5
604-
15
616+
julia> broadcast_getindex(A, [1 2 1; 1 2 2], [1, 2])
617+
2×3 Array{Int64,2}:
618+
11 21 11
619+
12 22 22
605620
```
606621
"""
607622
broadcast_getindex(src::AbstractArray, I::AbstractArray...) =
@@ -628,8 +643,16 @@ end
628643
"""
629644
broadcast_setindex!(A, X, inds...)
630645
631-
Broadcasts the `X` and `inds` arrays to a common size and stores the value from each
632-
position in `X` at the indices in `A` given by the same positions in `inds`.
646+
Efficient element-by-element setting of the values of `A` in a pattern established by `inds`.
647+
Equivalent to broadcasting the `X` and `inds` arrays to a common size, and then executing
648+
649+
for (is, js) in zip(zip(indsb), eachindex(Xb))
650+
A[is...] = Xb[js...]
651+
end
652+
653+
where `Xb` and `indsb` are the broadcast `X` and `inds`.
654+
655+
See [`broadcast_getindex`](@ref) for examples of the treatment of `inds`.
633656
"""
634657
@generated function broadcast_setindex!(A::AbstractArray, x, I::AbstractArray...)
635658
N = length(I)

0 commit comments

Comments
 (0)