|
1 | 1 | # Conversion |
2 | 2 | # ---------- |
3 | | -function Base.convert(::Type{T}, t::AbstractBlockTensorMap) where {T <: TensorMap} |
4 | | - cod = ProductSpace{spacetype(t), numout(t)}(oplus.(codomain(t).spaces)) |
5 | | - dom = ProductSpace{spacetype(t), numin(t)}(oplus.(domain(t).spaces)) |
6 | | - |
| 3 | +function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap) |
| 4 | + S = spacetype(t) |
| 5 | + N₁, N₂ = numout(t), numin(t) |
| 6 | + cod = ProductSpace{S, N₁}(oplus.(codomain(t).spaces)) |
| 7 | + dom = ProductSpace{S, N₂}(oplus.(domain(t).spaces)) |
7 | 8 | tdst = similar(t, cod ← dom) |
8 | | - for (f₁, f₂) in fusiontrees(tdst) |
9 | | - tdst[f₁, f₂] .= t[f₁, f₂] |
10 | | - end |
11 | 9 |
|
12 | | - return convert(T, tdst) |
13 | | -end |
14 | | -# disambiguate |
15 | | -function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap) |
16 | | - cod = ProductSpace{spacetype(t), numout(t)}(oplus.(codomain(t).spaces)) |
17 | | - dom = ProductSpace{spacetype(t), numin(t)}(oplus.(domain(t).spaces)) |
| 10 | + issparse(t) && zerovector!(tdst) |
18 | 11 |
|
19 | | - tdst = similar(t, cod ← dom) |
20 | | - for (f₁, f₂) in fusiontrees(tdst) |
21 | | - copyto!(tdst[f₁, f₂], t[f₁, f₂]) |
| 12 | + for ((f₁, f₂), arr) in subblocks(tdst) |
| 13 | + blockax = ntuple(N₁ + N₂) do i |
| 14 | + return if i <= N₁ |
| 15 | + blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(t, i))) |
| 16 | + else |
| 17 | + blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(t, i)')) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + for (k, v) in nonzero_pairs(t) |
| 22 | + indices = getindex.(blockax, Block.(Tuple(k))) |
| 23 | + copy!(arr[indices...], v[f₁, f₂]) |
| 24 | + end |
22 | 25 | end |
23 | 26 |
|
24 | 27 | return tdst |
25 | 28 | end |
26 | 29 |
|
| 30 | +function Base.convert(::Type{T}, t::AbstractBlockTensorMap) where {T <: TensorMap} |
| 31 | + tdst = convert(TensorMap, t) |
| 32 | + return convert(T, tdst) |
| 33 | +end |
| 34 | + |
27 | 35 | function Base.convert(::Type{TT}, t::AbstractTensorMap) where {TT <: AbstractBlockTensorMap} |
28 | 36 | t isa TT && return t |
29 | 37 | if t isa AbstractBlockTensorMap |
|
0 commit comments