@@ -662,41 +662,56 @@ tuplebroadcast_getargs(::Tuple{}, k) = ()
662
662
"""
663
663
broadcast_getindex(A, inds...)
664
664
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`.
668
669
669
670
# Examples
670
671
```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
678
676
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
686
681
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}:
692
703
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
694
710
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
700
715
```
701
716
"""
702
717
broadcast_getindex (src:: AbstractArray , I:: AbstractArray... ) =
723
738
"""
724
739
broadcast_setindex!(A, X, inds...)
725
740
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`.
728
751
"""
729
752
@generated function broadcast_setindex! (A:: AbstractArray , x, I:: AbstractArray... )
730
753
N = length (I)
0 commit comments