Skip to content

Commit 1d38cdf

Browse files
lkdvosclaude
andcommitted
Implement block and subblock access through cached blocks/subblocks iterators
`subblocks(t)` now caches each entry's subblock structure, mirroring `blocks(t)`; `block`/`subblock` share the same per-entry accessors, and the conversion code reuses them instead of its own getter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c4f5054 commit 1d38cdf

3 files changed

Lines changed: 70 additions & 57 deletions

File tree

‎src/tensors/abstractblocktensor/abstracttensormap.jl‎

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@ end
1010

1111
eachspace(t::AbstractBlockTensorMap) = SumSpaceIndices(space(t))
1212

13-
@inline function TensorKit.subblock(
14-
t::AbstractBlockTensorMap, (f₁, f₂)::Tuple{FusionTree, FusionTree}
15-
)
16-
sectortype(t) === sectortype(f₁) === sectortype(f₂) ||
17-
throw(SectorMismatch("Not a valid sectortype for this tensor"))
18-
numout(t) == length(f₁) && numin(t) == length(f₂) ||
19-
throw(DimensionMismatch("Invalid number of fusiontree legs for this tensor"))
20-
21-
W = eachspace(t)
22-
subblocks = map(eachindex(IndexCartesian(), t)) do I
23-
V = W[I]
24-
sz = (dims(codomain(V), f₁.uncoupled)..., dims(domain(V), f₂.uncoupled)...)
25-
if prod(sz) == 0 || (issparse(t) && !haskey(parent(t), I))
26-
data = zerovector!(similar(storagetype(t), prod(sz)))
27-
return sreshape(StridedView(data), sz)
28-
else
29-
return t[I][f₁, f₂]
30-
end
31-
end
32-
33-
return mortar(subblocks)
34-
end
3513
@inline function Base.setindex!(
3614
t::AbstractBlockTensorMap, v::AbstractBlockArray, f₁::FusionTree, f₂::FusionTree
3715
)
@@ -55,8 +33,53 @@ end
5533
return setindex!(t, v′, f₁, f₂)
5634
end
5735

36+
# Blocks and subblocks
37+
# --------------------
38+
_entrycache(f, t::AbstractBlockTensorMap) =
39+
issparse(t) ? Dict(I => f(x) for (I, x) in nonzero_pairs(t)) : map(f, parent(t))
40+
41+
_blockcache(x) = x
42+
_blockcache(x::AbstractTensorMap) = (b = TK.blocks(x); b isa TK.BlockIterator ? b : x)
43+
_cachedblock(x, c::Sector) = block(x, c)
44+
_cachedblock(b::TK.BlockIterator, c::Sector) = b[c]
45+
46+
_subblockcache(x) = x
47+
_subblockcache(x::AbstractTensorMap) = sectortype(x) === Trivial ? x : TK.subblocks(x)
48+
_cachedsubblock(x, f) = subblock(x, f)
49+
_cachedsubblock(s::TK.SubblockIterator, f) = s[f]
50+
51+
TensorKit.blocks(t::AbstractBlockTensorMap) =
52+
TK.BlockIterator(t, (blocksectors(t), _entrycache(_blockcache, t)))
5853
TensorKit.block(t::AbstractBlockTensorMap, c::Sector) = _block(t, parent(t), c)
5954

