Skip to content

Commit 0b5a954

Browse files
borisdevoslkdvos
authored andcommitted
introduce zerospace to replace zero of a space
1 parent 3237aa7 commit 0b5a954

9 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/TensorKit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export FibonacciAnyon, IsingAnyon
2020
export unit, rightunit, leftunit, allunits, isunit
2121

2222
export VectorSpace, Field, ElementarySpace # abstract vector spaces
23-
export unitspace
23+
export unitspace, zerospace
2424
export InnerProductStyle, NoInnerProduct, HasInnerProduct, EuclideanInnerProduct
2525
export ComplexSpace, CartesianSpace, GeneralSpace, GradedSpace # concrete spaces
2626
export ZNSpace, Z2Space, Z3Space, Z4Space, U1Space, CU1Space, SU2Space

src/spaces/cartesianspace.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ sectors(V::CartesianSpace) = OneOrNoneIterator(dim(V) != 0, Trivial())
4848
sectortype(::Type{CartesianSpace}) = Trivial
4949

5050
unitspace(::Type{CartesianSpace}) = CartesianSpace(1)
51-
Base.zero(::Type{CartesianSpace}) = CartesianSpace(0)
52-
51+
zerospace(::Type{CartesianSpace}) = CartesianSpace(0)
5352
(V₁::CartesianSpace, V₂::CartesianSpace) = CartesianSpace(V₁.d + V₂.d)
5453
function (V::CartesianSpace, W::CartesianSpace)
5554
V W || throw(ArgumentError("$(W) is not a subspace of $(V)"))

src/spaces/complexspace.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ sectortype(::Type{ComplexSpace}) = Trivial
4949
Base.conj(V::ComplexSpace) = ComplexSpace(dim(V), !isdual(V))
5050

5151
unitspace(::Type{ComplexSpace}) = ComplexSpace(1)
52-
Base.zero(::Type{ComplexSpace}) = ComplexSpace(0)
53-
52+
zerospace(::Type{ComplexSpace}) = ComplexSpace(0)
5453
function (V₁::ComplexSpace, V₂::ComplexSpace)
5554
return isdual(V₁) == isdual(V₂) ?
5655
ComplexSpace(dim(V₁) + dim(V₂), isdual(V₁)) :

src/spaces/generalspace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ field(::Type{GeneralSpace{𝔽}}) where {𝔽} = 𝔽
3636
InnerProductStyle(::Type{<:GeneralSpace}) = NoInnerProduct()
3737

3838
unitspace(::Type{GeneralSpace{𝔽}}) where {𝔽} = GeneralSpace{𝔽}(1, false, false)
39-
Base.zero(::Type{GeneralSpace{𝔽}}) where {𝔽} = GeneralSpace{𝔽}(0, false, false)
39+
zerospace(::Type{GeneralSpace{𝔽}}) where {𝔽} = GeneralSpace{𝔽}(0, false, false)
4040

4141
dual(V::GeneralSpace{𝔽}) where {𝔽} = GeneralSpace{𝔽}(dim(V), !isdual(V), isconj(V))
4242
Base.conj(V::GeneralSpace{𝔽}) where {𝔽} = GeneralSpace{𝔽}(dim(V), isdual(V), !isconj(V))

src/spaces/gradedspace.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ function Base.axes(V::GradedSpace{I}, c::I) where {I <: Sector}
134134
return (offset + 1):(offset + dim(c) * dim(V, c))
135135
end
136136

137-
unitspace(S::Type{<:GradedSpace{I}}) where {I <: Sector} = S(unit(I) => 1)
138-
Base.zero(S::Type{<:GradedSpace{I}}) where {I <: Sector} = S(unit(I) => 0)
137+
unitspace(S::Type{<:GradedSpace{I}}) where {I<:Sector} = S(unit(I) => 1)
138+
zerospace(S::Type{<:GradedSpace{I}}) where {I<:Sector} = S(unit(I) => 0)
139139

140140
# TODO: the following methods can probably be implemented more efficiently for
141141
# `FiniteGradedSpace`, but we don't expect them to be used often in hot loops, so

src/spaces/homspace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
function HomSpace(codomain::S, domain::S) where {S <: ElementarySpace}
2323
return HomSpace((codomain), (domain))
2424
end
25-
HomSpace(codomain::VectorSpace) = HomSpace(codomain, zero(codomain))
25+
HomSpace(codomain::VectorSpace) = HomSpace(codomain, zerospace(codomain))
2626

2727
codomain(W::HomSpace) = W.codomain
2828
domain(W::HomSpace) = W.domain

