@@ -292,81 +292,84 @@ See [`twist!`](@ref) for storing the result in place.
292292"""
293293twist (t:: AbstractTensorMap , i; inv:: Bool = false ) = twist! (copy (t), i; inv)
294294
295+ # Methods which change the number of indices, implement using `Val(i)` for type inference
295296"""
296- insertleftunit(tsrc::AbstractTensorMap, i::Int =numind(t) + 1;
297+ insertleftunit(tsrc::AbstractTensorMap, i=numind(t) + 1;
297298 conj=false, dual=false, copy=false) -> tdst
298299
299- Insert a trivial vector space, isomorphic to the underlying field, at position `i`.
300+ Insert a trivial vector space, isomorphic to the underlying field, at position `i`,
301+ which can be specified as an `Int` or as `Val(i)` for improved type stability.
300302More specifically, adds a left monoidal unit or its dual.
301303
302304If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
303305
304- See also [`insertrightunit`](@ref) and [`removeunit`](@ref).
306+ See also [`insertrightunit`](@ref insertrightunit(::AbstractTensorMap, ::Val{i}) where {i}),
307+ [`removeunit`](@ref removeunit(::AbstractTensorMap, ::Val{i}) where {i}).
305308"""
306- @constprop :aggressive function insertleftunit (t:: AbstractTensorMap , i:: Int = numind (t) + 1 ;
307- copy:: Bool = true , conj:: Bool = false ,
308- dual:: Bool = false )
309- W = insertleftunit (space (t), i; conj, dual)
310- tdst = similar (t, W)
311- for (c, b) in blocks (t)
312- copy! (block (tdst, c), b)
309+ function insertleftunit (t:: AbstractTensorMap , :: Val{i} = Val (numind (t) + 1 );
310+ copy:: Bool = false , conj:: Bool = false , dual:: Bool = false ) where {i}
311+ W = insertleftunit (space (t), Val (i); conj, dual)
312+ if t isa TensorMap
313+ return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
314+ else
315+ tdst = similar (t, W)
316+ for (c, b) in blocks (t)
317+ copy! (block (tdst, c), b)
318+ end
319+ return tdst
313320 end
314- return tdst
315- end
316- @constprop :aggressive function insertleftunit (t:: TensorMap , i:: Int = numind (t) + 1 ;
317- copy:: Bool = false , conj:: Bool = false ,
318- dual:: Bool = false )
319- W = insertleftunit (space (t), i; conj, dual)
320- return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
321321end
322322
323323"""
324- insertrightunit(tsrc::AbstractTensorMap, i::Int =numind(t);
324+ insertrightunit(tsrc::AbstractTensorMap, i=numind(t);
325325 conj=false, dual=false, copy=false) -> tdst
326326
327- Insert a trivial vector space, isomorphic to the underlying field, after position `i`.
327+ Insert a trivial vector space, isomorphic to the underlying field, after position `i`,
328+ which can be specified as an `Int` or as `Val(i)` for improved type stability.
328329More specifically, adds a right monoidal unit or its dual.
329330
330331If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
331332
332- See also [`insertleftunit`](@ref) and [`removeunit`](@ref).
333+ See also [`insertleftunit`](@ref insertleftunit(::AbstractTensorMap, ::Val{i}) where {i}),
334+ [`removeunit`](@ref removeunit(::AbstractTensorMap, ::Val{i}) where {i}).
333335"""
334- @constprop :aggressive function insertrightunit (t:: AbstractTensorMap , i:: Int = numind (t);
335- copy:: Bool = true , kwargs... )
336- W = insertrightunit (space (t), i; kwargs... )
337- tdst = similar (t, W)
338- for (c, b) in blocks (t)
339- copy! (block (tdst, c), b)
336+ function insertrightunit (t:: AbstractTensorMap , :: Val{i} = Val (numind (t));
337+ copy:: Bool = false , conj:: Bool = false , dual:: Bool = false ) where {i}
338+ W = insertrightunit (space (t), Val (i); conj, dual)
339+ if t isa TensorMap
340+ return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
341+ else
342+ tdst = similar (t, W)
343+ for (c, b) in blocks (t)
344+ copy! (block (tdst, c), b)
345+ end
346+ return tdst
340347 end
341- return tdst
342- end
343- @constprop :aggressive function insertrightunit (t:: TensorMap , i:: Int = numind (t);
344- copy:: Bool = false , kwargs... )
345- W = insertrightunit (space (t), i; kwargs... )
346- return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
347348end
348349
349350"""
350- removeunit(tsrc::AbstractTensorMap, i::Int ; copy=false) -> tdst
351+ removeunit(tsrc::AbstractTensorMap, i; copy=false) -> tdst
351352
352- This removes a trivial tensor product factor at position `1 ≤ i ≤ N`.
353+ This removes a trivial tensor product factor at position `1 ≤ i ≤ N`, where `i`
354+ can be specified as an `Int` or as `Val(i)` for improved type stability.
353355For this to work, that factor has to be isomorphic to the field of scalars.
354356
355357If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
356358
357- This operation undoes the work of [`insertunit`](@ref).
359+ This operation undoes the work of [`insertleftunit`](@ref insertleftunit(::AbstractTensorMap, ::Val{i}) where {i})
360+ and [`insertrightunit`](@ref insertrightunit(::AbstractTensorMap, ::Val{i}) where {i}).
358361"""
359- @constprop :aggressive function removeunit (t:: TensorMap , i:: Int ; copy:: Bool = false )
360- W = removeunit (space (t), i)
361- return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
362- end
363- @constprop :aggressive function removeunit (t:: AbstractTensorMap , i:: Int ; copy:: Bool = true )
364- W = removeunit (space (t), i)
365- tdst = similar (t, W)
366- for (c, b) in blocks (t)
367- copy! (block (tdst, c), b)
362+ function removeunit (t:: AbstractTensorMap , :: Val{i} ; copy:: Bool = false ) where {i}
363+ W = removeunit (space (t), Val (i))
364+ if t isa TensorMap
365+ return TensorMap {scalartype(t)} (copy ? Base. copy (t. data) : t. data, W)
366+ else
367+ tdst = similar (t, W)
368+ for (c, b) in blocks (t)
369+ copy! (block (tdst, c), b)
370+ end
371+ return tdst
368372 end
369- return tdst
370373end
371374
372375# Fusing and splitting
0 commit comments