1+ diagspacelist = ((ℂ^ 4 )' , ℂ[Z2Irrep](0 => 2 , 1 => 3 ),
2+ ℂ[FermionNumber](0 => 2 , 1 => 2 , - 1 => 1 )' ,
3+ ℂ[SU2Irrep](0 => 2 , 1 => 1 ), ℂ[FibonacciAnyon](:I => 2 , :τ => 2 ))
4+
5+ @testset " DiagonalTensor with domain $V " for V in diagspacelist
6+ @timedtestset " Basic properties and algebra" begin
7+ for T in (Float32, Float64, ComplexF32, ComplexF64, BigFloat)
8+ t = @constinferred DiagonalTensorMap{T}(undef, V)
9+ t = @constinferred DiagonalTensorMap(rand(T, reduceddim(V)), V)
10+ @test @constinferred(hash(t)) == hash(deepcopy(t))
11+ @test scalartype(t) == T
12+ @test codomain(t) == ProductSpace(V)
13+ @test domain(t) == ProductSpace(V)
14+ @test space(t) == (V ← V)
15+ @test space(t' ) == (V ← V)
16+ @test dim(t) == dim(space(t))
17+ @test isa(@constinferred(norm(t)), real(T))
18+ @test norm(t)^2 ≈ dot(t, t)
19+ α = rand(T)
20+ @test norm(α * t) ≈ abs(α) * norm(t)
21+ @test norm(t + t, 2) ≈ 2 * norm(t, 2)
22+ @test norm(t + t, 1) ≈ 2 * norm(t, 1)
23+ @test norm(t + t, Inf) ≈ 2 * norm(t, Inf)
24+ p = 3 * rand(Float64)
25+ @test norm(t + t, p) ≈ 2 * norm(t, p)
26+ @test norm(t) ≈ norm(t' )
27+
28+ @test t == @constinferred(TensorMap(t))
29+ @test norm(t + TensorMap(t)) ≈ 2 * norm(t)
30+
31+ @test norm(zerovector!(t)) == 0
32+ @test norm(one!(t)) ≈ sqrt(dim(V))
33+ @test one!(t) == id(V)
34+ @test norm(one!(t) - id(V)) == 0
35+
36+ t1 = DiagonalTensorMap(rand(T, reduceddim(V)), V)
37+ t2 = DiagonalTensorMap(rand(T, reduceddim(V)), V)
38+ t3 = DiagonalTensorMap(rand(T, reduceddim(V)), V)
39+ α = rand(T)
40+ β = rand(T)
41+ @test @constinferred(dot(t1, t2)) ≈ conj(dot(t2, t1))
42+ @test dot(t2, t1) ≈ conj(dot(t2' , t1' ))
43+ @test dot(t3, α * t1 + β * t2) ≈ α * dot(t3, t1) + β * dot(t3, t2)
44+ end
45+ end
46+ I = sectortype(V)
47+ @timedtestset " Basic linear algebra: test via conversion" begin
48+ for T in (Float32, ComplexF64)
49+ t1 = DiagonalTensorMap(rand(T, reduceddim(V)), V)
50+ t2 = DiagonalTensorMap(rand(T, reduceddim(V)), V)
51+ @test norm(t1, 2 ) ≈ norm(convert(TensorMap, t1), 2 )
52+ @test dot(t2, t1) ≈ dot(convert(TensorMap, t2), convert(TensorMap, t1))
53+ α = rand(T)
54+ @test convert(TensorMap, α * t1) ≈ α * convert(TensorMap, t1)
55+ @test convert(TensorMap, t1' ) ≈ convert(TensorMap, t1)'
56+ @test convert(TensorMap, t1 + t2) ≈
57+ convert(TensorMap, t1) + convert(TensorMap, t2)
58+ end
59+ end
60+ @timedtestset " Real and imaginary parts" begin
61+ for T in (Float64, ComplexF64, ComplexF32)
62+ t = DiagonalTensorMap(rand(T, reduceddim(V)), V)
63+
64+ tr = @constinferred real(t)
65+ @test scalartype(tr) <: Real
66+ @test real(convert(TensorMap, t)) == convert(TensorMap, tr)
67+
68+ ti = @constinferred imag(t)
69+ @test scalartype(ti) <: Real
70+ @test imag(convert(TensorMap, t)) == convert(TensorMap, ti)
71+
72+ tc = @inferred complex(t)
73+ @test scalartype(tc) <: Complex
74+ @test complex(convert(TensorMap, t)) == convert(TensorMap, tc)
75+
76+ tc2 = @inferred complex(tr, ti)
77+ @test tc2 ≈ tc
78+ end
79+ end
80+ @timedtestset " Tensor conversion" begin
81+ t = @constinferred DiagonalTensorMap(undef, V)
82+ rand!(t. data)
83+ tc = complex(t)
84+ @test convert(typeof(tc), t) == tc
85+ @test typeof(convert(typeof(tc), t)) == typeof(tc)
86+ end
87+ I = sectortype(V)
88+ if BraidingStyle(I) isa SymmetricBraiding
89+ @timedtestset " Permutations" begin
90+ t = DiagonalTensorMap(randn(ComplexF64, reduceddim(V)), V)
91+ t1 = @constinferred permute(t, $ (((2 ,), (1 ,))))
92+ if BraidingStyle(sectortype(V)) isa Bosonic
93+ @test t1 == transpose(t)
94+ end
95+ @test convert(TensorMap, t1) == permute(convert(TensorMap, t), (((2 ,), (1 ,))))
96+ t2 = @constinferred permute(t, $ (((1 , 2 ), ())))
97+ @test convert(TensorMap, t2) == permute(convert(TensorMap, t), (((1 , 2 ), ())))
98+ t3 = @constinferred permute(t, $ (((2 , 1 ), ())))
99+ @test convert(TensorMap, t3) == permute(convert(TensorMap, t), (((2 , 1 ), ())))
100+ t4 = @constinferred permute(t, $ (((), (1 , 2 ))))
101+ @test convert(TensorMap, t4) == permute(convert(TensorMap, t), (((), (1 , 2 ))))
102+ t5 = @constinferred permute(t, $ (((), (2 , 1 ))))
103+ @test convert(TensorMap, t5) == permute(convert(TensorMap, t), (((), (2 , 1 ))))
104+ end
105+ end
106+ end
0 commit comments