src/spaces/vectorspaces.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,18 @@ that this is different from `one(V::S)`, which returns the empty product space
129129
"""
130130
unitspace(V::ElementarySpace) = unitspace(typeof(V))
131131
Base.oneunit(V::ElementarySpace) = unitspace(V)
132+
#TODO: add for type
132133

133134
"""
134-
zero(V::S) where {S<:ElementarySpace} -> S
135+
zerospace(V::S) where {S<:ElementarySpace} -> S
135136
136137
Return the corresponding vector space of type `S` that represents the zero-dimensional or empty space.
137-
This is, with a slight abuse of notation, the zero element of the direct sum of vector spaces.
138+
This is, with a slight abuse of notation, the zero element of the direct sum of vector spaces.
139+
`Base.zero` falls back to `zerospace`.
138140
"""
139-
#TODO: zerospace?
140-
Base.zero(V::ElementarySpace) = zero(typeof(V))
141+
zerospace(V::ElementarySpace) = zerospace(typeof(V))
142+
Base.zero(V::ElementarySpace) = zerospace(V)
143+
Base.zero(::Type{V}) where {V<:ElementarySpace} = zerospace(V)
141144

142145
"""
143146
⊕(V₁::S, V₂::S, V₃::S...) where {S<:ElementarySpace} -> S

test/spaces.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ println("------------------------------------")
6565
@test length(sectors(V)) == 1
6666
@test @constinferred(TensorKit.hassector(V, Trivial()))
6767
@test @constinferred(dim(V)) == d == @constinferred(dim(V, Trivial()))
68-
@test dim(@constinferred(zero(V))) == 0
69-
@test (sectors(zero(V))...,) == ()
68+
@test dim(@constinferred(zerospace(V))) == 0
69+
@test (sectors(zerospace(V))...,) == ()
7070
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(d)
7171
@test^d == ℝ[](d) == CartesianSpace(d) == typeof(V)(d)
7272
W = @constinferred^1
7373
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
74-
@test @constinferred(zero(V)) ==^0 == zero(typeof(V))
75-
@test @constinferred((V, zero(V))) == V
74+
@test @constinferred(zerospace(V)) ==^0 == zerospace(typeof(V))
75+
@test @constinferred((V, zerospace(V))) == V
7676
@test @constinferred((V, V)) ==^(2d)
7777
@test @constinferred((V, unitspace(V))) ==^(d + 1)
7878
@test @constinferred((V, V, V, V)) ==^(4d)
@@ -111,14 +111,14 @@ println("------------------------------------")
111111
@test length(sectors(V)) == 1
112112
@test @constinferred(TensorKit.hassector(V, Trivial()))
113113
@test @constinferred(dim(V)) == d == @constinferred(dim(V, Trivial()))
114-
@test dim(@constinferred(zero(V))) == 0
115-
@test (sectors(zero(V))...,) == ()
114+
@test dim(@constinferred(zerospace(V))) == 0
115+
@test (sectors(zerospace(V))...,) == ()
116116
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(d)
117117
@test^d == Vect[Trivial](d) == Vect[](Trivial() => d) == ℂ[](d) == typeof(V)(d)
118118
W = @constinferred^1
119119
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
120-
@test @constinferred(zero(V)) ==^0 == zero(typeof(V))
121-
@test @constinferred((V, zero(V))) == V
120+
@test @constinferred(zerospace(V)) ==^0 == zerospace(typeof(V))
121+
@test @constinferred((V, zerospace(V))) == V
122122
@test @constinferred((V, V)) ==^(2d)
123123
@test_throws SpaceMismatch ((V, V'))
124124
# promote_except = ErrorException("promotion of types $(typeof(ℝ^d)) and " *
@@ -202,12 +202,12 @@ println("------------------------------------")
202202
@test eval(Meta.parse(sprint(show, V))) == V
203203
@test eval(Meta.parse(sprint(show, typeof(V)))) == typeof(V)
204204
# space with no sectors
205-
@test dim(@constinferred(zero(V))) == 0
205+
@test dim(@constinferred(zerospace(V))) == 0
206206
# space with a single sector
207207
W = @constinferred GradedSpace(unit(I) => 1)
208208
@test W == GradedSpace(unit(I) => 1, randsector(I) => 0)
209209
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
210-
@test @constinferred(zero(V)) == GradedSpace(unit(I) => 0)
210+
@test @constinferred(zerospace(V)) == GradedSpace(unit(I) => 0)
211211
# randsector never returns trivial sector, so this cannot error
212212
@test_throws ArgumentError GradedSpace(unit(I) => 1, randsector(I) => 0, unit(I) => 3)
213213
@test eval(Meta.parse(sprint(show, W))) == W
@@ -228,7 +228,7 @@ println("------------------------------------")
228228
if hasfusiontensor(I)
229229
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(dim(V))
230230
end
231-
@test @constinferred((V, zero(V))) == V
231+
@test @constinferred((V, zerospace(V))) == V
232232
@test @constinferred((V, V)) == Vect[I](c => 2dim(V, c) for c in sectors(V))
233233
@test @constinferred((V, V, V, V)) == Vect[I](c => 4dim(V, c) for c in sectors(V))
234234
@test @constinferred((V, oneunit(V))) == Vect[I](c => isunit(c) + dim(V, c) for c in sectors(V))

test/tensors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ for V in spacelist
8080
end
8181
end
8282
for T in (Int, Float32, ComplexF64)
83-
t = randn(T, V1 V2 zero(V1))
83+
t = randn(T, V1 V2 zerospace(V1))
8484
a = convert(Array, t)
8585
@test norm(a) == 0
8686
end

0 commit comments

Comments
 (0)