Skip to content

Commit 369f974

Browse files
committed
Add convert to ArrayPartition
1 parent cb2b6df commit 369f974

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/array_partition.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,9 @@ function LinearAlgebra.mul!(C::ArrayPartition, A::ArrayPartition, B::ArrayPartit
497497
end
498498
return C
499499
end
500+
501+
function Base.convert(::Type{ArrayPartition{T,S}}, A::ArrayPartition{<:Any,<:NTuple{N,Any}}) where {N, T, S<:NTuple{N,Any}}
502+
return ArrayPartition{T,S}(
503+
ntuple((@inline i -> convert(S.parameters[i], A.x[i])), Val(N))
504+
)
505+
end

test/partitions_test.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ end
197197
x = ArrayPartition(ArrayPartition([1, 2]), [3, 4])
198198
@test -x == 0-x
199199
@test typeof(x) === typeof(-x)
200+
201+
# Test conversions
202+
begin
203+
b = [1, 2, 3]
204+
c = [1 2; 3 4]
205+
d = ArrayPartition(view(b, :), c)
206+
207+
new_type = ArrayPartition{Float64,Tuple{Vector{Float64},Matrix{Float64}}}
208+
@test (@inferred convert(new_type, d)) isa new_type
209+
@test convert(new_type, d) == d
210+
@test_throws MethodError convert(new_type, ArrayPartition(view(b, :), c, c))
211+
end

0 commit comments

Comments
 (0)