@@ -35,30 +35,26 @@ function !(A::AbstractArray{Bool})
3535end
3636
3737# # Binary arithmetic operators ##
38- @pure promote_array_type {S<:Number, A<:AbstractArray} (F, :: Type{S} , :: Type{A} ) =
39- promote_array_type (F, S, eltype (A), promote_op (F, S, eltype (A)))
40- @pure promote_array_type {S<:Number, A<:AbstractArray} (F, :: Type{A} , :: Type{S} ) =
41- promote_array_type (F, S, eltype (A), promote_op (F, eltype (A), S))
4238
43- @pure promote_array_type {S, A, P} (F, :: Type{S} , :: Type{A} , :: Type{P} ) = P
44- @pure promote_array_type {S<:Real, A<:AbstractFloat, P } (F, :: Type{S} , :: Type{A} , :: Type{P} ) = A
45- @pure promote_array_type {S<:Integer, A<:Integer, P } (F:: typeof ( ./ ) , :: Type{S} , :: Type{A} , :: Type{P} ) = P
46- @pure promote_array_type {S<:Integer, A<:Integer, P} (F :: typeof (.\ ), :: Type{S} , :: Type{A} , :: Type{P} ) = P
47- @pure promote_array_type {S<:Integer, A<:Integer, P} (F , :: Type{S} , :: Type{A} , :: Type{P} ) = A
48- @pure promote_array_type {S<:Integer, P} (F :: typeof (./ ), :: Type{S} , :: Type{Bool} , :: Type{P} ) = P
49- @pure promote_array_type {S<:Integer, P} (F :: typeof (.\ ), :: Type{S} , :: Type{Bool} , :: Type{P} ) = P
50- @pure promote_array_type {S<:Integer, P } (F, :: Type{S} , :: Type{Bool} , :: Type{P} ) = P
39+ promote_array_type (F, :: Type , :: Type , T :: Type ) = T
40+ promote_array_type {S<:Real, A<:AbstractFloat} (F, :: Type{S} , :: Type{A} , :: Type ) = A
41+ promote_array_type {S<:Integer, A<:Integer} (F, :: Type{S} , :: Type{A} , :: Type ) = A
42+ promote_array_type {S<:Integer, A<:Integer} ( :: typeof (./ ), :: Type{S} , :: Type{A} , T :: Type ) = T
43+ promote_array_type {S<:Integer, A<:Integer} ( :: typeof ( .\ ) , :: Type{S} , :: Type{A} , T :: Type ) = T
44+ promote_array_type {S<:Integer} ( :: typeof (./ ), :: Type{S} , :: Type{Bool} , T :: Type ) = T
45+ promote_array_type {S<:Integer} ( :: typeof (.\ ), :: Type{S} , :: Type{Bool} , T :: Type ) = T
46+ promote_array_type {S<:Integer} (F, :: Type{S} , :: Type{Bool} , T :: Type ) = T
5147
5248for f in (:+ , :- , :div , :mod , :& , :| , :$ )
53- @eval ($ f){S,T} (A:: AbstractArray{S} , B:: AbstractArray{T} ) =
54- _elementwise ($ f, A, B, promote_eltype_op ( $ f , A, B) )
49+ @eval ($ f)(A:: AbstractArray , B:: AbstractArray ) =
50+ _elementwise ($ f, promote_op ( $ f, eltype (A), eltype (B)) , A, B)
5551end
56- function _elementwise {S,T} (op, A :: AbstractArray{S } , B :: AbstractArray{T} , :: Type{Any} )
57- promote_shape (A,B) # check size compatibility
52+ function _elementwise (op, :: Type{Any } , A :: AbstractArray , B :: AbstractArray )
53+ promote_shape (A, B) # check size compatibility
5854 return broadcast (op, A, B)
5955end
60- function _elementwise {S,T,R} (op, A :: AbstractArray{S} , B :: AbstractArray{T} , :: Type{R} )
61- F = similar (A, R , promote_shape (A,B))
56+ function _elementwise (op, T :: Type , A :: AbstractArray , B :: AbstractArray )
57+ F = similar (A, T , promote_shape (A, B))
6258 for (iF, iA, iB) in zip (eachindex (F), eachindex (A), eachindex (B))
6359 @inbounds F[iF] = op (A[iA], B[iB])
6460 end
6763
6864for f in (:.+ , :.- , :.* , :./ , :.\ , :.^ , :.÷ , :.% , :.<< , :.>> , :div , :mod , :rem , :& , :| , :$ )
6965 @eval begin
70- function ($ f){T}(A:: Number , B:: AbstractArray{T} )
71- F = similar (B, promote_array_type ($ f,typeof (A),typeof (B)))
66+ function ($ f)(A:: Number , B:: AbstractArray )
67+ P = promote_op ($ f, typeof (A), eltype (B))
68+ T = promote_array_type ($ f, typeof (A), eltype (B), P)
69+ T === Any && return [($ f)(A, b) for b in B]
70+ F = similar (B, T)
7271 for (iF, iB) in zip (eachindex (F), eachindex (B))
7372 @inbounds F[iF] = ($ f)(A, B[iB])
7473 end
7574 return F
7675 end
77- function ($ f){T}(A:: AbstractArray{T} , B:: Number )
78- F = similar (A, promote_array_type ($ f,typeof (A),typeof (B)))
76+ function ($ f)(A:: AbstractArray , B:: Number )
77+ P = promote_op ($ f, eltype (A), typeof (B))
78+ T = promote_array_type ($ f, typeof (B), eltype (A), P)
79+ T === Any && return [($ f)(a, B) for a in A]
80+ F = similar (A, T)
7981 for (iF, iA) in zip (eachindex (F), eachindex (A))
8082 @inbounds F[iF] = ($ f)(A[iA], B)
8183 end
0 commit comments