From c5d9034f74fca85910847413ab32bfd980f51d7d Mon Sep 17 00:00:00 2001 From: pabloferz Date: Fri, 17 Jun 2016 12:09:45 +0200 Subject: [PATCH] Treat types as scalars in broadcast --- base/abstractarray.jl | 7 +++++-- base/broadcast.jl | 13 +++++++++---- base/float.jl | 11 +++++++++++ base/int.jl | 1 + base/parse.jl | 3 +++ base/promotion.jl | 1 + 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c500a68586ef07..9eb5871bf34c50 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -412,8 +412,8 @@ end promote_indices(a::AbstractArray, b::AbstractArray) = _promote_indices(indicesbehavior(a), indicesbehavior(b), a, b) _promote_indices(::IndicesStartAt1, ::IndicesStartAt1, a, b) = a _promote_indices(::IndicesBehavior, ::IndicesBehavior, a, b) = throw(ArgumentError("types $(typeof(a)) and $(typeof(b)) do not have promote_indices defined")) -promote_indices(a::Number, b::AbstractArray) = b -promote_indices(a::AbstractArray, b::Number) = a +promote_indices(a::Union{Number,Type}, b::AbstractArray) = b +promote_indices(a::AbstractArray, b::Union{Number,Type}) = a # Strip off the index-changing container---this assumes that `parent` # performs such an operation. TODO: since few things in Base need this, it @@ -1459,9 +1459,12 @@ end promote_eltype_op(::Any) = (@_pure_meta; Bottom) promote_eltype_op{T}(op, ::AbstractArray{T}) = (@_pure_meta; promote_op(op, T)) promote_eltype_op{T}(op, ::T ) = (@_pure_meta; promote_op(op, T)) +promote_eltype_op{T}(op, Ts::AbstractArray{DataType}, ::AbstractArray{T}) = typejoin([promote_op(op, S, T) for S in Ts]...) promote_eltype_op{R,S}(op, ::AbstractArray{R}, ::AbstractArray{S}) = (@_pure_meta; promote_op(op, R, S)) promote_eltype_op{R,S}(op, ::AbstractArray{R}, ::S) = (@_pure_meta; promote_op(op, R, S)) promote_eltype_op{R,S}(op, ::R, ::AbstractArray{S}) = (@_pure_meta; promote_op(op, R, S)) +promote_eltype_op{R,S}(op, ::Type{R}, ::AbstractArray{S}) = (@_pure_meta; promote_op(op, R, S)) +promote_eltype_op{R,S}(op, ::AbstractArray{R}, ::Type{S}) = (@_pure_meta; promote_op(op, S, R)) promote_eltype_op(op, A, B, C, D...) = (@_pure_meta; promote_op(op, eltype(A), promote_eltype_op(op, B, C, D...))) ## 1 argument diff --git a/base/broadcast.jl b/base/broadcast.jl index 0c0165fac300ea..8550485fa9eef2 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -13,8 +13,9 @@ export broadcast_getindex, broadcast_setindex! ## Calculate the broadcast shape of the arguments, or error if incompatible # array inputs broadcast_shape() = () +broadcast_shape(::Type) = () broadcast_shape(A) = shape(A) -@inline broadcast_shape(A, B...) = broadcast_shape((), shape(A), map(shape, B)...) +@inline broadcast_shape(A, B...) = broadcast_shape((), broadcast_shape(A), map(shape, B)...) # shape inputs broadcast_shape(shape::Tuple) = shape @inline broadcast_shape(shape::Tuple, shape1::Tuple, shapes::Tuple...) = broadcast_shape(_bcs((), shape, shape1), shapes...) @@ -40,7 +41,7 @@ _bcsm(a::Number, b::Number) = a == b || b == 1 ## Check that all arguments are broadcast compatible with shape # comparing one input against a shape check_broadcast_shape(shp) = nothing -check_broadcast_shape(shp, A) = check_broadcast_shape(shp, shape(A)) +check_broadcast_shape(shp, A) = check_broadcast_shape(shp, broadcast_shape(A)) check_broadcast_shape(::Tuple{}, ::Tuple{}) = nothing check_broadcast_shape(shp, ::Tuple{}) = nothing check_broadcast_shape(::Tuple{}, Ashp::Tuple) = throw(DimensionMismatch("cannot broadcast array to have fewer dimensions")) @@ -63,7 +64,7 @@ end @inline _newindex(out, I) = out # can truncate if indexmap is shorter than I @inline _newindex(out, I, keep::Bool, indexmap...) = _newindex((out..., ifelse(keep, I[1], 1)), tail(I), indexmap...) -newindexer(sz, x::Number) = () +newindexer(sz, x::Union{Number,Type}) = () @inline newindexer(sz, A) = _newindexer(sz, size(A)) @inline _newindexer(sz, szA::Tuple{}) = () @inline _newindexer(sz, szA) = (sz[1] == szA[1], _newindexer(tail(sz), tail(szA))...) @@ -79,6 +80,10 @@ const bitcache_size = 64 * bitcache_chunks # do not change this dumpbitcache(Bc::Vector{UInt64}, bind::Int, C::Vector{Bool}) = Base.copy_to_bitarray_chunks!(Bc, ((bind - 1) << 6) + 1, C, 1, min(bitcache_size, (length(Bc)-bind+1) << 6)) +# Since we can't make T[1] return T, use this inside `_broadcast!` +@inline _broadcast_getvals(A::Union{Number,Type}, I::Int) = A +@inline _broadcast_getvals(A, I) = A[I] + ## Broadcasting core # nargs encodes the number of As arguments (which matches the number # of indexmaps). The first two type parameters are to ensure specialization. @@ -92,7 +97,7 @@ dumpbitcache(Bc::Vector{UInt64}, bind::Int, C::Vector{Bool}) = # reverse-broadcast the indices @nexprs $nargs i->(I_i = newindex(I, imap_i)) # extract array values - @nexprs $nargs i->(@inbounds val_i = A_i[I_i]) + @nexprs $nargs i->(@inbounds val_i = _broadcast_getvals(A_i, I_i)) # call the function and store the result @inbounds B[I] = @ncall $nargs f val end diff --git a/base/float.jl b/base/float.jl index 6d507f2d6b34bd..d28de714a775a3 100644 --- a/base/float.jl +++ b/base/float.jl @@ -199,6 +199,17 @@ promote_rule(::Type{Float64}, ::Type{Float32}) = Float64 widen(::Type{Float16}) = Float32 widen(::Type{Float32}) = Float64 +promote_op{T<:Union{Float32,Float64}}(::typeof(trunc), ::Type{Signed}, ::Type{T}) = Int +promote_op{T<:Union{Float32,Float64}}(::typeof(trunc), ::Type{Unsigned}, ::Type{T}) = UInt +for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128) + for Tf in (Float32, Float64) + @eval promote_op(::$(typeof(trunc)), ::Type{$Ti}, ::Type{$Tf}) = $Ti + end +end +for f in (ceil, floor, round) + @eval promote_op{R,S}(::$(typeof(f)), ::Type{R}, ::Type{S}) = promote_op($trunc, R, S) +end + ## floating point arithmetic ## -(x::Float32) = box(Float32,neg_float(unbox(Float32,x))) -(x::Float64) = box(Float64,neg_float(unbox(Float64,x))) diff --git a/base/int.jl b/base/int.jl index 5322eceddd82e4..3cc2256db5558d 100644 --- a/base/int.jl +++ b/base/int.jl @@ -305,6 +305,7 @@ promote_rule{T<:BitSigned64}(::Type{UInt64}, ::Type{T}) = UInt64 promote_rule{T<:Union{UInt32, UInt64}}(::Type{T}, ::Type{Int128}) = Int128 promote_rule{T<:BitSigned}(::Type{UInt128}, ::Type{T}) = UInt128 +promote_op{T<:Integer}(op, ::Type{T}) = typeof(op(one(T))) promote_op{R<:Integer,S<:Integer}(op, ::Type{R}, ::Type{S}) = typeof(op(one(R), one(S))) ## traits ## diff --git a/base/parse.jl b/base/parse.jl index 1da9b742457e31..fa8ff6ef8e5899 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -194,3 +194,6 @@ function parse(str::AbstractString; raise::Bool=true) end return ex end + +promote_op{R<:AbstractFloat,S<:AbstractString}(::typeof(parse), ::Type{R}, ::Type{S}) = R +promote_op{R<:Integer,S<:Union{AbstractString,Char}}(::typeof(parse), ::Type{R}, ::Type{S}) = R diff --git a/base/promotion.jl b/base/promotion.jl index 7b6ebf0f406cb8..bfa3efc9c6fbb0 100644 --- a/base/promotion.jl +++ b/base/promotion.jl @@ -224,6 +224,7 @@ minmax(x::Real, y::Real) = minmax(promote(x, y)...) promote_op(::Any) = (@_pure_meta; Bottom) promote_op(::Any, T) = (@_pure_meta; T) promote_op{T}(::Type{T}, ::Any) = (@_pure_meta; T) +promote_op{R,S}(::typeof(convert), ::Type{R}, ::Type{S}) = (@_pure_meta; R) promote_op{R,S}(::Any, ::Type{R}, ::Type{S}) = (@_pure_meta; promote_type(R, S)) promote_op(op, T, S, U, V...) = (@_pure_meta; promote_op(op, T, promote_op(op, S, U, V...)))