55+
TensorKit.subblocks(t::AbstractBlockTensorMap) =
56+
TK.SubblockIterator(t, (TK.fusiontrees(t), _entrycache(_subblockcache, t)))
57+
TensorKit.subblock(t::AbstractBlockTensorMap, f::Tuple{FusionTree, FusionTree}) = _subblock(t, parent(t), f)
58+
59+
Base.length(iter::TK.BlockIterator{<:AbstractBlockTensorMap}) = length(first(iter.structure))
60+
Base.isdone(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, state...) =
61+
Base.isdone(first(iter.structure), state...)
62+
function Base.iterate(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, state...)
63+
next = iterate(first(iter.structure), state...)
64+
isnothing(next) && return next
65+
c, newstate = next
66+
return c => iter[c], newstate
67+
end
68+
Base.getindex(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, c::Sector) =
69+
_block(iter.t, last(iter.structure), c)
70+
71+
Base.length(iter::TK.SubblockIterator{<:AbstractBlockTensorMap}) = length(first(iter.structure))
72+
Base.isdone(iter::TK.SubblockIterator{<:AbstractBlockTensorMap}, i::Int = 1) = i > length(iter)
73+
function Base.iterate(iter::TK.SubblockIterator{<:AbstractBlockTensorMap}, i::Int = 1)
74+
i > length(iter) && return nothing
75+
f = TK.gettokenvalue(first(iter.structure), i)
76+
return f => iter[f], i + 1
77+
end
78+
Base.getindex(iter::TK.SubblockIterator{<:AbstractBlockTensorMap}, i::Int) =
79+
iter[TK.gettokenvalue(first(iter.structure), i)]
80+
Base.getindex(iter::TK.SubblockIterator{<:AbstractBlockTensorMap}, f::Tuple{FusionTree, FusionTree}) =
81+
_subblock(iter.t, last(iter.structure), f)
82+
6083
function _block(t::AbstractBlockTensorMap, entries, c::Sector)
6184
sectortype(t) == typeof(c) || throw(SectorMismatch())
6285
rowdims = _blockdims(codomain(t), c)
@@ -73,6 +96,24 @@ function _block(t::AbstractBlockTensorMap, entries, c::Sector)
7396
return mortar(allblocks, rowdims, coldims)
7497
end
7598

99+
function _subblock(t::AbstractBlockTensorMap, entries, (f₁, f₂)::Tuple{FusionTree, FusionTree})
100+
sectortype(t) === sectortype(f₁) === sectortype(f₂) ||
101+
throw(SectorMismatch("Not a valid sectortype for this tensor"))
102+
numout(t) == length(f₁) && numin(t) == length(f₂) ||
103+
throw(DimensionMismatch("Invalid number of fusiontree legs for this tensor"))
104+
W = eachspace(t)
105+
subblocks = map(eachindex(IndexCartesian(), t)) do I
106+
V = W[I]
107+
sz = (dims(codomain(V), f₁.uncoupled)..., dims(domain(V), f₂.uncoupled)...)
108+
if prod(sz) == 0 || (issparse(t) && !haskey(entries, I))
109+
return sreshape(StridedView(zerovector!(similar(storagetype(t), prod(sz)))), sz)
110+
else
111+
return _cachedsubblock(entries[I], (f₁, f₂))
112+
end
113+
end
114+
return mortar(subblocks)
115+
end
116+
76117
function _blockdims(P::ProductSumSpace{S, N}, c::Sector) where {S, N}
77118
return vec(
78119
map(CartesianIndices(map(length, P.spaces))) do I
@@ -81,11 +122,6 @@ function _blockdims(P::ProductSumSpace{S, N}, c::Sector) where {S, N}
81122
)
82123
end
83124

84-
_blockcache(x) = x
85-
_blockcache(x::AbstractTensorMap) = (b = TK.blocks(x); b isa TK.BlockIterator ? b : x)
86-
_cachedblock(x, c::Sector) = block(x, c)
87-
_cachedblock(b::TK.BlockIterator, c::Sector) = b[c]
88-
89125
function _zeroblock(t::AbstractBlockTensorMap, I, d₁::Int, d₂::Int, c::Sector)
90126
eltype(t) <: TensorMap || return block(zerovector!(similar(eltype(t), eachspace(t)[I])), c)
91127
data = zerovector!(similar(storagetype(t), d₁ * d₂))
@@ -96,12 +132,6 @@ end
96132
TensorKit.blocksectors(t::AbstractBlockTensorMap) = blocksectors(space(t))
97133
TensorKit.hasblock(t::AbstractBlockTensorMap, c::Sector) = c in blocksectors(t)
98134

