Skip to content

Commit 68e93f0

Browse files
committed
Add removeunit functionality
1 parent 44d9dad commit 68e93f0

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/spaces/homspace.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ Base.@constprop :aggressive function insertunit(W::HomSpace, i::Int=numind(W) +
181181
end
182182
end
183183

184+
"""
185+
removeunit(P::HomSpace, i::Int)
186+
187+
This removes a trivial tensor product factor at position `1 ≤ i ≤ N`.
188+
For this to work, that factor has to be isomorphic to the field of scalars.
189+
190+
This operation undoes the work of [`insertunit`](@ref).
191+
"""
192+
function removeunit(P::HomSpace, i::Int)
193+
if i in 1:numout(P)
194+
return removeunit(codomain(P), i) domain(P)
195+
elseif i in (numout(P) + 1):numind(P)
196+
return codomain(P) removeunit(P, i - numout(P))
197+
else
198+
throw(BoundsError(P, i))
199+
end
200+
end
201+
184202
# Block and fusion tree ranges: structure information for building tensors
185203
#--------------------------------------------------------------------------
186204
struct FusionBlockStructure{I,N,F₁,F₂}

src/spaces/productspace.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ For `P::ProductSpace{S,N}`, this adds an extra tensor product factor at position
253253
underlying field of scalars, i.e. `oneunit(S)`. With the keyword arguments, one can choose
254254
to insert the conjugated or dual space instead, which are all isomorphic to the field of
255255
scalars.
256+
257+
This operation can be undone by [`removeunit`](@ref).
256258
"""
257259
function insertunit(P::ProductSpace, i::Int=length(P) + 1; dual=false, conj=false)
258260
u = oneunit(spacetype(P))
@@ -265,6 +267,21 @@ function insertunit(P::ProductSpace, i::Int=length(P) + 1; dual=false, conj=fals
265267
return ProductSpace(TupleTools.insertafter(P.spaces, i - 1, (u,)))
266268
end
267269

270+
"""
271+
removeunit(P::ProductSpace, i::Int)
272+
273+
This removes a trivial tensor product factor at position `1 ≤ i ≤ N`.
274+
For this to work, that factor has to be isomorphic to the field of scalars.
275+
276+
This operation undoes the work of [`insertunit`](@ref).
277+
"""
278+
function removeunit(P::ProductSpace, i::Int)
279+
1 i length(P) || throw(BoundsError(P, i))
280+
isisomorphic(P[i], oneunit(P[i])) ||
281+
throw(ArgumentError("Attempting to remove a non-trivial space"))
282+
return ProductSpace{spacetype(P)}(TupleTools.deleteat(P.spaces, i))
283+
end
284+
268285
# Functionality for extracting and iterating over spaces
269286
#--------------------------------------------------------
270287
Base.length(P::ProductSpace) = length(P.spaces)

src/tensors/indexmanipulations.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,29 @@ function insertunit(t::AbstractTensorMap, i::Int=numind(t) + 1;
298298
return tdst
299299
end
300300

301+
"""
302+
removeunit(tsrc::AbstractTensorMap, i::Int; copy=false) -> tdst
303+
304+
This removes a trivial tensor product factor at position `1 ≤ i ≤ N`.
305+
For this to work, that factor has to be isomorphic to the field of scalars.
306+
307+
If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
308+
309+
This operation undoes the work of [`insertunit`](@ref).
310+
"""
311+
function removeunit(t::TensorMap, i::Int; copy::Bool=false)
312+
W = removeunit(space(t), i)
313+
return TensorMap{scalartype(t)}(copy ? Base.copy(t.data) : t.data, W)
314+
end
315+
function removeunit(t::AbstractTensorMap, i::Int=numind(t) + 1; copy::Bool=true)
316+
W = removeunit(space(t), i)
317+
tdst = similar(t, W)
318+
for (c, b) in blocks(t)
319+
copy!(block(tdst, c), b)
320+
end
321+
return tdst
322+
end
323+
301324
# Fusing and splitting
302325
# TODO: add functionality for easy fusing and splitting of tensor indices
303326

0 commit comments

Comments
 (0)