Skip to content

Fix NamedArrayPartition similar when size is passed #433

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

Open
wants to merge 2 commits 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
16 changes: 12 additions & 4 deletions src/named_array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ end

# return ArrayPartition when possible, otherwise next best thing of the correct size
function Base.similar(A::NamedArrayPartition, dims::NTuple{N, Int}) where {N}
NamedArrayPartition(
similar(getfield(A, :array_partition), dims), getfield(A, :names_to_indices))
similar_array_partition = similar(getfield(A, :array_partition), dims)
if similar_array_partition isa ArrayPartition
NamedArrayPartition(similar_array_partition, getfield(A, :names_to_indices))
else
similar_array_partition
end
end

# similar array partition of common type
Expand All @@ -45,8 +49,12 @@ end

# return ArrayPartition when possible, otherwise next best thing of the correct size
function Base.similar(A::NamedArrayPartition, ::Type{T}, dims::NTuple{N, Int}) where {T, N}
NamedArrayPartition(
similar(getfield(A, :array_partition), T, dims), getfield(A, :names_to_indices))
similar_array_partition = similar(getfield(A, :array_partition), T, dims)
if similar_array_partition isa ArrayPartition
NamedArrayPartition(similar_array_partition, getfield(A, :names_to_indices))
else
similar_array_partition
end
end

# similar array partition with different types
Expand Down
2 changes: 2 additions & 0 deletions test/named_array_partition_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using RecursiveArrayTools, Test
@test typeof(x .^ 2) <: NamedArrayPartition
@test typeof(similar(x)) <: NamedArrayPartition
@test typeof(similar(x, Int)) <: NamedArrayPartition
@test typeof(similar(x, (5, 5))) <: Matrix
@test typeof(similar(x, Int, (5, 5))) <: Matrix
@test x.a ≈ ones(10)
@test typeof(x .+ x[1:end]) <: Vector # test broadcast precedence
@test all(x .== x[1:end])
Expand Down
Loading