Skip to content

Commit 34a00be

Browse files
authored
blockwise _twist! to support twist and non-inplace twist! (#94)
1 parent 28a59cd commit 34a00be

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,51 @@ Base.@constprop :aggressive function TK.removeunit(
138138
return tdst
139139
end
140140

141+
# twist!, twist
142+
# -------------
143+
# the in-place `twist!(t, inds)` twists each block in place
141144
function TK.twist!(t::AbstractBlockTensorMap, is; inv::Bool = false)
142145
foreach(x -> twist!(x, is; inv), nonzero_values(t))
143146
return t
144147
end
148+
149+
# `twist` and `twist!(tdst, tsrc, inds, α, β)` unwrap adjoints and then call `_twist!`
150+
151+
# unwrap just `tsrc`, see `_braid_block!`
152+
# unlike in `_braid!`, `twistmask` refers to the legs of `tsrc`, so it's relabeled along with it
153+
@propagate_inbounds function _twist_block!(
154+
tdst, tsrc, p, conjsrc, twistmask, inv, α, β, backend, allocator
155+
)
156+
tsrc′, p′, twistmask′, conjsrc′ = TK._unwrap_source(tsrc, p, twistmask, conjsrc)
157+
return TK._twist!(tdst, tsrc′, p′, conjsrc′, twistmask′, inv, α, β, backend, allocator)
158+
end
159+
160+
@propagate_inbounds function TK._twist!(
161+
tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool,
162+
twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
163+
)
164+
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
165+
dstdata = parent(tdst)
166+
srcdata = permutedims(StridedView(parent(tsrc)), (p[1]..., p[2]...))
167+
@inbounds for I in eachindex(dstdata, srcdata)
168+
dstdata[I] = _twist_block!(
169+
dstdata[I], srcdata[I], p, conjsrc, twistmask, inv, α, β, backend, allocator
170+
)
171+
end
172+
return tdst
173+
end
174+
@propagate_inbounds function TK._twist!(
175+
tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, p::Index2Tuple, conjsrc::Bool,
176+
twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
177+
)
178+
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
179+
p_lin = (p[1]..., p[2]...)
180+
_scale_untouched!(tdst, tsrc, β, p_lin)
181+
@inbounds for (I, v) in nonzero_pairs(tsrc)
182+
I′ = CartesianIndex(TT.getindices(I.I, p_lin))
183+
_addblock!(tdst, I′, β, allocator) do c, γ
184+
_twist_block!(c, v, p, conjsrc, twistmask, inv, α, γ, backend, allocator)
185+
end
186+
end
187+
return tdst
188+
end

‎test/linalg/indexmanipulations.jl‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ transform!(f, C, A, p, α, β) = f === braid! ? f(C, A, p, (1, 2, 3, 4, 5), α,
4141
@test dense(transform!(f, C, A, p, One(), Zero())) ≈ transform!(f, zerovector(dense(C)), Ad, p, One(), Zero())
4242
end
4343

44+
# `twist` and `twist!(tdst, tsrc, ...)` go through `_twist!`, unlike the in-place `twist!(t, inds)`
45+
@testset "twist ($(sectortype(V[1])), C sparse = $Csparse, A sparse = $Asparse, adjoint = $adj)" for V in (Vtr, Vf),
46+
Csparse in (false, true), Asparse in (false, true), adj in (false, true)
47+
T = ComplexF64
48+
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
49+
A = maketensor(Asparse, T, adj ? W' : W)
50+
A = adj ? A' : A
51+
Ad = adj ? dense(A')' : dense(A)
52+
inds = (1, 4)
53+
for β in (Zero(), One(), randn(T)), inv in (false, true)
54+
α = randn(T)
55+
C = maketensor(Csparse, T, space(A))
56+
Cd = twist!(dense(C), Ad, inds, α, β; inv)
57+
@test twist!(C, A, inds, α, β; inv) === C
58+
@test dense(C) ≈ Cd
59+
end
60+
Csparse || @test dense(twist(A, inds)) ≈ twist(Ad, inds)
61+
end
62+
4463
@testset "permute of adjoint ($(sectortype(V[1])), sparse = $sparse)" for V in (Vtr, Vf), sparse in (false, true)
4564
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
4665
t = maketensor(sparse, ComplexF64, W)

0 commit comments

Comments
 (0)