Skip to content

Commit a8d0e00

Browse files
committed
Rework promote_array_type
1 parent 66f9db4 commit a8d0e00

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

base/arraymath.jl

+16-17
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ function !(A::AbstractArray{Bool})
3535
end
3636

3737
## Binary arithmetic operators ##
38-
39-
promote_array_type{Scalar, Arry}(F, ::Type{Scalar}, ::Type{Arry}) = promote_op(F, Scalar, Arry)
40-
promote_array_type{S<:Real, A<:AbstractFloat}(F, ::Type{S}, ::Type{A}) = A
41-
promote_array_type{S<:Integer, A<:Integer}(F, ::Type{S}, ::Type{A}) = A
42-
promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}) = S
43-
promote_array_type(F, ::Type{Bool}, ::Type{Bool}) = promote_op(F, Bool, Bool)
44-
45-
# Handle operations that return different types
46-
for f in (:./, :.\, :.^)
47-
@eval ($f)(x::Number, Y::AbstractArray) =
48-
reshape((promote_eltype_op($f, x, Y))[ ($f)(x, y) for y in Y ], size(Y))
49-
@eval ($f)(X::AbstractArray, y::Number) =
50-
reshape((promote_eltype_op($f, X, y))[ ($f)(x, y) for x in X ], size(X))
51-
end
38+
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+
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))
42+
43+
promote_array_type{S, A, P}(F, ::Type{S}, ::Type{A}, ::Type{P}) = P
44+
promote_array_type{S<:Real, A<:AbstractFloat, P}(F, ::Type{S}, ::Type{A}, ::Type{P}) = A
45+
promote_array_type{S<:Integer, A<:Integer, P}(F::typeof(./), ::Type{S}, ::Type{A}, ::Type{P}) = P
46+
promote_array_type{S<:Integer, A<:Integer, P}(F::typeof(.\), ::Type{S}, ::Type{A}, ::Type{P}) = P
47+
promote_array_type{S<:Integer, A<:Integer, P}(F, ::Type{S}, ::Type{A}, ::Type{P}) = A
48+
promote_array_type{S<:Integer, P}(F::typeof(./), ::Type{S}, ::Type{Bool}, ::Type{P}) = P
49+
promote_array_type{S<:Integer, P}(F::typeof(.\), ::Type{S}, ::Type{Bool}, ::Type{P}) = P
50+
promote_array_type{S<:Integer, P}(F, ::Type{S}, ::Type{Bool}, ::Type{P}) = P
5251

5352
for f in (:+, :-, :div, :mod, :&, :|, :$)
5453
@eval begin
@@ -82,17 +81,17 @@ for f in (:+, :-, :div, :mod, :&, :|, :$)
8281
end
8382
end
8483
end
85-
for f in (:.+, :.-, :.*, :, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
84+
for f in (:.+, :.-, :.*, :./, :.\, :.^, :.÷, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
8685
@eval begin
8786
function ($f){T}(A::Number, B::AbstractArray{T})
88-
F = similar(B, promote_array_type($f,typeof(A),T))
87+
F = similar(B, promote_array_type($f,typeof(A),typeof(B)))
8988
for (iF, iB) in zip(eachindex(F), eachindex(B))
9089
@inbounds F[iF] = ($f)(A, B[iB])
9190
end
9291
return F
9392
end
9493
function ($f){T}(A::AbstractArray{T}, B::Number)
95-
F = similar(A, promote_array_type($f,typeof(B),T))
94+
F = similar(A, promote_array_type($f,typeof(A),typeof(B)))
9695
for (iF, iA) in zip(eachindex(F), eachindex(A))
9796
@inbounds F[iF] = ($f)(A[iA], B)
9897
end

base/bitarray.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1045,9 +1045,9 @@ for f in (:+, :-)
10451045
end
10461046
for (f) in (:.+, :.-)
10471047
for (arg1, arg2, T, fargs) in ((:(B::BitArray), :(x::Bool) , Int , :(b, x)),
1048-
(:(B::BitArray), :(x::Number) , :(promote_array_type($f, typeof(x), Bool)), :(b, x)),
1048+
(:(B::BitArray), :(x::Number) , :(promote_array_type($f, BitArray, typeof(x))), :(b, x)),
10491049
(:(x::Bool) , :(B::BitArray), Int , :(x, b)),
1050-
(:(x::Number) , :(B::BitArray), :(promote_array_type($f, typeof(x), Bool)), :(x, b)))
1050+
(:(x::Number) , :(B::BitArray), :(promote_array_type($f, typeof(x), BitArray)), :(x, b)))
10511051
@eval function ($f)($arg1, $arg2)
10521052
r = Array($T, size(B))
10531053
bi = start(B)
@@ -1086,7 +1086,7 @@ function div(x::Bool, B::BitArray)
10861086
end
10871087
function div(x::Number, B::BitArray)
10881088
all(B) || throw(DivideError())
1089-
pt = promote_array_type(div, typeof(x), Bool)
1089+
pt = promote_array_type(div, typeof(x), BitArray)
10901090
y = div(x, true)
10911091
reshape(pt[ y for i = 1:length(B) ], size(B))
10921092
end
@@ -1107,15 +1107,15 @@ function mod(x::Bool, B::BitArray)
11071107
end
11081108
function mod(x::Number, B::BitArray)
11091109
all(B) || throw(DivideError())
1110-
pt = promote_array_type(mod, typeof(x), Bool)
1110+
pt = promote_array_type(mod, typeof(x), BitArray)
11111111
y = mod(x, true)
11121112
reshape(pt[ y for i = 1:length(B) ], size(B))
11131113
end
11141114

11151115
for f in (:div, :mod)
11161116
@eval begin
11171117
function ($f)(B::BitArray, x::Number)
1118-
F = Array(promote_array_type($f, typeof(x), Bool), size(B))
1118+
F = Array(promote_array_type($f, BitArray, typeof(x)), size(B))
11191119
for i = 1:length(F)
11201120
F[i] = ($f)(B[i], x)
11211121
end

base/complex.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ big{T<:AbstractFloat,N}(A::AbstractArray{Complex{T},N}) = convert(AbstractArray{
807807

808808
## promotion to complex ##
809809

810-
promote_array_type{S<:Union{Complex, Real}, AT<:AbstractFloat}(F, ::Type{S}, ::Type{Complex{AT}}) = Complex{AT}
810+
promote_array_type{S<:Union{Complex, Real}, AT<:AbstractFloat, P}(F, ::Type{S}, ::Type{Complex{AT}}, ::Type{P}) = Complex{AT}
811811

812812
function complex{S<:Real,T<:Real}(A::AbstractArray{S}, B::AbstractArray{T})
813813
if size(A) != size(B); throw(DimensionMismatch()); end

0 commit comments

Comments
 (0)