Skip to content

Commit c8bb374

Browse files
committed
Rename Broadcast.*_indices to *_axes as appropriate
1 parent a2b9015 commit c8bb374

File tree

7 files changed

+66
-62
lines changed

7 files changed

+66
-62
lines changed

base/broadcast.jl

+28-28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using .Base.Cartesian
66
using .Base: Indices, OneTo, linearindices, tail, to_shape, isoperator, promote_typejoin,
77
_msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache, unalias
88
import .Base: broadcast, broadcast!, copy, copyto!
9-
export BroadcastStyle, broadcast_indices, broadcast_similar, broadcastable,
9+
export BroadcastStyle, broadcast_axes, broadcast_similar, broadcastable,
1010
broadcast_getindex, broadcast_setindex!, dotview, @__dot__
1111

1212
### Objects with customized broadcasting behavior should declare a BroadcastStyle
@@ -188,7 +188,7 @@ Base.show(io::IO, bc::Broadcasted{Style}) where {Style} = print(io, Broadcasted,
188188
broadcast_similar(::BroadcastStyle, ::Type{ElType}, inds, As...)
189189
190190
Allocate an output object for [`broadcast`](@ref), appropriate for the indicated
191-
[`Broadcast.BroadcastStyle`](@ref). `ElType` and `inds` specify the desired element type and indices of the
191+
[`Broadcast.BroadcastStyle`](@ref). `ElType` and `inds` specify the desired element type and axes of the
192192
container. `As...` are the input arguments supplied to `broadcast`.
193193
"""
194194
broadcast_similar(::DefaultArrayStyle{N}, ::Type{ElType}, inds::Indices{N}, bc) where {N,ElType} =
@@ -201,25 +201,25 @@ broadcast_similar(::ArrayConflict, ::Type{ElType}, inds::Indices, bc) where ElTy
201201
broadcast_similar(::ArrayConflict, ::Type{Bool}, inds::Indices, bc) =
202202
similar(BitArray, inds)
203203

204-
## Computing the result's indices. Most types probably won't need to specialize this.
205-
broadcast_indices() = ()
206-
broadcast_indices(A::Tuple) = (OneTo(length(A)),)
207-
broadcast_indices(A::Ref) = ()
208-
broadcast_indices(A) = axes(A)
204+
## Computing the result's axes. Most types probably won't need to specialize this.
205+
broadcast_axes() = ()
206+
broadcast_axes(A::Tuple) = (OneTo(length(A)),)
207+
broadcast_axes(A::Ref) = ()
208+
broadcast_axes(A) = axes(A)
209209
"""
210-
Base.broadcast_indices(::SrcStyle, A)
210+
Base.broadcast_axes(A)
211211
212-
Compute the indices for objects `A` with [`BroadcastStyle`](@ref) `SrcStyle`.
213-
If needed, you can specialize this method for your styles.
214-
You should only need to provide a custom implementation for non-AbstractArrayStyles.
212+
Compute the axes for `A`.
213+
214+
This should only be specialized for objects that do not define axes but want to participate in broadcasting.
215215
"""
216-
broadcast_indices
216+
broadcast_axes
217217

218218
### End of methods that users will typically have to specialize ###
219219

220220
Base.axes(bc::Broadcasted) = _axes(bc, bc.axes)
221221
_axes(::Broadcasted, axes::Tuple) = axes
222-
_axes(bc::Broadcasted, ::Nothing) = combine_indices(bc.args...)
222+
_axes(bc::Broadcasted, ::Nothing) = combine_axes(bc.args...)
223223
_axes(bc::Broadcasted{Style{Tuple}}, ::Nothing) = (Base.OneTo(length(longest_tuple(nothing, bc.args))),)
224224
_axes(bc::Broadcasted{<:AbstractArrayStyle{0}}, ::Nothing) = ()
225225

@@ -252,10 +252,10 @@ they must provide their own `Base.axes(::Broadcasted{Style})` and
252252
"""
253253
@inline function instantiate(bc::Broadcasted{Style}) where {Style}
254254
if bc.axes isa Nothing # Not done via dispatch to make it easier to extend instantiate(::Broadcasted{Style})
255-
axes = combine_indices(bc.args...)
255+
axes = combine_axes(bc.args...)
256256
else
257257
axes = bc.axes
258-
check_broadcast_indices(axes, bc.args...)
258+
check_broadcast_axes(axes, bc.args...)
259259
end
260260
return Broadcasted{Style}(bc.f, bc.args, axes)
261261
end
@@ -411,8 +411,8 @@ One of these should be undefined (and thus return Broadcast.Unknown).""")
411411
end
412412

413413
# Indices utilities
414-
combine_indices(A, B...) = broadcast_shape(broadcast_indices(A), combine_indices(B...))
415-
combine_indices(A) = broadcast_indices(A)
414+
combine_axes(A, B...) = broadcast_shape(broadcast_axes(A), combine_axes(B...))
415+
combine_axes(A) = broadcast_axes(A)
416416

417417
# shape (i.e., tuple-of-indices) inputs
418418
broadcast_shape(shape::Tuple) = shape
@@ -444,11 +444,11 @@ function check_broadcast_shape(shp, Ashp::Tuple)
444444
_bcsm(shp[1], Ashp[1]) || throw(DimensionMismatch("array could not be broadcast to match destination"))
445445
check_broadcast_shape(tail(shp), tail(Ashp))
446446
end
447-
check_broadcast_indices(shp, A) = check_broadcast_shape(shp, broadcast_indices(A))
447+
check_broadcast_axes(shp, A) = check_broadcast_shape(shp, broadcast_axes(A))
448448
# comparing many inputs
449-
@inline function check_broadcast_indices(shp, A, As...)
450-
check_broadcast_indices(shp, A)
451-
check_broadcast_indices(shp, As...)
449+
@inline function check_broadcast_axes(shp, A, As...)
450+
check_broadcast_axes(shp, A)
451+
check_broadcast_axes(shp, As...)
452452
end
453453

454454
## Indexing manipulations
@@ -468,8 +468,8 @@ an `Int`.
468468
Any remaining indices in `I` beyond the length of the `keep` tuple are truncated. The `keep` and `default`
469469
tuples may be created by `newindexer(argument)`.
470470
"""
471-
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(broadcast_indices(arg), I.I))
472-
Base.@propagate_inbounds newindex(arg, I::Int) = CartesianIndex(_newindex(broadcast_indices(arg), (I,)))
471+
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(broadcast_axes(arg), I.I))
472+
Base.@propagate_inbounds newindex(arg, I::Int) = CartesianIndex(_newindex(broadcast_axes(arg), (I,)))
473473
Base.@propagate_inbounds _newindex(ax::Tuple, I::Tuple) = (ifelse(Base.unsafe_length(ax[1])==1, ax[1][1], I[1]), _newindex(tail(ax), tail(I))...)
474474
Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple) = ()
475475
Base.@propagate_inbounds _newindex(ax::Tuple, I::Tuple{}) = (ax[1][1], _newindex(tail(ax), ())...)
@@ -484,7 +484,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()
484484