99-
function TensorKit.blocks(t::AbstractBlockTensorMap)
100-
entries = issparse(t) ? Dict(I => _blockcache(x) for (I, x) in nonzero_pairs(t)) :
101-
map(_blockcache, parent(t))
102-
return TK.BlockIterator(t, (blocksectors(t), entries))
103-
end
104-
105135
Base.@assume_effects :foldable function _blockeltype(::Type{TT}) where {TT <: AbstractBlockTensorMap}
106136
T = scalartype(TT)
107137
B = TK.blocktype(eltype(TT))
@@ -112,25 +142,6 @@ function TensorKit.blocktype(::Type{TT}) where {TT <: AbstractBlockTensorMap}
112142
return BlockMatrix{scalartype(TT), Matrix{_blockeltype(TT)}, BS}
113143
end
114144

115-
Base.length(iter::TK.BlockIterator{<:AbstractBlockTensorMap}) = length(first(iter.structure))
116-
Base.isdone(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, state...) =
117-
Base.isdone(first(iter.structure), state...)
118-
function Base.iterate(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, state...)
119-
sectors, entries = iter.structure
120-
next = iterate(sectors, state...)
121-
isnothing(next) && return next
122-
c, newstate = next
123-
return c => _block(iter.t, entries, c), newstate
124-
end
125-
Base.getindex(iter::TK.BlockIterator{<:AbstractBlockTensorMap}, c::Sector) =
126-
_block(iter.t, last(iter.structure), c)
127-
128-
function TensorKit.foreachblock(f, t::AbstractBlockTensorMap; scheduler = nothing)
129-
foreach(TK.blocks(t)) do (c, b)
130-
return f(c, (b,))
131-
end
132-
return nothing
133-
end
134145
function TensorKit.foreachblock(f, t::AbstractBlockTensorMap, ts...; scheduler = nothing)
135146
tensors = (t, ts...)
136147
iters = map(_blockcache, tensors)

‎src/tensors/abstractblocktensor/conversion.jl‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ function _subblock_pairs(t::AbstractTensorMap)
1212
f = TK.trivial_fusiontree(t)
1313
return (f => subblock(t, f),)
1414
end
15-
function _subblock_getter(t::AbstractTensorMap)
16-
return sectortype(t) === Trivial ? Base.Fix1(subblock, t) : Base.Fix1(getindex, subblocks(t))
17-
end
1815

1916
function _copy_subblocks!(tdst::TensorMap, tsrc::AbstractBlockTensorMap)
2017
offsets = map(_sumspace_offsets, eachspace(tsrc).sumspaces)
21-
dst = _subblock_getter(tdst)
18+
dst = Base.Fix1(_cachedsubblock, _subblockcache(tdst))
2219
for (I, v) in nonzero_pairs(tsrc), (f, src) in _subblock_pairs(v)
2320
copy!(view(dst(f), _subblock_ranges(offsets, f, I)...), src)
2421
end
@@ -27,7 +24,7 @@ end
2724

2825
function _copy_subblocks!(tdst::AbstractBlockTensorMap, tsrc::AbstractTensorMap)
2926
offsets = map(_sumspace_offsets, eachspace(tdst).sumspaces)
30-
src = _subblock_getter(tsrc)
27+
src = Base.Fix1(_cachedsubblock, _subblockcache(tsrc))
3128
for (I, V) in pairs(eachspace(tdst))
3229
v = similar(eltype(tdst), V)
3330
vblocks = _subblock_pairs(v)

‎test/abstracttensor/sparseblocktensor.jl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ end
310310
for (f₁, f₂) in fusiontrees(t)
311311
@test t[f₁, f₂] == td[f₁, f₂]
312312
end
313+
sbs = TensorKit.subblocks(t)
314+
@test length(sbs) == length(fusiontrees(t))
315+
for (i, (f, b)) in enumerate(sbs)
316+
@test b == sbs[i] == TensorKit.subblock(td, f)
317+
end
313318
@test t == td
314319
end
315320
end

0 commit comments

Comments
 (0)