Skip to content

Fix to copyto!(::ArrayPartition,::ArrayPartition) and copyto!(::Array,::ArrayPartition) #69

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 3 commits into from
Aug 7, 2019
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: 14 additions & 7 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,29 @@ end
Base.mapreduce(f,op,A::ArrayPartition) = mapreduce(f,op,(mapreduce(f,op,x) for x in A.x))
Base.any(f,A::ArrayPartition) = any(f,(any(f,x) for x in A.x))
Base.any(f::Function,A::ArrayPartition) = any(f,(any(f,x) for x in A.x))
function Base.copyto!(dest::Array,A::ArrayPartition)
function Base.copyto!(dest::AbstractArray,A::ArrayPartition)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this seems more general

@assert length(dest) == length(A)
cur = 1
@inbounds for i in 1:length(A.x)
dest[cur:(cur+length(A.x[i])-1)] .= A.x[i]
cur += length(A.x[i])
dest[cur:(cur+length(A.x[i])-1)] .= vec(A.x[i])
cur += length(A.x[i])
end
dest
end

function Base.copyto!(A::ArrayPartition,src::ArrayPartition)
@assert length(src) == length(A)
cur = 1
@inbounds for i in 1:length(A.x)
A.x[i] .= @view(src[cur:(cur+length(A.x[i])-1)])
cur += length(A.x[i])
if size.(A.x) == size.(src.x)
Copy link
Member

Choose a reason for hiding this comment

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

does this need if all(...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to be working without all... I guess when using all you would need .==?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems we have to use size.(A.x) == size.(src.x). If partition don't agree this can happen:

julia> x = ArrayPartition(randn(3,2),randn(2)); y = ArrayPartition(randn(3),randn(3),randn(2));

julia> all(size.(x.x) .== size.(y.x))
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")

A .= src
else
cnt = 0
for i in eachindex(A.x)
x = A.x[i]
for k in eachindex(x)
cnt += 1
x[k] = src[cnt]
end
end
end
A
end
Expand Down
19 changes: 19 additions & 0 deletions test/partitions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ _scalar_op(y) = y + 1
_broadcast_wrapper(y) = _scalar_op.(y)
# Issue #8
# @inferred _broadcast_wrapper(x)

#### testing copyto!
S = [
((1,),(2,)) => ((1,),(2,)),
((3,2),(2,)) => ((3,2),(2,)),
((3,2),(2,)) => ((3,),(3,),(2,))
]

for sizes in S
x = ArrayPartition( randn.(sizes[1]) )
y = ArrayPartition( zeros.(sizes[2]) )
y_array = zeros(length(x))
copyto!(y,x) #testing Base.copyto!(dest::ArrayPartition,A::ArrayPartition)
copyto!(y_array,x) #testing Base.copyto!(dest::Array,A::ArrayPartition)
@test all([x[i] == y[i] for i in eachindex(x)])
@test all([x[i] == y_array[i] for i in eachindex(x)])
end