485485
# newindexer(A) generates `keep` and `Idefault` (for use by `newindex` above)
486486
# for a particular array `A`; `shapeindexer` does so for its axes.
487-
@inline newindexer(A) = shapeindexer(broadcast_indices(A))
487+
@inline newindexer(A) = shapeindexer(broadcast_axes(A))
488488
@inline shapeindexer(ax) = _newindexer(ax)
489489
@inline _newindexer(indsA::Tuple{}) = (), ()
490490
@inline function _newindexer(indsA::Tuple)
@@ -525,7 +525,7 @@ struct Extruded{T, K, D}
525525
keeps::K # A tuple of booleans, specifying which indices should be passed normally
526526
defaults::D # A tuple of integers, specifying the index to use when keeps[i] is false (as defaults[i])
527527
end
528-
@inline broadcast_indices(b::Extruded) = broadcast_indices(b.x)
528+
@inline broadcast_axes(b::Extruded) = broadcast_axes(b.x)
529529
Base.@propagate_inbounds _broadcast_getindex(b::Extruded, i) = b.x[newindex(i, b.keeps, b.defaults)]
530530
extrude(x::AbstractArray) = Extruded(x, newindexer(x)...)
531531
extrude(x) = x
@@ -1034,14 +1034,14 @@ julia> broadcast_getindex(A, [1 2 1; 1 2 2], [1, 2])
10341034
```
10351035
"""
10361036
broadcast_getindex(src::AbstractArray, I::AbstractArray...) =
1037-
broadcast_getindex!(Base.similar(Array{eltype(src)}, combine_indices(I...)), src, I...)
1037+
broadcast_getindex!(Base.similar(Array{eltype(src)}, combine_axes(I...)), src, I...)
10381038

10391039
@generated function broadcast_getindex!(dest::AbstractArray, src::AbstractArray, I::AbstractArray...)
10401040
N = length(I)
10411041
Isplat = Expr[:(I[$d]) for d = 1:N]
10421042
quote
10431043
@nexprs $N d->(I_d = I[d])
1044-
check_broadcast_indices(Base.axes(dest), $(Isplat...)) # unnecessary if this function is never called directly
1044+
check_broadcast_axes(Base.axes(dest), $(Isplat...)) # unnecessary if this function is never called directly
10451045
checkbounds(src, $(Isplat...))
10461046
@nexprs $N d->(@nexprs $N k->(Ibcast_d_k = Base.axes(I_k, d) == OneTo(1)))
10471047
@nloops $N i dest d->(@nexprs $N k->(j_d_k = Ibcast_d_k ? 1 : i_d)) begin
@@ -1072,7 +1072,7 @@ See [`broadcast_getindex`](@ref) for examples of the treatment of `inds`.
10721072
quote
10731073
@nexprs $N d->(I_d = I[d])
10741074
checkbounds(A, $(Isplat...))
1075-
shape = combine_indices($(Isplat...))
1075+
shape = combine_axes($(Isplat...))
10761076
@nextract $N shape d->(length(shape) < d ? OneTo(1) : shape[d])
10771077
@nexprs $N d->(@nexprs $N k->(Ibcast_d_k = Base.axes(I_k, d) == 1:1))
10781078
if !isa(x, AbstractArray)

base/deprecated.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,10 @@ end
11141114
@deprecate indices(a) axes(a)
11151115
@deprecate indices(a, d) axes(a, d)
11161116

1117+
# And similar _indices names in Broadcast
1118+
@eval Broadcast Base.@deprecate_binding broadcast_indices broadcast_axes false
1119+
@eval Broadcast Base.@deprecate_binding check_broadcast_indices check_broadcast_axes false
1120+
11171121
# PR #25046
11181122
export reload, workspace
11191123
reload(name::AbstractString) = error("`reload($(repr(name)))` is discontinued, consider Revise.jl for an alternative workflow.")

doc/src/base/arrays.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ For specializing broadcast on custom types, see
6969
```@docs
7070
Base.BroadcastStyle
7171
Base.broadcast_similar
72-
Base.broadcast_indices
72+
Base.broadcast_axes
7373
Base.Broadcast.AbstractArrayStyle
7474
Base.Broadcast.ArrayStyle
7575
Base.Broadcast.DefaultArrayStyle

