Skip to content

Commit 95d776a

Browse files
lkdvosclaude
andauthored
fix: mixed dense/sparse add!, scale! and inner (#74) (#75)
These were only defined for `BlockTensorMap`/`BlockTensorMap` and `SparseBlockTensorMap`/`SparseBlockTensorMap` pairs, so a mixed pair fell through to TensorKit's generic `AbstractTensorMap` methods, which operate on the fused per-sector `BlockMatrix`es. That path throws a `BoundsError` whenever a coupled sector has an interior zero-length block, which is the normal situation for a `SumSpace` with heterogeneous summands. Replace the specializations by a single `AbstractBlockTensorMap` method each, branching on `issparse` only where the cases differ. Also drop the dense-only `axpy!`, `axpby!`, `dot` and `mul!`, which reimplement TensorKit generics that forward to this layer (the deleted `mul!` referenced `SparseArrayKit._zero!`, which is not a dependency of this package). Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 204c99f commit 95d776a

3 files changed

Lines changed: 66 additions & 102 deletions

File tree

‎src/linalg/linalg.jl‎

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,6 @@ Base.:(*)(α::Number, t::AbstractBlockTensorMap) = scale(t, α)
99
Base.:(/)(t::AbstractBlockTensorMap, α::Number) = scale(t, inv(α))
1010
Base.:(\)(α::Number, t::AbstractBlockTensorMap) = scale(t, inv(α))
1111

12-
function LinearAlgebra.axpy!(α::Number, t1::BlockTensorMap, t2::BlockTensorMap)
13-
space(t1) == space(t2) || throw(SpaceMismatch())
14-
for (i, v) in nonzero_pairs(t1)
15-
t2[i] = axpy!(α, v, t2[i])
16-
end
17-
return t2
18-
end
19-
20-
function LinearAlgebra.axpby!(α::Number, t1::BlockTensorMap, β::Number, t2::BlockTensorMap)
21-
space(t1) == space(t2) || throw(SpaceMismatch())
22-
rmul!(t2, β)
23-
for (i, v) in nonzero_pairs(t1)
24-
t2[i] = axpy!(α, v, t2[i])
25-
end
26-
return t2
27-
end
28-
29-
function LinearAlgebra.dot(t1::BlockTensorMap, t2::BlockTensorMap)
30-
size(t1) == size(t2) || throw(DimensionMismatch("dot arguments have different size"))
31-
32-
s = zero(promote_type(scalartype(t1), scalartype(t2)))
33-
if nonzero_length(t1) >= nonzero_length(t2)
34-
@inbounds for (I, v) in nonzero_pairs(t1)
35-
s += dot(v, t2[I])
36-
end
37-
else
38-
@inbounds for (I, v) in nonzero_pairs(t2)
39-
s += dot(t1[I], v)
40-
end
41-
end
42-
return s
43-
end
44-
45-
function LinearAlgebra.mul!(C::BlockTensorMap, α::Number, A::BlockTensorMap)
46-
space(C) == space(A) || throw(SpaceMismatch())
47-
SparseArrayKit._zero!(parent(C))
48-
for (i, v) in nonzero_pairs(A)
49-
C[i] = mul!(C[i], α, v)
50-
end
51-
return C
52-
end
53-
5412
for TA in (:AbstractBlockTensorMap, :(TK.DiagonalTensorMap), :(TK.AdjointTensorMap), :TensorMap),
5513
TB in (:AbstractBlockTensorMap, :(TK.DiagonalTensorMap), :(TK.AdjointTensorMap), :TensorMap)
5614
(TA === :AbstractBlockTensorMap || TB === :AbstractBlockTensorMap) || continue

‎src/tensors/vectorinterface.jl‎

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,13 @@ function VI.scale!(t::AbstractBlockTensorMap, α::Number)
1010
return t
1111
end
1212

13-
function VI.scale!(ty::BlockTensorMap, tx::BlockTensorMap, α::Number)
13+
function VI.scale!(ty::AbstractBlockTensorMap, tx::AbstractBlockTensorMap, α::Number)
1414
space(ty) == space(tx) || throw(SpaceMismatch("$(space(ty)) ≠ $(space(tx))"))
15-
scale!(parent(ty), parent(tx), α)
16-
return ty
17-
end
18-
function VI.scale!(ty::SparseBlockTensorMap, tx::SparseBlockTensorMap, α::Number)
19-
space(ty) == space(tx) || throw(SpaceMismatch("$(space(ty)) ≠ $(space(tx))"))
20-
y_notin_x = setdiff(nonzero_keys(ty), nonzero_keys(tx))
21-
x_notin_y = setdiff(nonzero_keys(tx), nonzero_keys(ty))
22-
inboth = intersect(nonzero_keys(ty), nonzero_keys(tx))
23-
24-
# remove elements that are not in tx
25-
for k in y_notin_x
26-
delete!(ty.data, k)
27-
end
28-
# in-place scale elements that are in both
29-
for k in inboth
30-
ty[k] = scale!(ty[k], tx[k], α)
15+
# entries of ty that are structurally zero in tx have to be zeroed out
16+
issparse(tx) && zerovector!(ty)
17+
for (I, v) in nonzero_pairs(tx)
18+
ty[I] = scale!!(ty[I], v, α)
3119
end
32-
# new scale for elements in x that are not in y
33-
for k in x_notin_y
34-
ty[k] = scale(tx[k], α)
35-
end
36-
3720
return ty
3821
end
3922

@@ -61,41 +44,27 @@ function VI.add(ty::AbstractBlockTensorMap, tx::AbstractBlockTensorMap, α::Numb
6144
return add!(scale!(tdst, ty, β), tx, α)
6245
end
6346

64-
function VI.add!(ty::BlockTensorMap, tx::BlockTensorMap, α::Number, β::Number)
47+
function VI.add!(ty::AbstractBlockTensorMap, tx::AbstractBlockTensorMap, α::Number, β::Number)
6548
space(ty) == space(tx) || throw(SpaceMismatch("$(space(ty)) ≠ $(space(tx))"))
66-
add!(parent(ty), parent(tx), α, β)
67-
return ty
68-
end
69-
function VI.add!(ty::SparseBlockTensorMap, tx::SparseBlockTensorMap, α::Number, β::Number)
70-
space(ty) == space(tx) || throw(SpaceMismatch("$(space(ty)) ≠ $(space(tx))"))
71-
y_notin_x = setdiff(nonzero_keys(ty), nonzero_keys(tx))
72-
x_notin_y = setdiff(nonzero_keys(tx), nonzero_keys(ty))
73-
inboth = intersect(nonzero_keys(ty), nonzero_keys(tx))
74-
75-
for k in y_notin_x
76-
ty[k] = scale!!(ty[k], β)
77-
end
78-
for k in x_notin_y
79-
ty[k] = scale(tx[k], α)
80-
end
81-
for k in inboth
82-
ty[k] = add!!(ty[k], tx[k], α, β)
49+
isone(β) || scale!(ty, β)
50+
for (I, v) in nonzero_pairs(tx)
51+
ty[I] = add!!(ty[I], v, α, One())
8352
end
84-
8553
return ty
8654
end
8755

8856
# inner
8957
# -----
90-
function VI.inner(x::BlockTensorMap, y::BlockTensorMap)
91-
space(y) == space(x) || throw(SpaceMismatch())
92-
return inner(parent(x), parent(y))
93-
end
94-
function VI.inner(x::SparseBlockTensorMap, y::SparseBlockTensorMap)
95-
space(x) == space(y) || throw(SpaceMismatch())
96-
both_nonzero = intersect(nonzero_keys(x), nonzero_keys(y))
58+
function VI.inner(x::AbstractBlockTensorMap, y::AbstractBlockTensorMap)
59+
space(x) == space(y) || throw(SpaceMismatch("$(space(x)) ≠ $(space(y))"))
9760
T = VI.promote_inner(x, y)
98-
return sum(both_nonzero; init = zero(T)) do k
99-
inner(x[k], y[k])
61+
# only entries that are nonzero in both contribute
62+
ks = if issparse(x) && issparse(y)
63+
intersect(nonzero_keys(x), nonzero_keys(y))
64+
else
65+
nonzero_keys(issparse(y) ? y : x)
66+
end
67+
return sum(ks; init = zero(T)) do I
68+
inner(x[I], y[I])
10069
end
10170
end

‎test/linalg/vectorinterface.jl‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ Vtr = (
1414

1515
V = Vtr
1616

17-
@testset "VectorInterface $(issparse ? "SparseBlockTensorMap" : "BlockTensorMap")" for issparse in
18-
(
19-
false, true,
17+
storagename(sparse) = sparse ? "SparseBlockTensorMap" : "BlockTensorMap"
18+
makestorage(sparse, W) = sparse ? sprand(Float64, W, 0.5) : rand(W)
19+
20+
@testset "VectorInterface $(storagename(sparse_y)) ← $(storagename(sparse_x))" for (
21+
sparse_y, sparse_x,
22+
) in (
23+
(false, false), (true, true), (false, true), (true, false),
2024
)
21-
if issparse
22-
t = sprand(Float64, *(V[1:3]...) ← *(V[4], V[5]), 0.5)
23-
t′ = sprand(Float64, *(V[1:3]...) ← *(V[4], V[5]), 0.5)
24-
else
25-
t = rand(*(V[1:3]...) ← *(V[4], V[5]))
26-
t′ = rand(*(V[1:3]...) ← *(V[4], V[5]))
27-
end
25+
W = *(V[1:3]...) ← *(V[4], V[5])
26+
t = makestorage(sparse_y, W)
27+
t′ = makestorage(sparse_x, W)
2828

2929
@testset "scalartype" begin
3030
@test Float64 === @inferred scalartype(t)
@@ -132,6 +132,15 @@ V = Vtr
132132
@test @inferred inner(t″, t‴) ≈ conj(α) * β * inner(t, t′)
133133
end
134134

135+
@testset "mixed-storage in-place" begin
136+
α, β = rand(2)
137+
@test @inferred scale!(zerovector(t), t′, α) ≈ scale(t′, α)
138+
@test @inferred axpy!(α, t′, deepcopy(t)) ≈ add(t, t′, α)
139+
@test @inferred axpby!(α, t′, β, deepcopy(t)) ≈ add(t, t′, α, β)
140+
@test @inferred mul!(zerovector(t), α, t′) ≈ scale(t′, α)
141+
@test @inferred dot(t, t′) ≈ inner(t, t′)
142+
end
143+
135144
@testset "general linalg" begin
136145
α, β = rand(ComplexF64, 2)
137146
@test (α * α) * t ≈ α * (α * t)
@@ -143,3 +152,31 @@ V = Vtr
143152
@test inner(t, t′) ≈ conj(inner(t′, t))
144153
end
145154
end
155+
156+
# regression test for #74: heterogeneous summands give interior zero-length coupled blocks
157+
@testset "mixed storage with interior zero blocks" begin
158+
S = Vect[U1Irrep]
159+
P = S(0 => 1, 1 => 1, 2 => 1)
160+
Vu = SumSpace(S(0 => 1), S(1 => 2), S(-1 => 2))
161+
W = (Vu ⊗ SumSpace(P)) ← SumSpace(P)
162+
163+
tsp = SparseBlockTensorMap{TensorMap{Float64, S, 2, 1, Vector{Float64}}}(undef, W)
164+
for (I, Wi) in zip(CartesianIndices(size(tsp)), BlockTensorKit.eachspace(tsp))
165+
dim(Wi) == 0 || (tsp[I] = randn(Float64, Wi))
166+
end
167+
tdn = BlockTensorMap(tsp)
168+
@test convert(TensorMap, tdn) ≈ convert(TensorMap, tsp)
169+
170+
α, β = rand(2)
171+
ref = add(tdn, tdn, α, β)
172+
for (y, x) in ((tdn, tsp), (tsp, tdn))
173+
@test add!(deepcopy(y), x, α, β) ≈ ref
174+
@test add(y, x, α, β) ≈ ref
175+
@test y + x ≈ add(tdn, tdn)
176+
@test norm(y - x) ≈ 0 atol = 1.0e-12
177+
@test scale!(zerovector(y), x, α) ≈ scale(tdn, α)
178+
@test axpy!(α, x, deepcopy(y)) ≈ add(tdn, tdn, α)
179+
@test inner(y, x) ≈ inner(tdn, tdn)
180+
@test dot(y, x) ≈ inner(tdn, tdn)
181+
end
182+
end

0 commit comments

Comments
 (0)