Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/tensors/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,51 @@ Base.@constprop :aggressive function TK.removeunit(
return tdst
end

# twist!, twist
# -------------
# the in-place `twist!(t, inds)` twists each block in place
function TK.twist!(t::AbstractBlockTensorMap, is; inv::Bool = false)
foreach(x -> twist!(x, is; inv), nonzero_values(t))
return t
end

# `twist` and `twist!(tdst, tsrc, inds, α, β)` unwrap adjoints and then call `_twist!`

# unwrap just `tsrc`, see `_braid_block!`
# unlike in `_braid!`, `twistmask` refers to the legs of `tsrc`, so it's relabeled along with it
@propagate_inbounds function _twist_block!(
tdst, tsrc, p, conjsrc, twistmask, inv, α, β, backend, allocator
)
tsrc′, p′, twistmask′, conjsrc′ = TK._unwrap_source(tsrc, p, twistmask, conjsrc)
return TK._twist!(tdst, tsrc′, p′, conjsrc′, twistmask′, inv, α, β, backend, allocator)
end

@propagate_inbounds function TK._twist!(
tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool,
twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
)
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
dstdata = parent(tdst)
srcdata = permutedims(StridedView(parent(tsrc)), (p[1]..., p[2]...))
@inbounds for I in eachindex(dstdata, srcdata)
dstdata[I] = _twist_block!(
dstdata[I], srcdata[I], p, conjsrc, twistmask, inv, α, β, backend, allocator
)
end
return tdst
end
@propagate_inbounds function TK._twist!(
tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, p::Index2Tuple, conjsrc::Bool,
twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
)
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
p_lin = (p[1]..., p[2]...)
_scale_untouched!(tdst, tsrc, β, p_lin)
@inbounds for (I, v) in nonzero_pairs(tsrc)
I′ = CartesianIndex(TT.getindices(I.I, p_lin))
_addblock!(tdst, I′, β, allocator) do c, γ
_twist_block!(c, v, p, conjsrc, twistmask, inv, α, γ, backend, allocator)
end
end
return tdst
end
19 changes: 19 additions & 0 deletions test/linalg/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ transform!(f, C, A, p, α, β) = f === braid! ? f(C, A, p, (1, 2, 3, 4, 5), α,
@test dense(transform!(f, C, A, p, One(), Zero())) ≈ transform!(f, zerovector(dense(C)), Ad, p, One(), Zero())
end

# `twist` and `twist!(tdst, tsrc, ...)` go through `_twist!`, unlike the in-place `twist!(t, inds)`
@testset "twist ($(sectortype(V[1])), C sparse = $Csparse, A sparse = $Asparse, adjoint = $adj)" for V in (Vtr, Vf),
Csparse in (false, true), Asparse in (false, true), adj in (false, true)
T = ComplexF64
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
A = maketensor(Asparse, T, adj ? W' : W)
A = adj ? A' : A
Ad = adj ? dense(A')' : dense(A)
inds = (1, 4)
for β in (Zero(), One(), randn(T)), inv in (false, true)
α = randn(T)
C = maketensor(Csparse, T, space(A))
Cd = twist!(dense(C), Ad, inds, α, β; inv)
@test twist!(C, A, inds, α, β; inv) === C
@test dense(C) ≈ Cd
end
Csparse || @test dense(twist(A, inds)) ≈ twist(Ad, inds)
end

@testset "permute of adjoint ($(sectortype(V[1])), sparse = $sparse)" for V in (Vtr, Vf), sparse in (false, true)
W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5]
t = maketensor(sparse, ComplexF64, W)
Expand Down
Loading