Skip to content

Commit 5aaf060

Browse files
feat: Preserve Indices When Copying Tracked Arrays (#263)
1 parent 554a9c0 commit 5aaf060

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/similar_convert_copy.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ end
5555
function Base.convert(::Type{ComponentArray{T1,N,A1,Ax1}}, x::ComponentArray{T2,N,A2,Ax2}) where {T1,T2,N,A1,A2,Ax1,Ax2}
5656
return T1.(x)
5757
end
58+
function Base.convert(::Type{ComponentArray{T,N,A1,Ax}}, x::ComponentArray{T,N,A2,Ax}) where {T,N,A1,A2,Ax}
59+
return x
60+
end
61+
function Base.convert(::Type{ComponentArray{T,N,A,Ax}}, x::ComponentArray{T,N,A,Ax}) where {T,N,A,Ax}
62+
return x
63+
end
5864
Base.convert(T::Type{<:Array}, x::ComponentArray) = convert(T, getdata(x))
5965

6066
Base.convert(::Type{Cholesky{T1,Matrix{T1}}}, x::Cholesky{T2,<:ComponentArray}) where {T1,T2} = Cholesky(Matrix{T1}(x.factors), x.uplo, x.info)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ComponentArrays
22
using BenchmarkTools
33
using ForwardDiff
4+
using Tracker
45
using InvertedIndices
56
using LabelledArrays
67
using LinearAlgebra
@@ -400,6 +401,10 @@ end
400401

401402
@test convert(Array, ca) == getdata(ca)
402403
@test convert(Matrix{Float32}, cmat) isa Matrix{Float32}
404+
405+
tr = Tracker.param(ca)
406+
ca_ = convert(typeof(ca), tr)
407+
@test ca_.a == ca.a
403408
end
404409

405410
@testset "Broadcasting" begin

0 commit comments

Comments
 (0)