|
1 | 1 | # Conversion |
2 | 2 | # ---------- |
3 | 3 |
|
4 | | -function _copy_subblocks!(tdst, tsrc) |
5 | | - N₁, N₂ = numout(tsrc), numin(tsrc) |
6 | | - offsets = ntuple(i -> _sumspace_offsets(i <= N₁ ? codomain(tsrc)[i] : domain(tsrc)[i - N₁]), N₁ + N₂) |
7 | | - # a single `SubblockIterator` holds the structure: `tdst[f₁, f₂]` would look it up in TensorKit's locked cache every time |
8 | | - dstblocks = subblocks(tdst) |
9 | | - for (k, v) in nonzero_pairs(tsrc), ((f₁, f₂), src) in subblocks(v) |
10 | | - ranges = map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(k)) do o, c, kᵢ |
11 | | - return (o[c][kᵢ] + 1):o[c][kᵢ + 1] |
| 4 | +function _subblock_ranges(offsets, (f₁, f₂), I::CartesianIndex) |
| 5 | + return map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(I)) do o, c, kᵢ |
| 6 | + return (o[c][kᵢ] + 1):o[c][kᵢ + 1] |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +function _subblock_pairs(t::AbstractTensorMap) |
| 11 | + sectortype(t) === Trivial || return subblocks(t) |
| 12 | + f = TK.trivial_fusiontree(t) |
| 13 | + return (f => subblock(t, f),) |
| 14 | +end |
| 15 | +function _subblock_getter(t::AbstractTensorMap) |
| 16 | + return sectortype(t) === Trivial ? Base.Fix1(subblock, t) : Base.Fix1(getindex, subblocks(t)) |
| 17 | +end |
| 18 | + |
| 19 | +function _copy_subblocks!(tdst::TensorMap, tsrc::AbstractBlockTensorMap) |
| 20 | + offsets = map(_sumspace_offsets, eachspace(tsrc).sumspaces) |
| 21 | + dst = _subblock_getter(tdst) |
| 22 | + for (I, v) in nonzero_pairs(tsrc), (f, src) in _subblock_pairs(v) |
| 23 | + copy!(view(dst(f), _subblock_ranges(offsets, f, I)...), src) |
| 24 | + end |
| 25 | + return tdst |
| 26 | +end |
| 27 | + |
| 28 | +function _copy_subblocks!(tdst::AbstractBlockTensorMap, tsrc::AbstractTensorMap) |
| 29 | + offsets = map(_sumspace_offsets, eachspace(tdst).sumspaces) |
| 30 | + src = _subblock_getter(tsrc) |
| 31 | + for (I, V) in pairs(eachspace(tdst)) |
| 32 | + v = similar(eltype(tdst), V) |
| 33 | + vblocks = _subblock_pairs(v) |
| 34 | + issparse(tdst) && all(isempty ∘ last, vblocks) && continue |
| 35 | + for (f, dst) in vblocks |
| 36 | + copy!(dst, view(src(f), _subblock_ranges(offsets, f, I)...)) |
12 | 37 | end |
13 | | - copy!(dstblocks[(f₁, f₂)][ranges...], src) |
| 38 | + @inbounds tdst[I] = v |
14 | 39 | end |
15 | 40 | return tdst |
16 | 41 | end |
|
0 commit comments