Skip to content

Commit 10ff55c

Browse files
committed
do not forbid empty NamedTuple
1 parent 22e9ac9 commit 10ff55c

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

NDTensors/src/lib/Sectors/src/category_product.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CategoryProduct(c::CategoryProduct) = _CategoryProduct(categories(c))
1515

1616
categories(s::CategoryProduct) = s.cats
1717

18-
const TrivialSector = CategoryProduct{Tuple{}}
18+
const TrivialSector{Categories<:Union{Tuple{},NamedTuple{()}}} = CategoryProduct{Categories}
1919
TrivialSector() = CategoryProduct(())
2020

2121
# ================================= Sectors interface ====================================
@@ -207,6 +207,8 @@ function SymmetryStyle(T::Type{<:Tuple})
207207
return mapreduce(SymmetryStyle, combine_styles, fieldtypes(T); init=AbelianStyle())
208208
end
209209

210+
categories_product(::NamedTuple{()}, l1::Tuple) = l1
211+
categories_product(l2::Tuple, ::NamedTuple{()}) = l2
210212
categories_product(l1::Tuple, l2::Tuple) = (l1..., l2...)
211213

212214
categories_trivial(type::Type{<:Tuple}) = trivial.(fieldtypes(type))
@@ -235,9 +237,6 @@ end
235237

236238
CategoryProduct(; kws...) = CategoryProduct((; kws...))
237239

238-
# avoid having 2 different kinds of TrivialSector: cast empty NamedTuple to Tuple{}
239-
CategoryProduct(::NamedTuple{()}) = CategoryProduct(())
240-
241240
function CategoryProduct(pairs::Pair...)
242241
keys = ntuple(n -> Symbol(pairs[n][1]), length(pairs))
243242
vals = ntuple(n -> pairs[n][2], length(pairs))

NDTensors/src/lib/Sectors/test/test_category_product.jl

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -542,58 +542,58 @@ end
542542
end
543543

