Skip to content

Commit 434df93

Browse files
authored
Fix bugs in GradedSpace constructor + similar of SectorVector (#498)
* two small bugs * add tests * remove line
1 parent 313dff4 commit 434df93

4 files changed

Lines changed: 144 additions & 2 deletions

File tree

‎src/spaces/gradedspace.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function GradedSpace{I, D}(d1::Pair, d2::Pair, dims::Vararg{Pair}; kwargs...) wh
6565
return GradedSpace{I, D}((d1, d2, dims...); kwargs...)
6666
end
6767

68-
GradedSpace{I}(args...; kwargs...) where {I <: Sector} = Vect[I](args..., kwargs...)
68+
GradedSpace{I}(args...; kwargs...) where {I <: Sector} = Vect[I](args...; kwargs...)
6969

7070
function GradedSpace(
7171
dims::Tuple{Pair{I, <:Integer}, Vararg{Pair{I, <:Integer}}}; dual::Bool = false

‎src/tensors/sectorvector.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Base.size(v::SectorVector, args...) = size(parent(v), args...)
3737
Base.similar(v::SectorVector) = SectorVector(similar(v.data), v.structure)
3838
Base.similar(v::SectorVector, ::Type{T}) where {T} = SectorVector(similar(v.data, T), v.structure)
3939
Base.similar(v::SectorVector, V::ElementarySpace) = SectorVector{eltype(v), sectortype(V), storagetype(v)}(undef, V)
40-
Base.similar(v::SectorVector, ::Type{T}, V::ElementarySpace) where {T} = SectorVector(similar(v.data, T, reduceddim(V)), V)
40+
Base.similar(v::SectorVector, ::Type{T}, V::ElementarySpace) where {T} = SectorVector(similar(v.data, T, reduceddim(V)), diagonalblockstructure(V ← V))
4141

4242
Base.copy(v::SectorVector) = SectorVector(copy(v.data), v.structure)
4343

‎test/symmetries/spaces.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ end
199199
@test eval_show(V) == V
200200
@test eval_show(V') == V'
201201
@test V' == GradedSpace(gen; dual = true)
202+
@test V' == GradedSpace{I}(gen; dual = true)
202203
@test V == @constinferred GradedSpace(gen...)
203204
@test V' == @constinferred GradedSpace(gen...; dual = true)
204205
@test V == @constinferred GradedSpace(tuple(gen...))

‎test/tensors/sectorvector.jl‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using TensorKit: SectorVector, diagonalblockstructure
4+
5+
spacelist = (
6+
(ℂ^4)',
7+
Vect[Z2Irrep](0 => 2, 1 => 3),
8+
Vect[SU2Irrep](0 => 2, 1 => 1)',
9+
Vect[FibonacciAnyon](:I => 2, :τ => 2),
10+
)
11+
12+
@testset "SectorVector with space $V" for V in spacelist
13+
I = sectortype(V)
14+
for T in (Float32, Float64, ComplexF32, ComplexF64)
15+
@timedtestset "Constructors ($T)" begin
16+
v = @testinferred SectorVector{T}(undef, V)
17+
@test v isa SectorVector{T, I, Vector{T}}
18+
@test length(v) == reduceddim(V)
19+
@test sectortype(v) == I
20+
@test storagetype(typeof(v)) == Vector{T}
21+
22+
structure = diagonalblockstructure(V ← V)
23+
data = rand(T, reduceddim(V))
24+
v2 = @testinferred SectorVector(data, structure)
25+
@test parent(v2) === data
26+
@test v2.structure == structure
27+
28+
v3 = @testinferred SectorVector{T, I, Vector{T}}(undef, V)
29+
@test length(v3) == reduceddim(V)
30+
end
31+
32+
@timedtestset "AbstractVector interface ($T)" begin
33+
v = SectorVector{T}(undef, V)
34+
rand!(parent(v))
35+
@test @testinferred(eltype(v)) == T
36+
@test length(v) == length(parent(v))
37+
oldval = v[1]
38+
v[1] = one(T)
39+
@test v[1] == one(T) == parent(v)[1]
40+
end
41+
42+
@timedtestset "similar and copy ($T)" begin
43+
v = SectorVector{T}(undef, V)
44+
rand!(parent(v))
45+
46+
v1 = @testinferred similar(v)
47+
@test v1 isa typeof(v)
48+
@test v1.structure == v.structure
49+
@test length(v1) == length(v)
50+
51+
v2 = @testinferred similar(v, ComplexF64)
52+
@test eltype(v2) == ComplexF64
53+
@test v2.structure == v.structure
54+
55+
v3 = @testinferred similar(v, V)
56+
@test v3 isa SectorVector{T, I, Vector{T}}
57+
@test v3.structure == diagonalblockstructure(V ← V)
58+
59+
v4 = @testinferred similar(v, ComplexF64, V)
60+
@test v4 isa SectorVector{ComplexF64, I, Vector{ComplexF64}}
61+
@test v4.structure == diagonalblockstructure(V ← V)
62+
@test length(v4) == reduceddim(V)
63+
64+
v5 = @testinferred copy(v)
65+
@test v5 == v
66+
@test parent(v5) !== parent(v)
67+
@test v5.structure == v.structure
68+
end
69+
70+
@timedtestset "AbstractDict interface ($T)" begin
71+
v = SectorVector{T}(undef, V)
72+
rand!(parent(v))
73+
@test @testinferred(keytype(v)) == I
74+
@test @testinferred(keytype(typeof(v))) == I
75+
@test valtype(v) == SubArray{T, 1, Vector{T}, Tuple{UnitRange{Int}}, true}
76+
77+
for c in sectors(V)
78+
@test @testinferred(haskey(v, c))
79+
@test v[c] == view(parent(v), v.structure[c])
80+
newvals = rand(T, length(v[c]))
81+
v[c] = newvals
82+
@test v[c] == newvals
83+
end
84+
@test collect(@testinferred(keys(v))) == collect(sectors(V))
85+
@test collect(values(v)) == [v[c] for c in keys(v)]
86+
@test pairs(v) == Dict(c => v[c] for c in keys(v))
87+
end
88+
89+
@timedtestset "VectorInterface ($T)" begin
90+
v1 = SectorVector{T}(undef, V)
91+
rand!(parent(v1))
92+
v2 = SectorVector{T}(undef, V)
93+
rand!(parent(v2))
94+
95+
z = @testinferred zerovector(v1, T)
96+
@test all(iszero, parent(z))
97+
@test z.structure == v1.structure
98+
v1c = deepcopy(v1)
99+
zerovector!(v1c)
100+
@test all(iszero, parent(v1c))
101+
zerovector!!(v1c)
102+
@test all(iszero, parent(v1c))
103+
104+
α = rand(T)
105+
vs = @testinferred scale(v1, α)
106+
@test parent(vs) ≈ α * parent(v1)
107+
v1c = deepcopy(v1)
108+
scale!(v1c, α)
109+
@test parent(v1c) ≈ α * parent(v1)
110+
v1c = deepcopy(v1)
111+
scale!!(v1c, α)
112+
@test parent(v1c) ≈ α * parent(v1)
113+
114+
β = rand(T)
115+
# VectorInterface convention: add(x, y, α, β) computes β*x + α*y
116+
va = @testinferred add(v1, v2, α, β)
117+
@test parent(va) ≈ β * parent(v1) + α * parent(v2)
118+
v1c = deepcopy(v1)
119+
add!(v1c, v2, α, β)
120+
@test parent(v1c) ≈ β * parent(v1) + α * parent(v2)
121+
v1c = deepcopy(v1)
122+
add!!(v1c, v2, α, β)
123+
@test parent(v1c) ≈ β * parent(v1) + α * parent(v2)
124+
125+
@test @testinferred(inner(v1, v2)) ≈ dot(v1, v2)
126+
v3 = SectorVector{T}(undef, V ⊕ V)
127+
@test_throws SpaceMismatch inner(v1, v3)
128+
end
129+
130+
@timedtestset "LinearAlgebra ($T)" begin
131+
v = SectorVector{T}(undef, V)
132+
rand!(parent(v))
133+
@test norm(v)^2 ≈ dot(v, v)
134+
@test norm(v) ≈ @testinferred(norm(v, 2))
135+
for p in (1, 2, Inf)
136+
@test norm(v, p) isa real(T)
137+
@test norm(v, p) >= 0
138+
end
139+
end
140+
end
141+
end

0 commit comments

Comments
 (0)