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

Use map instead of broadcast for Array*/Number and Matrix+-Matrix. #22208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ julia> A
2-2im 3-1im
```
"""
conj!(A::AbstractArray{<:Number}) = (@inbounds broadcast!(conj, A, A); A)
conj!(A::AbstractArray{<:Number}) = (@inbounds map!(conj, A, A); A)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
conj!(A::AbstractArray{<:Number}) = (@inbounds map!(conj, A, A); A)
conj!(A::AbstractArray{<:Number}) = (map!(conj, A, A); A)

This caller doesn't know anything more than map! does (and I don't think map! uses a @boundscheck in any case).


for f in (:-, :conj, :real, :imag)
@eval ($f)(A::AbstractArray) = broadcast_preserving_zero_d($f, A)
Expand All @@ -36,23 +36,23 @@ 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)
return map($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...)
return map(+, A, Bs...)
end

for f in (:/, :\, :*)
if f !== :/
@eval ($f)(A::Number, B::AbstractArray) = broadcast_preserving_zero_d($f, A, B)
@eval ($f)(x::Number, A::AbstractArray) = map(t -> ($f)(x, t), A)
end
if f !== :\
@eval ($f)(A::AbstractArray, B::Number) = broadcast_preserving_zero_d($f, A, B)
@eval ($f)(A::AbstractArray, x::Number) = map(t -> ($f)(t, x), A)
end
end

Expand Down
20 changes: 20 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2815,3 +2815,23 @@ end
@test String(take!(b)) ==
"BoundsError: attempt to access 2×2 Array{Float64,2} at index [10, \"bad index\"]"
end

struct T22208 <: DenseMatrix{Float64}
A::Matrix{Float64}
end
Base.size(A::T22208) = size(A.A)
Base.map(f, A::T22208) = T22208(map(f, A.A))
Base.map(f, A::T22208, B::T22208) = T22208(map(f, A.A, B.A))
@testset "test that binary ops for custom arrays call map" begin
A = T22208(randn(3,3))
x = randn()

@test (x * A).A == x * A.A
@test (x \ A).A == x \ A.A

@test (A * x).A == A.A * x
@test (A / x).A == A.A / x

@test (A + A).A == A.A + A.A
@test (A - A).A == A.A - A.A
end