|
1 | 1 | # Conversion |
2 | 2 | # ---------- |
3 | 3 |
|
4 | | -function _copy_subblocks!(tdst, tsrc) |
5 | | - S = spacetype(tsrc) |
6 | | - N₁, N₂ = numout(tsrc), numin(tsrc) |
7 | | - for ((f₁, f₂), arr) in subblocks(tdst) |
8 | | - blockax = ntuple(N₁ + N₂) do i |
9 | | - return if i <= N₁ |
10 | | - blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(tsrc, i))) |
11 | | - else |
12 | | - blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(tsrc, i)')) |
13 | | - end |
14 | | - end |
| 4 | +function _subblock_offsets(W::TensorMapSumSpace) |
| 5 | + return map(eachspace(W).sumspaces) do V |
| 6 | + sectortype(V) === Trivial && return Returns([0; cumsum(dims(V))]) |
| 7 | + offsets = Dict(s => [0; cumsum(map(Base.Fix2(dim, s), V.spaces))] for s in sectors(V)) |
| 8 | + return Base.Fix1(getindex, offsets) |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +function _subblock_ranges(offsets, (f₁, f₂), I::CartesianIndex) |
| 13 | + return map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(I)) do o, s, j |
| 14 | + r = o(s) |
| 15 | + return (r[j] + 1):r[j + 1] |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +function _subblock_pairs(t::AbstractTensorMap) |
| 20 | + sectortype(t) === Trivial || return subblocks(t) |
| 21 | + f = TK.trivial_fusiontree(t) |
| 22 | + return (f => subblock(t, f),) |
| 23 | +end |
| 24 | +function _subblock_getter(t::AbstractTensorMap) |
| 25 | + return sectortype(t) === Trivial ? Base.Fix1(subblock, t) : Base.Fix1(getindex, subblocks(t)) |
| 26 | +end |
| 27 | + |
| 28 | +function _copy_subblocks!(tdst::TensorMap, tsrc::AbstractBlockTensorMap) |
| 29 | + offsets = _subblock_offsets(space(tsrc)) |
| 30 | + dst = _subblock_getter(tdst) |
| 31 | + for (I, v) in nonzero_pairs(tsrc), (f, src) in _subblock_pairs(v) |
| 32 | + copy!(view(dst(f), _subblock_ranges(offsets, f, I)...), src) |
| 33 | + end |
| 34 | + return tdst |
| 35 | +end |
15 | 36 |
|
16 | | - for (k, v) in nonzero_pairs(tsrc) |
17 | | - indices = getindex.(blockax, Block.(Tuple(k))) |
18 | | - arr_slice = arr[indices...] |
19 | | - # need to check for empty since fusion tree pair might not be present |
20 | | - isempty(arr_slice) || copy!(arr_slice, v[f₁, f₂]) |
| 37 | +function _copy_subblocks!(tdst::AbstractBlockTensorMap, tsrc::AbstractTensorMap) |
| 38 | + offsets = _subblock_offsets(space(tdst)) |
| 39 | + src = _subblock_getter(tsrc) |
| 40 | + for (I, V) in pairs(eachspace(tdst)) |
| 41 | + v = similar(eltype(tdst), V) |
| 42 | + vblocks = _subblock_pairs(v) |
| 43 | + issparse(tdst) && all(isempty ∘ last, vblocks) && continue |
| 44 | + for (f, dst) in vblocks |
| 45 | + copy!(dst, view(src(f), _subblock_ranges(offsets, f, I)...)) |
21 | 46 | end |
| 47 | + @inbounds tdst[I] = v |
22 | 48 | end |
23 | 49 | return tdst |
24 | 50 | end |
|
0 commit comments