Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make op(Array, Scalar) use broadcast #19692

Merged
merged 1 commit into from
Dec 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
--------------------

36 changes: 2 additions & 34 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/linalg/lapack.jl
Original file line number Diff line number Diff line change
@@ -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'