doc/src/manual/interfaces.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ V = view(A, [1,2,4], :) # is not strided, as the spacing between rows is not f
443443
| `Base.broadcast_similar(::DestStyle, ::Type{ElType}, inds, bc)` | Allocation of output container |
444444
| **Optional methods** | | |
445445
| `Base.BroadcastStyle(::Style1, ::Style2) = Style12()` | Precedence rules for mixing styles |
446-
| `Base.broadcast_indices(::StyleA, A)` | Declaration of the indices of `A` for broadcasting purposes (defaults to [`axes(A)`](@ref)) |
446+
| `Base.broadcast_axes(::StyleA, A)` | Declaration of the indices of `A` for broadcasting purposes (defaults to [`axes(A)`](@ref)) |
447447
| `Base.broadcastable(x)` | Convert `x` to an object that has `axes` and supports indexing |
448448
| **Bypassing default machinery** | |
449449
| `Base.copy(bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast` |

stdlib/SparseArrays/src/higherorderfns.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function _diffshape_broadcast(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMa
183183
fpreszeros = _iszero(fofzeros)
184184
indextypeC = _promote_indtype(A, Bs...)
185185
entrytypeC = Base.Broadcast.combine_eltypes(f, (A, Bs...))
186-
shapeC = to_shape(Base.Broadcast.combine_indices(A, Bs...))
186+
shapeC = to_shape(Base.Broadcast.combine_axes(A, Bs...))
187187
maxnnzC = fpreszeros ? _checked_maxnnzbcres(shapeC, A, Bs...) : _densennz(shapeC)
188188
C = _allocres(shapeC, indextypeC, entrytypeC, maxnnzC)
189189
return fpreszeros ? _broadcast_zeropres!(f, C, A, Bs...) :
@@ -984,7 +984,7 @@ end
984984

985985
@inline function _copyto!(f, dest, As::SparseVecOrMat...)
986986
_aresameshape(dest, As...) && return _noshapecheck_map!(f, dest, As...)
987-
Base.Broadcast.check_broadcast_indices(axes(dest), As...)
987+
Base.Broadcast.check_broadcast_axes(axes(dest), As...)
988988
fofzeros = f(_zeros_eltypes(As...)...)
989989
if _iszero(fofzeros)
990990
return _broadcast_zeropres!(f, dest, As...)

stdlib/SparseArrays/test/higherorderfns.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ end
125125
@test broadcast!(cos, Z, X) == sparse(broadcast!(cos, fZ, fX))
126126
# --> test shape checks for broadcast! entry point
127127
# TODO strengthen this test, avoiding dependence on checking whether
128-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
128+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
129129
try
130-
Base.Broadcast.check_broadcast_indices(axes(Z), spzeros((shapeX .- 1)...))
130+
Base.Broadcast.check_broadcast_axes(axes(Z), spzeros((shapeX .- 1)...))
131131
catch
132132
@test_throws DimensionMismatch broadcast!(sin, Z, spzeros((shapeX .- 1)...))
133133
end
@@ -149,9 +149,9 @@ end
149149
@test broadcast!(cos, V, X) == sparse(broadcast!(cos, fV, fX))
150150
# --> test shape checks for broadcast! entry point
151151
# TODO strengthen this test, avoiding dependence on checking whether
152-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
152+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
153153
try
154-
Base.Broadcast.check_broadcast_indices(axes(V), spzeros((shapeX .- 1)...))
154+
Base.Broadcast.check_broadcast_axes(axes(V), spzeros((shapeX .- 1)...))
155155
catch
156156
@test_throws DimensionMismatch broadcast!(sin, V, spzeros((shapeX .- 1)...))
157157
end
@@ -184,9 +184,9 @@ end
184184
@test broadcast(*, X, Y) == sparse(broadcast(*, fX, fY))
185185
@test broadcast(f, X, Y) == sparse(broadcast(f, fX, fY))
186186
# TODO strengthen this test, avoiding dependence on checking whether
187-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
187+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
188188
try
189-
Base.Broadcast.combine_indices(spzeros((shapeX .- 1)...), Y)
189+
Base.Broadcast.combine_axes(spzeros((shapeX .- 1)...), Y)
190190
catch
191191
@test_throws DimensionMismatch broadcast(+, spzeros((shapeX .- 1)...), Y)
192192
end
@@ -207,9 +207,9 @@ end
207207
@test broadcast!(f, Z, X, Y) == sparse(broadcast!(f, fZ, fX, fY))
208208
# --> test shape checks for both broadcast and broadcast! entry points
209209
# TODO strengthen this test, avoiding dependence on checking whether
210-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
210+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
211211
try
212-
Base.Broadcast.check_broadcast_indices(axes(Z), spzeros((shapeX .- 1)...), Y)
212+
Base.Broadcast.check_broadcast_axes(axes(Z), spzeros((shapeX .- 1)...), Y)
213213
catch
214214
@test_throws DimensionMismatch broadcast!(f, Z, spzeros((shapeX .- 1)...), Y)
215215
end
@@ -247,9 +247,9 @@ end
247247
@test broadcast(*, X, Y, Z) == sparse(broadcast(*, fX, fY, fZ))
248248
@test broadcast(f, X, Y, Z) == sparse(broadcast(f, fX, fY, fZ))
249249
# TODO strengthen this test, avoiding dependence on checking whether
250-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
250+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
251251
try
252-
Base.Broadcast.combine_indices(spzeros((shapeX .- 1)...), Y, Z)
252+
Base.Broadcast.combine_axes(spzeros((shapeX .- 1)...), Y, Z)
253253
catch
254254
@test_throws DimensionMismatch broadcast(+, spzeros((shapeX .- 1)...), Y, Z)
255255
end
@@ -279,9 +279,9 @@ end
279279
@test broadcast!(f, Q, X, Y, Z) == sparse(broadcast!(f, fQ, fX, fY, fZ))
280280
# --> test shape checks for both broadcast and broadcast! entry points
281281
# TODO strengthen this test, avoiding dependence on checking whether
282-
# check_broadcast_indices throws to determine whether sparse broadcast should throw
282+
# check_broadcast_axes throws to determine whether sparse broadcast should throw
283283
try
284-
Base.Broadcast.check_broadcast_indices(axes(Q), spzeros((shapeX .- 1)...), Y, Z)
284+
Base.Broadcast.check_broadcast_axes(axes(Q), spzeros((shapeX .- 1)...), Y, Z)
285285
catch
286286
@test_throws DimensionMismatch broadcast!(f, Q, spzeros((shapeX .- 1)...), Y, Z)
287287
end

test/broadcast.jl

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module TestBroadcastInternals
44

5-
using Base.Broadcast: check_broadcast_indices, check_broadcast_shape, newindex, _bcs
5+
using Base.Broadcast: check_broadcast_axes, check_broadcast_shape, newindex, _bcs
66
using Base: OneTo
77
using Test, Random
88

@@ -19,22 +19,22 @@ using Test, Random
1919
@test_throws DimensionMismatch _bcs((-1:1, 2:6), (-1:1, 2:5))
2020
@test_throws DimensionMismatch _bcs((-1:1, 2:5), (2, 2:5))
2121

22-
@test @inferred(Broadcast.combine_indices(zeros(3,4), zeros(3,4))) == (OneTo(3),OneTo(4))
23-
@test @inferred(Broadcast.combine_indices(zeros(3,4), zeros(3))) == (OneTo(3),OneTo(4))
24-
@test @inferred(Broadcast.combine_indices(zeros(3), zeros(3,4))) == (OneTo(3),OneTo(4))
25-
@test @inferred(Broadcast.combine_indices(zeros(3), zeros(1,4), zeros(1))) == (OneTo(3),OneTo(4))
26-
27-
check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,5))
28-
check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,1))
29-
check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3))
30-
check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,5), zeros(3))
31-
check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,5), 1)
32-
check_broadcast_indices((OneTo(3),OneTo(5)), 5, 2)
33-
@test_throws DimensionMismatch check_broadcast_indices((OneTo(3),OneTo(5)), zeros(2,5))
34-
@test_throws DimensionMismatch check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,4))
35-
@test_throws DimensionMismatch check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,4,2))
36-
@test_throws DimensionMismatch check_broadcast_indices((OneTo(3),OneTo(5)), zeros(3,5), zeros(2))
37-
check_broadcast_indices((-1:1, 6:9), 1)
22+
@test @inferred(Broadcast.combine_axes(zeros(3,4), zeros(3,4))) == (OneTo(3),OneTo(4))
23+
@test @inferred(Broadcast.combine_axes(zeros(3,4), zeros(3))) == (OneTo(3),OneTo(4))
24+
@test @inferred(Broadcast.combine_axes(zeros(3), zeros(3,4))) == (OneTo(3),OneTo(4))
25+
@test @inferred(Broadcast.combine_axes(zeros(3), zeros(1,4), zeros(1))) == (OneTo(3),OneTo(4))
26+
27+
check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,5))
28+
check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,1))
29+
check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3))
30+
check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,5), zeros(3))
31+
check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,5), 1)
32+
check_broadcast_axes((OneTo(3),OneTo(5)), 5, 2)
33+
@test_throws DimensionMismatch check_broadcast_axes((OneTo(3),OneTo(5)), zeros(2,5))
34+
@test_throws DimensionMismatch check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,4))
35+
@test_throws DimensionMismatch check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,4,2))
36+
@test_throws DimensionMismatch check_broadcast_axes((OneTo(3),OneTo(5)), zeros(3,5), zeros(2))
37+
check_broadcast_axes((-1:1, 6:9), 1)
3838

3939
check_broadcast_shape((-1:1, 6:9), (-1:1, 6:9))
4040
check_broadcast_shape((-1:1, 6:9), (-1:1, 1))
@@ -678,7 +678,7 @@ struct T22053
678678
t
679679
end
680680
Broadcast.BroadcastStyle(::Type{T22053}) = Broadcast.Style{T22053}()
681-
Broadcast.broadcast_indices(::T22053) = ()
681+
Broadcast.broadcast_axes(::T22053) = ()
682682
Broadcast.broadcastable(t::T22053) = t
683683
function Base.copy(bc::Broadcast.Broadcasted{Broadcast.Style{T22053}})
684684
all(x->isa(x, T22053), bc.args) && return 1

0 commit comments

Comments
 (0)