Skip to content

Commit 3d1809e

Browse files
lkdvosclaude
andcommitted
Test permute!/braid!/transpose! into sparse and dense block tensors
Cover sparse/dense destinations with missing and present entries, β ∈ {Zero(), 0, One(), random}, adjoint sources, fermionic sectors, and that β = 0 overwrites NaN-filled destinations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f2a274d commit 3d1809e

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎test/linalg/indexmanipulations.jl‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using BlockTensorKit
4+
using BlockTensorKit: sprand, nonzero_keys, nonzero_length
5+
using VectorInterface: Zero, One
16

7+
Vtr = (ℂ^2 ⊞ ℂ^1, (ℂ^1 ⊞ ℂ^2)', ℂ^2 ⊞ ℂ^3, ℂ^2 ⊞ ℂ^2 ⊞ ℂ^1, (ℂ^1 ⊞ ℂ^2)')
8+
F = Vect[FermionParity]
9+
Vf = (
10+
F(0 => 1, 1 => 1) ⊞ F(0 => 2), F(1 => 1)' ⊞ F(0 => 1, 1 => 2)', F(0 => 2, 1 => 1),
11+
F(0 => 1) ⊞ F(1 => 1) ⊞ F(0 => 1, 1 => 1), F(0 => 1, 1 => 1)' ⊞ F(1 => 2)',
12+
)
13+
14+
maketensor(sparse, T, W) = sparse ? sprand(T, W, 0.4) : randn(T, W)
15+
dense(t) = convert(TensorMap, t)
16+
17+
# `(p₁..., reverse(p₂)...)` is a cyclic rotation of the planar order of a 3 ← 2 tensor
18+
perms(f) = f === transpose! ? ((2, 3), (1, 4, 5)) : ((3, 1), (5, 2, 4))
19+
transform!(f, C, A, p, α, β) = f === braid! ? f(C, A, p, (1, 2, 3, 4, 5), α, β) : f(C, A, p, α, β)
20+
21+
@testset "$f ($(sectortype(V[1])), C sparse = $Csparse, A sparse = $Asparse, adjoint = $adj)" for V in (Vtr, Vf),
22+
f in (permute!, braid!, transpose!), Csparse in (false, true), Asparse in (false, true), adj in (false, true)
23+
T = ComplexF64
24+
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
25+
A = maketensor(Asparse, T, adj ? W' : W)
26+
A = adj ? A' : A
27+
Ad = adj ? dense(A')' : dense(A)
28+
p = perms(f)
29+
Wdst = permute(space(A), p)
30+
for β in (Zero(), zero(T), One(), randn(T))
31+
α = randn(T)
32+
C = maketensor(Csparse, T, Wdst)
33+
Cd = transform!(f, dense(C), Ad, p, α, β)
34+
@test transform!(f, C, A, p, α, β) === C
35+
@test dense(C) ≈ Cd
36+
if iszero(β) && Csparse && Asparse
37+
@test nonzero_length(C) == nonzero_length(A)
38+
end
39+
end
40+
C = scale!(maketensor(Csparse, T, Wdst), NaN)
41+
@test dense(transform!(f, C, A, p, One(), Zero())) ≈ transform!(f, zerovector(dense(C)), Ad, p, One(), Zero())
42+
end
43+
44+
@testset "permute of adjoint ($(sectortype(V[1])), sparse = $sparse)" for V in (Vtr, Vf), sparse in (false, true)
45+
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
46+
t = maketensor(sparse, ComplexF64, W)
47+
p = ((3, 1), (5, 2, 4))
48+
@test dense(permute(t', p)) ≈ permute(dense(t)', p)
49+
C = similar(t, permute(space(t'), p))
50+
@test dense(permute!(C, t', p)) ≈ permute(dense(t)', p)
51+
end

0 commit comments

Comments
 (0)