Skip to content

Speed up some use cases of ArrayPartition #163

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

Merged
Merged
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
21 changes: 11 additions & 10 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ end
ArrayPartition(x...) = ArrayPartition((x...,))

function ArrayPartition(x::S, ::Type{Val{copy_x}}=Val{false}) where {S<:Tuple,copy_x}
T = promote_type(recursive_bottom_eltype.(x)...)
T = promote_type(map(recursive_bottom_eltype,x)...)
if copy_x
return ArrayPartition{T,S}(copy.(x))
return ArrayPartition{T,S}(map(copy,x))
else
return ArrayPartition{T,S}(x)
end
Expand Down Expand Up @@ -81,31 +81,31 @@ end
for op in (:+, :-)
@eval begin
function Base.$op(A::ArrayPartition, B::ArrayPartition)
Base.broadcast($op, A, B)
ArrayPartition(map((x, y)->Base.broadcast($op, x, y), A.x, B.x))
end

function Base.$op(A::ArrayPartition, B::Number)
Base.broadcast($op, A, B)
ArrayPartition(map(y->Base.broadcast($op, y, B), A.x))
end

function Base.$op(A::Number, B::ArrayPartition)
Base.broadcast($op, A, B)
ArrayPartition(map(y->Base.broadcast($op, A, y), B.x))
end
end
end

for op in (:*, :/)
@eval function Base.$op(A::ArrayPartition, B::Number)
Base.broadcast($op, A, B)
ArrayPartition(map(y->Base.broadcast($op, y, B), A.x))
end
end

function Base.:*(A::Number, B::ArrayPartition)
Base.broadcast(*, A, B)
ArrayPartition(map(y->Base.broadcast(*, A, y), B.x))
end

function Base.:\(A::Number, B::ArrayPartition)
Base.broadcast(/, B, A)
ArrayPartition(map(y->Base.broadcast(/, y, A), B.x))
end

Base.:(==)(A::ArrayPartition,B::ArrayPartition) = A.x == B.x
Expand Down Expand Up @@ -134,7 +134,7 @@ end
function Base.copyto!(A::ArrayPartition,src::ArrayPartition)
@assert length(src) == length(A)
if size.(A.x) == size.(src.x)
A .= src
map(copyto!, A.x, src.x)
else
cnt = 0
for i in eachindex(A.x)
Expand Down Expand Up @@ -281,9 +281,10 @@ end

@inline function Base.copyto!(dest::ArrayPartition, bc::Broadcast.Broadcasted{ArrayPartitionStyle{Style}}) where Style
N = npartitions(dest, bc)
@inbounds for i in 1:N
@inline function f(i)
copyto!(dest.x[i], unpack(bc, i))
end
ntuple(f, Val(N))
dest
end

Expand Down
2 changes: 1 addition & 1 deletion test/partitions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ _scalar_op(y) = y + 1
# Can't do `@inferred(_scalar_op.(x))` so we wrap that in a function:
_broadcast_wrapper(y) = _scalar_op.(y)
# Issue #8
# @inferred _broadcast_wrapper(x)
@inferred _broadcast_wrapper(x)

# Testing map
@test map(x->x^2, x) == ArrayPartition(x.x[1].^2, x.x[2].^2)
Expand Down