Skip to content

Commit

Permalink
Even more simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Jan 21, 2017
1 parent a99ca73 commit 039b793
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
10 changes: 5 additions & 5 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ containertype(ct1, ct2) = promote_containertype(containertype(ct1), containertyp
@inline containertype(ct1, ct2, cts...) = promote_containertype(containertype(ct1), containertype(ct2, cts...))

promote_containertype(ct1, ct2) = Array
promote_containertype{T<:AbstractArray}(::Type{T}, ::Type{Tuple}) = Array
promote_containertype{T<:AbstractArray}(::Type{Tuple}, ::Type{T}) = Array
promote_containertype(::Type{Array}, ::Type{VecOrMat}) = Array
promote_containertype(::Type{VecOrMat}, ::Type{Array}) = Array
promote_containertype(::Type{VecOrMat}, ::Type{Tuple}) = Array
promote_containertype(::Type{Tuple}, ::Type{VecOrMat}) = Array
promote_containertype{T<:Array}(::Type{T}, ::ScalarType) = T
promote_containertype{T<:Array}(::ScalarType, ::Type{T}) = T
promote_containertype(::Type{Tuple}, ::ScalarType) = Tuple
promote_containertype(::ScalarType, ::Type{Tuple}) = Tuple
promote_containertype(::Type{Any}, ::Type{Nullable}) = Nullable
promote_containertype(::Type{Nullable}, ::Type{Any}) = Nullable
promote_containertype(T::Type, ::ScalarType) = T
promote_containertype(::ScalarType, T::Type) = T
promote_containertype{T}(::Type{T}, ::Type{T}) = T

## Calculate the broadcast indices of the arguments, or error if incompatible
Expand All @@ -52,7 +52,7 @@ broadcast_indices(A) = broadcast_indices(containertype(A), A)
broadcast_indices(::ScalarType, A) = ()
broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
broadcast_indices(::Type{Array}, A::Ref) = ()
broadcast_indices{T<:Array}(::Type{T}, A) = indices(A)
broadcast_indices{T<:AbstractArray}(::Type{T}, A) = indices(A)
@inline broadcast_indices(A, B...) = broadcast_shape((), broadcast_indices(A), map(broadcast_indices, B)...)
# shape (i.e., tuple-of-indices) inputs
broadcast_shape(shape::Tuple) = shape
Expand Down
9 changes: 1 addition & 8 deletions base/sparse/higherorderfns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module HigherOrderFns
# This module provides higher order functions specialized for sparse arrays,
# particularly map[!]/broadcast[!] for SparseVectors and SparseMatrixCSCs at present.
import Base: map, map!, broadcast, broadcast!
import Base.Broadcast: _containertype, promote_containertype,
broadcast_indices, broadcast_c, broadcast_c!
import Base.Broadcast: _containertype, promote_containertype, broadcast_c, broadcast_c!

using Base: front, tail, to_shape
using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseArray,
Expand Down Expand Up @@ -857,20 +856,14 @@ end

# (9) broadcast[!] over combinations of broadcast scalars and sparse vectors/matrices

# broadcast shape promotion for combinations of sparse arrays and other types
broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A)
# broadcast container type promotion for combinations of sparse arrays and other types
_containertype{T<:SparseVecOrMat}(::Type{T}) = AbstractSparseArray
# combinations of sparse arrays with broadcast scalars should yield sparse arrays
promote_containertype{T<:AbstractSparseArray}(::Type{VecOrMat}, ::Type{T}) = AbstractSparseArray
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{VecOrMat}) = AbstractSparseArray
promote_containertype{T<:AbstractSparseArray}(::Type{Any}, ::Type{T}) = AbstractSparseArray
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Any}) = AbstractSparseArray
# combinations of sparse arrays with anything else should fall back to generic dense broadcast
promote_containertype{T<:AbstractSparseArray}(::Type{Array}, ::Type{T}) = Array
promote_containertype{T<:AbstractSparseArray}(::Type{Tuple}, ::Type{T}) = Array
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Array}) = Array
promote_containertype{T<:AbstractSparseArray}(::Type{T}, ::Type{Tuple}) = Array

# broadcast[!] entry points for combinations of sparse arrays and other (scalar) types
@inline function broadcast_c{T<:AbstractSparseArray,N}(f, ::Type{T}, mixedargs::Vararg{Any,N})
Expand Down
21 changes: 9 additions & 12 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,12 @@ Base.size(A::Array19745) = size(A.data)

Base.Broadcast._containertype{T<:Array19745}(::Type{T}) = Array19745

# This way of defining promote_containertype methods is discouraged. The recommended
# way is by defining definitions for combinations of tight containers types
Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Array19745}) = Array19745
Base.Broadcast.promote_containertype(::Type{Array19745}, ct) = Array19745
Base.Broadcast.promote_containertype(ct, ::Type{Array19745}) = Array19745

Base.Broadcast.broadcast_indices(::Type{Array19745}, A) = indices(A)
Base.Broadcast.broadcast_indices(::Type{Array19745}, A::Ref) = ()
# Only define promote_containertype methods with tight container types
# (scalars are properly handled by default)
Base.Broadcast.promote_containertype{T<:AbstractArray}(::Type{Array19745}, ::Type{T}) = Array19745
Base.Broadcast.promote_containertype{T<:AbstractArray}(::Type{T}, ::Type{Array19745}) = Array19745
Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Tuple}) = Array19745
Base.Broadcast.promote_containertype(::Type{Tuple}, ::Type{Array19745}) = Array19745

getfield19745(x::Array19745) = x.data
getfield19745(x) = x
Expand All @@ -411,10 +409,9 @@ Base.Broadcast.broadcast_c(f, ::Type{Array19745}, A, Bs...) =
@testset "broadcasting for custom AbstractArray" begin
a = randn(10)
aa = Array19745(a)
@test a .+ 1 == @inferred(aa .+ 1)
@test a .* a' == @inferred(aa .* aa')
@test isa(aa .+ 1, Array19745)
@test isa(aa .* aa', Array19745)
@test a .+ 1 == @inferred(aa .+ 1)::Array19745
@test a .* a' == @inferred(aa .* aa')::Array19745
@test (aa .+ [1])::Array19745 == (aa .+ (1,))::Array19745
end

# broadcast should only "peel off" one container layer
Expand Down

0 comments on commit 039b793

Please sign in to comment.