Skip to content

Commit 4a140d2

Browse files
authored
Make op(Array, Scalar) use broadcast (#19692)
1 parent 546bfa3 commit 4a140d2

File tree

4 files changed

+7
-36
lines changed

4 files changed

+7
-36
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ This section lists changes that do not have deprecation warnings.
5757
The flip-side of this is that new method definitions should now reliably actually
5858
take effect, and be called when evaluating new code ([#265]).
5959

60+
* The array-scalar operations `div`, `mod`, `rem`, `&`, `|`, `xor`, `/`, `\`, `*`, `+`, and `-`
61+
now follow broadcast promotion rules ([#19692]).
62+
6063
Library improvements
6164
--------------------
6265

base/arraymath.jl

+2-34
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,10 @@ end
9191

9292
for f in (:div, :mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
9393
if f != :/
94-
@eval function ($f){T}(A::Number, B::AbstractArray{T})
95-
R = promote_op($f, typeof(A), T)
96-
S = promote_array_type($f, typeof(A), T, R)
97-
S === Any && return [($f)(A, b) for b in B]
98-
F = similar(B, S)
99-
RF, RB = eachindex(F), eachindex(B)
100-
if RF == RB
101-
for i in RB
102-
@inbounds F[i] = ($f)(A, B[i])
103-
end
104-
else
105-
for (iF, iB) in zip(RF, RB)
106-
@inbounds F[iF] = ($f)(A, B[iB])
107-
end
108-
end
109-
return F
110-
end
94+
@eval ($f){T}(A::Number, B::AbstractArray{T}) = broadcast($f, A, B)
11195
end
11296
if f != :\
113-
@eval function ($f){T}(A::AbstractArray{T}, B::Number)
114-
R = promote_op($f, T, typeof(B))
115-
S = promote_array_type($f, typeof(B), T, R)
116-
S === Any && return [($f)(a, B) for a in A]
117-
F = similar(A, S)
118-
RF, RA = eachindex(F), eachindex(A)
119-
if RF == RA
120-
for i in RA
121-
@inbounds F[i] = ($f)(A[i], B)
122-
end
123-
else
124-
for (iF, iA) in zip(RF, RA)
125-
@inbounds F[iF] = ($f)(A[iA], B)
126-
end
127-
end
128-
return F
129-
end
97+
@eval ($f){T}(A::AbstractArray{T}, B::Number) = broadcast($f, A, B)
13098
end
13199
end
132100

test/broadcast.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ let a = Real[2, 2.0, 4//2] / 2
186186
@test eltype(a) == Real
187187
end
188188
let a = Real[2, 2.0, 4//2] / 2.0
189-
@test eltype(a) == Real
189+
@test eltype(a) == Float64
190190
end
191191

192192
# issue 16164

test/linalg/lapack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ end
488488

489489
@testset "posv and some errors for friends" begin
490490
@testset for elty in (Float32, Float64, Complex64, Complex128)
491-
A = 0.01*rand(elty,10,10)
491+
A = rand(elty,10,10)/100
492492
A += real(diagm(10*real(rand(elty,10))))
493493
if elty <: Complex
494494
A = A + A'

0 commit comments

Comments
 (0)