544544
@testset "Empty category" begin
545-
s = TrivialSector()
546-
@test s == s
547-
@test (@inferred dual(s)) == s
548-
@test (@inferred s × s) == s
549-
@test (@inferred s s) == s
550-
@test (@inferred quantum_dimension(s)) == 1
551-
@test (@inferred_latest trivial(s)) == s
552-
@test typeof(s) == typeof(CategoryProduct(()))
553-
@test typeof(s) == typeof(CategoryProduct((;))) # empty NamedTuple is cast to Tuple{}
554-
555-
g0 = gradedrange([s => 2])
556-
@test space_isequal((@inferred fusion_product(g0, g0)), gradedrange([s => 4]))
557-
558-
@test (@inferred s × U1(1)) == CategoryProduct(U1(1))
559-
@test (@inferred s × CategoryProduct(U1(1))) == CategoryProduct(U1(1))
560-
@test (@inferred s × CategoryProduct(; A=U1(1))) == CategoryProduct(; A=U1(1))
561-
@test (@inferred U1(1) × s) == CategoryProduct(U1(1))
562-
@test (@inferred CategoryProduct(U1(1)) × s) == CategoryProduct(U1(1))
563-
@test (@inferred CategoryProduct(; A=U1(1)) × s) == CategoryProduct(; A=U1(1))
564-
565-
# Empty acts as trivial
566-
@test (@inferred_latest U1(1) s) == U1(1)
567-
@test (@inferred SU2(0) s) == gradedrange([SU2(0) => 1])
568-
@test (@inferred Fib("τ") s) == gradedrange([Fib("τ") => 1])
569-
@test (@inferred_latest s U1(1)) == U1(1)
570-
@test (@inferred s SU2(0)) == gradedrange([SU2(0) => 1])
571-
@test (@inferred s Fib("τ")) == gradedrange([Fib("τ") => 1])
572-
573-
@test (@inferred_latest CategoryProduct(U1(1)) s) == CategoryProduct(U1(1))
574-
@test (@inferred_latest CategoryProduct(SU2(0)) s) ==
575-
gradedrange([CategoryProduct(SU2(0)) => 1])
576-
@test (@inferred_latest CategoryProduct(Fib("τ"), SU2(1), U1(2)) s) ==
577-
gradedrange([CategoryProduct(Fib("τ"), SU2(1), U1(2)) => 1])
578-
579-
@test (@inferred_latest CategoryProduct(; A=U1(1)) s) == CategoryProduct(; A=U1(1))
580-
@test (@inferred_latest CategoryProduct(; A=SU2(0)) s) ==
581-
gradedrange([CategoryProduct(; A=SU2(0)) => 1])
582-
@test (@inferred_latest CategoryProduct(; A=Fib("τ"), B=SU2(1), C=U1(2)) s) ==
583-
gradedrange([CategoryProduct(; A=Fib("τ"), B=SU2(1), C=U1(2)) => 1])
584-
585-
# Empty behaves as empty NamedTuple
586-
@test s != U1(0)
587-
@test s != CategoryProduct(U1(0))
588-
@test s != CategoryProduct(; A=U1(1))
589-
@test s == CategoryProduct(; A=U1(0))
590-
@test CategoryProduct(; A=U1(0)) == s
591-
592-
@test !(s < s)
593-
@test_throws ArgumentError s < CategoryProduct(U1(0))
594-
@test s < CategoryProduct(; A=U1(1))
595-
@test s > CategoryProduct(; A=U1(-1))
596-
@test !(s < CategoryProduct(; A=U1(0)))
597-
@test !(s > CategoryProduct(; A=U1(0)))
545+
for s in (CategoryProduct(()), CategoryProduct((;)))
546+
@test s == CategoryProduct(())
547+
@test s == CategoryProduct((;))
548+
@test (@inferred dual(s)) == s
549+
@test (@inferred s × s) == s
550+
@test (@inferred s s) == s
551+
@test (@inferred quantum_dimension(s)) == 1
552+
@test (@inferred_latest trivial(s)) == s
553+
554+
g0 = gradedrange([s => 2])
555+
@test space_isequal((@inferred fusion_product(g0, g0)), gradedrange([s => 4]))
556+
557+
@test (@inferred s × U1(1)) == CategoryProduct(U1(1))
558+
@test (@inferred s × CategoryProduct(U1(1))) == CategoryProduct(U1(1))
559+
@test (@inferred s × CategoryProduct(; A=U1(1))) == CategoryProduct(; A=U1(1))
560+
@test (@inferred U1(1) × s) == CategoryProduct(U1(1))
561+
@test (@inferred CategoryProduct(U1(1)) × s) == CategoryProduct(U1(1))
562+
@test (@inferred CategoryProduct(; A=U1(1)) × s) == CategoryProduct(; A=U1(1))
563+
564+
# Empty acts as trivial
565+
@test (@inferred_latest U1(1) s) == U1(1)
566+
@test (@inferred SU2(0) s) == gradedrange([SU2(0) => 1])
567+
@test (@inferred Fib("τ") s) == gradedrange([Fib("τ") => 1])
568+
@test (@inferred_latest s U1(1)) == U1(1)
569+
@test (@inferred s SU2(0)) == gradedrange([SU2(0) => 1])
570+
@test (@inferred s Fib("τ")) == gradedrange([Fib("τ") => 1])
571+
572+
@test (@inferred_latest CategoryProduct(U1(1)) s) == CategoryProduct(U1(1))
573+
@test (@inferred_latest CategoryProduct(SU2(0)) s) ==
574+
gradedrange([CategoryProduct(SU2(0)) => 1])
575+
@test (@inferred_latest CategoryProduct(Fib("τ"), SU2(1), U1(2)) s) ==
576+
gradedrange([CategoryProduct(Fib("τ"), SU2(1), U1(2)) => 1])
577+
578+
@test (@inferred_latest CategoryProduct(; A=U1(1)) s) == CategoryProduct(; A=U1(1))
579+
@test (@inferred_latest CategoryProduct(; A=SU2(0)) s) ==
580+
gradedrange([CategoryProduct(; A=SU2(0)) => 1])
581+
@test (@inferred_latest CategoryProduct(; A=Fib("τ"), B=SU2(1), C=U1(2)) s) ==
582+
gradedrange([CategoryProduct(; A=Fib("τ"), B=SU2(1), C=U1(2)) => 1])
583+
584+
# Empty behaves as empty NamedTuple
585+
@test s != U1(0)
586+
@test s != CategoryProduct(U1(0))
587+
@test s != CategoryProduct(; A=U1(1))
588+
@test s == CategoryProduct(; A=U1(0))
589+
@test CategoryProduct(; A=U1(0)) == s
590+
591+
@test !(s < s)
592+
@test_throws ArgumentError s < CategoryProduct(U1(0))
593+
@test s < CategoryProduct(; A=U1(1))
594+
@test s > CategoryProduct(; A=U1(-1))
595+
@test !(s < CategoryProduct(; A=U1(0)))
596+
@test !(s > CategoryProduct(; A=U1(0)))
597+
end
598598
end
599599
end

0 commit comments

Comments
 (0)