Skip to content
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
27 changes: 23 additions & 4 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,45 @@

## Binary arithmetic operators ##

function _broadcast_preserving_zero_d(f, A, B)
broadcast_preserving_zero_d(f, A, B)
end

# Using map over broadcast enables vectorization for wide matrices with few rows.
# This is because we use linear indexing in `map` as opposed to Cartesian indexing in broadcasting.
# https://github.com/JuliaLang/julia/issues/47873#issuecomment-1352472461
function _broadcast_preserving_zero_d(f, A::Array, B::Array)
map(f, A, B)
end

function _broadcast_preserving_zero_d(f, A::Array, B::Number)
map(Fix2(f, B), A)
end

function _broadcast_preserving_zero_d(f, A::Number, B::Array)
map(Fix1(f, A), B)
end

for f in (:+, :-)
@eval function ($f)(A::AbstractArray, B::AbstractArray)
promote_shape(A, B) # check size compatibility
broadcast_preserving_zero_d($f, A, B)
_broadcast_preserving_zero_d($f, A, B)
end
end

function +(A::Array, Bs::Array...)
for B in Bs
promote_shape(A, B) # check size compatibility
end
broadcast_preserving_zero_d(+, A, Bs...)
map(+, A, Bs...)
end

for f in (:/, :\, :*)
if f !== :/
@eval ($f)(A::Number, B::AbstractArray) = broadcast_preserving_zero_d($f, A, B)
@eval ($f)(A::Number, B::AbstractArray) = _broadcast_preserving_zero_d($f, A, B)
end
if f !== :\
@eval ($f)(A::AbstractArray, B::Number) = broadcast_preserving_zero_d($f, A, B)
@eval ($f)(A::AbstractArray, B::Number) = _broadcast_preserving_zero_d($f, A, B)
end
end

Expand Down
Loading