Skip to content

Commit cf26d31

Browse files
committed
share more code
1 parent 9ef15ab commit cf26d31

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/tensors/indexmanipulations.jl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,49 +262,57 @@ To repartition into an existing destination, see [repartition!](@ref).
262262
end
263263

264264
# Twist
265+
function has_shared_twist(t, inds)
266+
I = sectortype(t)
267+
if BraidingStyle(I) == NoBraiding()
268+
for i in inds
269+
cs = sectors(space(t, i))
270+
all(isone, cs) || throw(SectorMismatch(lazy"Cannot twist sectors $cs"))
271+
end
272+
return true
273+
elseif BraidingStyle(I) == Bosonic()
274+
return true
275+
else
276+
return isempty(inds)
277+
end
278+
end
279+
265280
"""
266281
twist!(t::AbstractTensorMap, i::Int; inv::Bool=false) -> t
267-
twist!(t::AbstractTensorMap, is; inv::Bool=false) -> t
282+
twist!(t::AbstractTensorMap, inds; inv::Bool=false) -> t
268283
269-
Apply a twist to the `i`th index of `t`, or all indices in `is`, storing the result in `t`.
284+
Apply a twist to the `i`th index of `t`, or all indices in `inds`, storing the result in `t`.
270285
If `inv=true`, use the inverse twist.
271286
272287
See [`twist`](@ref) for creating a new tensor.
273288
"""
274-
function twist!(t::AbstractTensorMap, is; inv::Bool = false)
275-
if !all(in(allind(t)), is)
276-
msg = "Can't twist indices $is of a tensor with only $(numind(t)) indices."
289+
function twist!(t::AbstractTensorMap, inds; inv::Bool = false)
290+
if !all(in(allind(t)), inds)
291+
msg = "Can't twist indices $inds of a tensor with only $(numind(t)) indices."
277292
throw(ArgumentError(msg))
278293
end
279-
(BraidingStyle(sectortype(t)) == Bosonic() || isempty(is)) && return t
280-
if BraidingStyle(sectortype(t)) == NoBraiding()
281-
for i in is
282-
cs = sectors(space(t, i))
283-
all(isone, cs) || throw(SectorMismatch(lazy"Cannot twist sectors $cs"))
284-
end
285-
return t
286-
end
294+
has_shared_twist(t, inds) && return t
287295
N₁ = numout(t)
288296
for (f₁, f₂) in fusiontrees(t)
289-
θ = prod(i -> i <= N₁ ? twist(f₁.uncoupled[i]) : twist(f₂.uncoupled[i - N₁]), is)
297+
θ = prod(i -> i <= N₁ ? twist(f₁.uncoupled[i]) : twist(f₂.uncoupled[i - N₁]), inds)
290298
inv &&= θ')
291-
rmul!(t[f₁, f₂], θ)
299+
scale!(t[f₁, f₂], θ)
292300
end
293301
return t
294302
end
295303

296304
"""
297305
twist(tsrc::AbstractTensorMap, i::Int; inv::Bool = false, copy::Bool = false) -> tdst
298-
twist(tsrc::AbstractTensorMap, is; inv::Bool = false, copy::Bool = false) -> tdst
306+
twist(tsrc::AbstractTensorMap, inds; inv::Bool = false, copy::Bool = false) -> tdst
299307
300308
Apply a twist to the `i`th index of `tsrc` and return the result as a new tensor.
301309
If `inv = true`, use the inverse twist.
302310
If `copy = false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
303311
304312
See [`twist!`](@ref) for storing the result in place.
305313
"""
306-
function twist(t::AbstractTensorMap, is; inv::Bool = false, copy::Bool = false)
307-
!copy && (BraidingStyle(sectortype(t)) == Bosonic() || isempty(is)) && return t
314+
function twist(t::AbstractTensorMap, inds; inv::Bool = false, copy::Bool = false)
315+
!copy && has_shared_twist(t, inds) && return t
308316
return twist!(copy(t), is; inv)
309317
end
310318

0 commit comments

Comments
 (0)