Skip to content

Commit 14bd37d

Browse files
committed
centralize numin/numout/numind
1 parent 1be36e5 commit 14bd37d

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/spaces/homspace.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@ end
3939

4040
spacetype(::Type{<:HomSpace{S}}) where {S} = S
4141

42-
numout(W::HomSpace) = length(codomain(W))
43-
numin(W::HomSpace) = length(domain(W))
44-
numind(W::HomSpace) = numin(W) + numout(W)
45-
4642
const TensorSpace{S <: ElementarySpace} = Union{S, ProductSpace{S}}
4743
const TensorMapSpace{S <: ElementarySpace, N₁, N₂} = HomSpace{
4844
S, ProductSpace{S, N₁},
4945
ProductSpace{S, N₂},
5046
}
47+
5148
numout(::Type{TensorMapSpace{S, N₁, N₂}}) where {S, N₁, N₂} = N₁
5249
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)
5450

5551
function Base.getindex(W::TensorMapSpace{<:IndexSpace, N₁, N₂}, i) where {N₁, N₂}
5652
return i <= N₁ ? codomain(W)[i] : dual(domain(W)[i - N₁])

src/tensors/abstracttensor.jl

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,70 +89,74 @@ domain(t::AbstractTensorMap) = domain(space(t))
8989
domain(t::AbstractTensorMap, i) = domain(t)[i]
9090
source(t::AbstractTensorMap) = domain(t) # categorical terminology
9191

92-
"""
93-
numout(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Int
92+
@doc """
93+
numout(x) -> Int
94+
numout(T::Type) -> Int
9495
95-
Return the number of output spaces of a tensor. This is equivalent to the number of spaces in the codomain of that tensor.
96+
Return the length of the codomain, i.e. the number of output spaces.
97+
By default, this is implemented in the type domain.
9698
9799
See also [`numin`](@ref) and [`numind`](@ref).
98-
"""
100+
""" numout
101+
102+
numout(x) = numout(typeof(x))
103+
numout(T::Type) = throw(MethodError(numout, T)) # avoid infinite recursion
99104
numout(::Type{<:AbstractTensorMap{T, S, N₁}}) where {T, S, N₁} = N₁
100105

101-
"""
102-
numin(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Int
106+
@doc """
107+
numin(x) -> Int
108+
numin(T::Type) -> Int
103109
104-
Return the number of input spaces of a tensor. This is equivalent to the number of spaces in the domain of that tensor.
110+
Return the length of the domain, i.e. the number of input spaces.
111+
By default, this is implemented in the type domain.
105112
106113
See also [`numout`](@ref) and [`numind`](@ref).
107-
"""
114+
""" numin
115+
116+
numin(x) = numin(typeof(x))
117+
numin(T::Type) = throw(MethodError(numin, T)) # avoid infinite recursion
108118
numin(::Type{<:AbstractTensorMap{T, S, N₁, N₂}}) where {T, S, N₁, N₂} = N₂
109119

110120
"""
111-
numind(::Union{T,Type{T}}) where {T<:AbstractTensorMap} -> Int
121+
numind(x) -> Int
122+
numind(T::Type) -> Int
123+
order(x) = numind(x)
112124
113-
Return the total number of input and output spaces of a tensor. This is equivalent to the
114-
total number of spaces in the domain and codomain of that tensor.
125+
Return the total number of input and output spaces, i.e. `numin(x) + numout(x)`.
126+
Alternatively, the alias `order` can also be used.
115127
116128
See also [`numout`](@ref) and [`numin`](@ref).
117129
"""
118-
numind(::Type{TT}) where {TT <: AbstractTensorMap} = numin(TT) + numout(TT)
130+
numind(x) = numin(x) + numout(x)
131+
119132
const order = numind
120133

121134
"""
122-
codomainind(::Union{TT, Type{TT}}) where {TT <: Union{HomSpace, AbstractTensorMap}} -> Tuple{Int}
135+
codomainind(x) -> Tuple{Int}
123136
124-
Return all indices of the codomain of a tensor or tensor space.
137+
Return all indices of the codomain.
125138
126139
See also [`domainind`](@ref) and [`allind`](@ref).
127140
"""
128-
function codomainind(::Type{TT}) where {TT <: Union{AbstractTensorMap, HomSpace}}
129-
return ntuple(identity, numout(TT))
130-
end
131-
codomainind(t::Union{AbstractTensorMap, HomSpace}) = codomainind(typeof(t))
141+
codomainind(x) = ntuple(identity, numout(x))
132142

133143
"""
134-
domainind(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Tuple{Int}
144+
domainind(x) -> Tuple{Int}
135145
136-
Return all indices of the domain of a tensor or tensor space.
146+
Return all indices of the domain.
137147
138148
See also [`codomainind`](@ref) and [`allind`](@ref).
139149
"""
140-
function domainind(::Type{TT}) where {TT <: Union{HomSpace, AbstractTensorMap}}
141-
return ntuple(n -> numout(TT) + n, numin(TT))
142-
end
143-
domainind(t::Union{AbstractTensorMap, HomSpace}) = domainind(typeof(t))
150+
domainind(x) = ntuple(n -> numout(x) + n, numin(x))
144151

145152
"""
146-
allind(::Union{TT,Type{TT}}) where {TT<:AbstractTensorMap} -> Tuple{Int}
153+
allind(x) -> Tuple{Int}
147154
148-
Return all indices of a tensor or tensor space, i.e. the indices of its domain and codomain.
155+
Return all indices, i.e. the indices of both domain and codomain.
149156
150157
See also [`codomainind`](@ref) and [`domainind`](@ref).
151158
"""
152-
function allind(::Type{TT}) where {TT <: Union{HomSpace, AbstractTensorMap}}
153-
return ntuple(identity, numind(TT))
154-
end
155-
allind(t::Union{HomSpace, AbstractTensorMap}) = allind(typeof(t))
159+
allind(x) = ntuple(identity, numind(x))
156160

157161
function adjointtensorindex(t, i)
158162
return ifelse(i <= numout(t), numin(t) + i, i - numout(t))

0 commit comments

Comments
 (0)