Skip to content

Commit

Permalink
Make op(Array, Scalar) use broadcast (#19692)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack authored Dec 28, 2016
1 parent 546bfa3 commit 4a140d2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ This section lists changes that do not have deprecation warnings.
The flip-side of this is that new method definitions should now reliably actually
take effect, and be called when evaluating new code ([#265]).

* The array-scalar operations `div`, `mod`, `rem`, `&`, `|`, `xor`, `/`, `\`, `*`, `+`, and `-`
now follow broadcast promotion rules ([#19692]).

Library improvements
--------------------

Expand Down
36 changes: 2 additions & 34 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,10 @@ end

for f in (:div, :mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
if f != :/
@eval function ($f){T}(A::Number, B::AbstractArray{T})
R = promote_op($f, typeof(A), T)
S = promote_array_type($f, typeof(A), T, R)
S === Any && return [($f)(A, b) for b in B]
F = similar(B, S)
RF, RB = eachindex(F), eachindex(B)
if RF == RB
for i in RB
@inbounds F[i] = ($f)(A, B[i])
end
else
for (iF, iB) in zip(RF, RB)
@inbounds F[iF] = ($f)(A, B[iB])
end
end
return F
end
@eval ($f){T}(A::Number, B::AbstractArray{T}) = broadcast($f, A, B)
end
if f != :\
@eval function ($f){T}(A::AbstractArray{T}, B::Number)
R = promote_op($f, T, typeof(B))
S = promote_array_type($f, typeof(B), T, R)
S === Any && return [($f)(a, B) for a in A]
F = similar(A, S)
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
@inbounds F[i] = ($f)(A[i], B)
end
else
for (iF, iA) in zip(RF, RA)
@inbounds F[iF] = ($f)(A[iA], B)
end
end
return F
end
@eval ($f){T}(A::AbstractArray{T}, B::Number) = broadcast($f, A, B)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ let a = Real[2, 2.0, 4//2] / 2
@test eltype(a) == Real
end
let a = Real[2, 2.0, 4//2] / 2.0
@test eltype(a) == Real
@test eltype(a) == Float64
end

# issue 16164
Expand Down
2 changes: 1 addition & 1 deletion test/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ end

@testset "posv and some errors for friends" begin
@testset for elty in (Float32, Float64, Complex64, Complex128)
A = 0.01*rand(elty,10,10)
A = rand(elty,10,10)/100
A += real(diagm(10*real(rand(elty,10))))
if elty <: Complex
A = A + A'
Expand Down

0 comments on commit 4a140d2

Please sign in to comment.