Skip to content

Commit 09cee6d

Browse files
committed
introduce zerospace to replace zero of a space
1 parent 3a2e67c commit 09cee6d

File tree

9 files changed

+27
-24
lines changed

9 files changed

+27
-24
lines changed

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +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)
51+
zerospace(::Type{CartesianSpace}) = CartesianSpace(0)
5252
(V₁::CartesianSpace, V₂::CartesianSpace) = CartesianSpace(V₁.d + V₂.d)
5353
fuse(V₁::CartesianSpace, V₂::CartesianSpace) = CartesianSpace(V₁.d * V₂.d)
5454
flip(V::CartesianSpace) = V

src/spaces/complexspace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +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)
52+
zerospace(::Type{ComplexSpace}) = ComplexSpace(0)
5353
function (V₁::ComplexSpace, V₂::ComplexSpace)
5454
return isdual(V₁) == isdual(V₂) ?
5555
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function Base.axes(V::GradedSpace{I}, c::I) where {I<:Sector}
132132
end
133133

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

137137
# TODO: the following methods can probably be implemented more efficiently for
138138
# `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
@@ -66,14 +66,14 @@ println("------------------------------------")
6666
@test length(sectors(V)) == 1
6767
@test @constinferred(TensorKit.hassector(V, Trivial()))
6868
@test @constinferred(dim(V)) == d == @constinferred(dim(V, Trivial()))
69-
@test dim(@constinferred(zero(V))) == 0
70-
@test (sectors(zero(V))...,) == ()
69+
@test dim(@constinferred(zerospace(V))) == 0
70+
@test (sectors(zerospace(V))...,) == ()
7171
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(d)
7272
@test^d == ℝ[](d) == CartesianSpace(d) == typeof(V)(d)
7373
W = @constinferred^1
7474
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
75-
@test @constinferred(zero(V)) ==^0 == zero(typeof(V))
76-
@test @constinferred((V, zero(V))) == V
75+
@test @constinferred(zerospace(V)) ==^0 == zerospace(typeof(V))
76+
@test @constinferred((V, zerospace(V))) == V
7777
@test @constinferred((V, V)) ==^(2d)
7878
@test @constinferred((V, unitspace(V))) ==^(d + 1)
7979
@test @constinferred((V, V, V, V)) ==^(4d)
@@ -113,14 +113,14 @@ println("------------------------------------")
113113
@test length(sectors(V)) == 1
114114
@test @constinferred(TensorKit.hassector(V, Trivial()))
115115
@test @constinferred(dim(V)) == d == @constinferred(dim(V, Trivial()))
116-
@test dim(@constinferred(zero(V))) == 0
117-
@test (sectors(zero(V))...,) == ()
116+
@test dim(@constinferred(zerospace(V))) == 0
117+
@test (sectors(zerospace(V))...,) == ()
118118
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(d)
119119
@test^d == Vect[Trivial](d) == Vect[](Trivial() => d) == ℂ[](d) == typeof(V)(d)
120120
W = @constinferred^1
121121
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
122-
@test @constinferred(zero(V)) ==^0 == zero(typeof(V))
123-
@test @constinferred((V, zero(V))) == V
122+
@test @constinferred(zerospace(V)) ==^0 == zerospace(typeof(V))
123+
@test @constinferred((V, zerospace(V))) == V
124124
@test @constinferred((V, V)) ==^(2d)
125125
@test_throws SpaceMismatch ((V, V'))
126126
# promote_except = ErrorException("promotion of types $(typeof(ℝ^d)) and " *
@@ -204,12 +204,12 @@ println("------------------------------------")
204204
@test eval(Meta.parse(sprint(show, V))) == V
205205
@test eval(Meta.parse(sprint(show, typeof(V)))) == typeof(V)
206206
# space with no sectors
207-
@test dim(@constinferred(zero(V))) == 0
207+
@test dim(@constinferred(zerospace(V))) == 0
208208
# space with a single sector
209209
W = @constinferred GradedSpace(unit(I) => 1)
210210
@test W == GradedSpace(unit(I) => 1, randsector(I) => 0)
211211
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
212-
@test @constinferred(zero(V)) == GradedSpace(unit(I) => 0)
212+
@test @constinferred(zerospace(V)) == GradedSpace(unit(I) => 0)
213213
# randsector never returns trivial sector, so this cannot error
214214
@test_throws ArgumentError GradedSpace(unit(I) => 1, randsector(I) => 0, unit(I) => 3)
215215
@test eval(Meta.parse(sprint(show, W))) == W
@@ -231,7 +231,7 @@ println("------------------------------------")
231231
if hasfusiontensor(I)
232232
@test @constinferred(TensorKit.axes(V)) == Base.OneTo(dim(V))
233233
end
234-
@test @constinferred((V, zero(V))) == V
234+
@test @constinferred((V, zerospace(V))) == V
235235
@test @constinferred((V, V)) == Vect[I](c => 2dim(V, c) for c in sectors(V))
236236
@test @constinferred((V, V, V, V)) == Vect[I](c => 4dim(V, c) for c in sectors(V))
237237
@test @constinferred((V, unitspace(V))) ==

test/tensors.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ for V in spacelist
8686
end
8787
end
8888
for T in (Int, Float32, ComplexF64)
89-
t = randn(T, V1 V2 zero(V1))
89+
t = randn(T, V1 V2 zerospace(V1))
9090
a = convert(Array, t)
9191
@test norm(a) == 0
9292
end
@@ -525,7 +525,7 @@ for V in spacelist
525525
end
526526
end
527527
@testset "empty tensor" begin
528-
t = randn(T, V1 V2, zero(V1))
528+
t = randn(T, V1 V2, zerospace(V1))
529529
@testset "leftorth with $alg" for alg in
530530
(TensorKit.QR(), TensorKit.QRpos(),
531531
TensorKit.QL(), TensorKit.QLpos(),
@@ -565,7 +565,7 @@ for V in spacelist
565565
end
566566
@testset "cond and rank" begin
567567
@test rank(t) == 0
568-
W2 = zero(V1) * zero(V2)
568+
W2 = zerospace(V1) * zerospace(V2)
569569
t2 = rand(W2, W2)
570570
@test rank(t2) == 0
571571
@test cond(t2) == 0.0

0 commit comments

Comments
 (0)