Skip to content

Commit c2007b7

Browse files
Merge pull request #386 from SciML/scalar_arraypartition
Fix copyto! overload for arraypartitions of scalars
2 parents 80a2a24 + efa84b2 commit c2007b7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/array_partition.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ for type in [AbstractArray, SparseArrays.AbstractCompressedVector, PermutedDimsA
181181
@assert length(dest) == length(A)
182182
cur = 1
183183
@inbounds for i in 1:length(A.x)
184-
dest[cur:(cur + length(A.x[i]) - 1)] .= vec(A.x[i])
184+
if A.x[i] isa Number
185+
dest[cur:(cur + length(A.x[i]) - 1)] .= A.x[i]
186+
else
187+
dest[cur:(cur + length(A.x[i]) - 1)] .= vec(A.x[i])
188+
end
185189
cur += length(A.x[i])
186190
end
187191
dest

test/partitions_test.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ begin
266266
end
267267

268268
@testset "Copy and zero with type changing array" begin
269-
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
269+
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
270270
struct TypeChangingArray{T, N} <: AbstractArray{T, N} end
271271
Base.copy(::TypeChangingArray{T, N}) where {T, N} = Array{T, N}(undef,
272272
ntuple(_ -> 0, N))
@@ -281,3 +281,9 @@ end
281281
@testset "Cartesian indexing" begin
282282
@test ArrayPartition([1, 2], [3])[1:3, 1] == [1, 2, 3]
283283
end
284+
285+
@testset "Scalar copyto!" begin
286+
u = [2.0,1.0]
287+
copyto!(u, ArrayPartition(1.0,-1.2))
288+
@test u == [1.0,-1.2]
289+
end

0 commit comments

Comments
 (0)