Skip to content

Commit 51355b8

Browse files
committed
various utility functions
1 parent a5811da commit 51355b8

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/spaces/homspace.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const TensorMapSpace{S <: ElementarySpace, N₁, N₂} = HomSpace{
4848
S, ProductSpace{S, N₁},
4949
ProductSpace{S, N₂},
5050
}
51+
numout(::Type{TensorMapSpace{S, N₁, N₂}}) where {S, N₁, N₂} = N₁
52+
numin(::Type{TensorMapSpace{S, N₁, N₂}}) where {S, N₁, N₂} = N₂
53+
numind(T::Type{TensorMapSpace{S, N₁, N₂}}) where {S, N₁, N₂} = numout(T) + numin(T)
5154

5255
function Base.getindex(W::TensorMapSpace{<:IndexSpace, N₁, N₂}, i) where {N₁, N₂}
5356
return i <= N₁ ? codomain(W)[i] : dual(domain(W)[i - N₁])
@@ -137,18 +140,33 @@ fusiontrees(W::HomSpace) = fusionblockstructure(W).fusiontreelist
137140
# Operations on HomSpaces
138141
# -----------------------
139142
"""
140-
permute(W::HomSpace, (p₁, p₂)::Index2Tuple{N₁,N₂})
143+
permute(W::HomSpace, (p₁, p₂)::Index2Tuple)
141144
142145
Return the `HomSpace` obtained by permuting the indices of the domain and codomain of `W`
143146
according to the permutation `p₁` and `p₂` respectively.
144147
"""
145-
function permute(W::HomSpace{S}, (p₁, p₂)::Index2Tuple{N₁, N₂}) where {S, N₁, N₂}
148+
function permute(W::HomSpace, (p₁, p₂)::Index2Tuple)
146149
p = (p₁..., p₂...)
147150
TupleTools.isperm(p) && length(p) == numind(W) ||
148151
throw(ArgumentError("$((p₁, p₂)) is not a valid permutation for $(W)"))
149152
return select(W, (p₁, p₂))
150153
end
151154

155+
_transpose_indices(W::HomSpace) = (reverse(domainind(W)), reverse(codomainind(W)))
156+
157+
function LinearAlgebra.transpose(W::HomSpace, (p₁, p₂)::Index2Tuple = _transpose_indices(W))
158+
p = linearizepermutation(p₁, p₂, numout(W), numin(W))
159+
iscyclicpermutation(p) || throw(ArgumentError(lazy"$((p₁, p₂)) is not a cyclic permutation for $W"))
160+
return select(W, (p₁, p₂))
161+
end
162+
163+
function braid(W::HomSpace, (p₁, p₂)::Index2Tuple, lvls::IndexTuple)
164+
p = (p₁..., p₂...)
165+
TupleTools.isperm(p) && length(p) == numind(W) == length(lvls) ||
166+
throw(ArgumentError("$((p₁, p₂)), $lvls is not a valid braiding for $(W)"))
167+
return select(W, (p₁, p₂))
168+
end
169+
152170
"""
153171
select(W::HomSpace, (p₁, p₂)::Index2Tuple{N₁,N₂})
154172

src/tensors/abstracttensor.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,40 @@ numind(::Type{TT}) where {TT <: AbstractTensorMap} = numin(TT) + numout(TT)
119119
const order = numind
120120

121121
"""
122-
codomainind(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Tuple{Int}
122+
codomainind(::Union{TT, Type{TT}}) where {TT <: Union{HomSpace, AbstractTensorMap}} -> Tuple{Int}
123123
124-
Return all indices of the codomain of a tensor.
124+
Return all indices of the codomain of a tensor or tensor space.
125125
126126
See also [`domainind`](@ref) and [`allind`](@ref).
127127
"""
128-
function codomainind(::Type{TT}) where {TT <: AbstractTensorMap}
128+
function codomainind(::Type{TT}) where {TT <: Union{AbstractTensorMap, HomSpace}}
129129
return ntuple(identity, numout(TT))
130130
end
131-
codomainind(t::AbstractTensorMap) = codomainind(typeof(t))
131+
codomainind(t::Union{AbstractTensorMap, HomSpace}) = codomainind(typeof(t))
132132

133133
"""
134134
domainind(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Tuple{Int}
135135
136-
Return all indices of the domain of a tensor.
136+
Return all indices of the domain of a tensor or tensor space.
137137
138138
See also [`codomainind`](@ref) and [`allind`](@ref).
139139
"""
140-
function domainind(::Type{TT}) where {TT <: AbstractTensorMap}
140+
function domainind(::Type{TT}) where {TT <: Union{HomSpace, AbstractTensorMap}}
141141
return ntuple(n -> numout(TT) + n, numin(TT))
142142
end
143-
domainind(t::AbstractTensorMap) = domainind(typeof(t))
143+
domainind(t::Union{AbstractTensorMap, HomSpace}) = domainind(typeof(t))
144144

145145
"""
146146
allind(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Tuple{Int}
147147
148-
Return all indices of a tensor, i.e. the indices of its domain and codomain.
148+
Return all indices of a tensor or tensor space, i.e. the indices of its domain and codomain.
149149
150150
See also [`codomainind`](@ref) and [`domainind`](@ref).
151151
"""
152-
function allind(::Type{TT}) where {TT <: AbstractTensorMap}
152+
function allind(::Type{TT}) where {TT <: Union{HomSpace, AbstractTensorMap}}
153153
return ntuple(identity, numind(TT))
154154
end
155-
allind(t::AbstractTensorMap) = allind(typeof(t))
155+
allind(t::Union{HomSpace, AbstractTensorMap}) = allind(typeof(t))
156156

157157
function adjointtensorindex(t::AbstractTensorMap, i)
158158
return ifelse(i <= numout(t), numin(t) + i, i - numout(t))

0 commit comments

Comments
 (0)