Skip to content

Commit c9f8010

Browse files
committed
code suggestions
1 parent 8f6c338 commit c9f8010

3 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/fusiontrees/fusiontrees.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function FusionTree{I}(
9292
uncoupled::NTuple{N}, coupled = unit(I), isdual = ntuple(Returns(false), N)
9393
) where {I <: Sector, N}
9494
FusionStyle(I) isa UniqueFusion ||
95-
error("fusion tree requires inner lines if `FusionStyle(I) <: MultipleFusion`")
95+
throw(ArgumentError("fusion tree requires inner lines if `FusionStyle(I) <: MultipleFusion`"))
9696
return FusionTree{I}(
9797
map(s -> convert(I, s), uncoupled), convert(I, coupled), isdual,
9898
_abelianinner(map(s -> convert(I, s), (uncoupled..., dual(coupled))))

test/setup.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,14 @@ function force_planar(tsrc::TensorMap{<:Any, <:GradedSpace})
7676
return tdst
7777
end
7878

79-
function random_fusion(I::Type{<:Sector}, N::Int) # for fusion tree tests
80-
t = Vector{I}(undef, N)
81-
t[1] = randsector(I)
82-
len = 1
83-
84-
while len < N
85-
r = randsector(I)
86-
f = (t[len], r)
87-
if !isempty(f)
88-
len += 1
89-
t[len] = r
90-
end
79+
function random_fusion(I::Type{<:Sector}, ::Val{N}) where {N} # for fusion tree tests
80+
N == 1 && return (randsector(I),)
81+
tail = random_fusion(I, Val(N-1))
82+
s = randsector(I)
83+
while isempty((s, first(tail)))
84+
s = randsector(I)
9185
end
92-
return tuple(t...)
86+
return (s, tail...)
9387
end
9488

9589
sectorlist = (

test/symmetries/fusiontrees.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using .TestSetup
1313
@timedtestset "Fusion trees for $(TensorKit.type_repr(I))" verbose = true for I in sectorlist
1414
Istr = TensorKit.type_repr(I)
1515
N = 5
16-
out = random_fusion(I, N)
16+
out = random_fusion(I, Val(N))
1717
isdual = ntuple(n -> rand(Bool), N)
1818
in = rand(collect((out...)))
1919
numtrees = length(fusiontrees(out, in, isdual))
@@ -63,31 +63,30 @@ using .TestSetup
6363
@constinferred FusionTree((u, u, u), u)
6464
if isa(UnitStyle(I), SimpleUnit)
6565
@constinferred FusionTree((u, u, u, u))
66+
else
67+
@test_throws ArgumentError FusionTree((u, u, u, u))
6668
end
6769
@test_throws MethodError FusionTree((u, u), u, (false, false, false))
6870
else
69-
errstr = "fusion tree requires inner lines if `FusionStyle(I) <: MultipleFusion`"
70-
@test_throws errstr FusionTree((), u, ())
71-
@test_throws errstr FusionTree((u,), u, (false,))
72-
@test_throws errstr FusionTree((u, u), u, (false, false))
73-
@test_throws errstr FusionTree((u, u, u), u)
74-
if isa(UnitStyle(I), SimpleUnit)
75-
@test_throws errstr FusionTree((u, u, u, u))
76-
end
71+
@test_throws ArgumentError FusionTree((), u, ())
72+
@test_throws ArgumentError FusionTree((u,), u, (false,))
73+
@test_throws ArgumentError FusionTree((u, u), u, (false, false))
74+
@test_throws ArgumentError FusionTree((u, u, u), u)
75+
@test_throws ArgumentError FusionTree((u, u, u, u))
7776
end
7877
end
7978
end
8079
@testset "Fusion tree $Istr: insertat" begin
8180
N = 4
82-
out2 = random_fusion(I, N)
81+
out2 = random_fusion(I, Val(N))
8382
in2 = rand(collect((out2...)))
8483
isdual2 = ntuple(n -> rand(Bool), N)
8584
f2 = rand(collect(fusiontrees(out2, in2, isdual2)))
8685
for i in 1:N
87-
out1 = random_fusion(I, N) # guaranteed good fusion
86+
out1 = random_fusion(I, Val(N)) # guaranteed good fusion
8887
out1 = Base.setindex(out1, in2, i) # can lead to poor fusion
8988
while isempty((out1...))
90-
out1 = random_fusion(I, N)
89+
out1 = random_fusion(I, Val(N))
9190
out1 = Base.setindex(out1, in2, i)
9291
end
9392
in1 = rand(collect((out1...)))
@@ -325,14 +324,14 @@ using .TestSetup
325324

326325
@testset "Fusion tree $Istr: merging" begin
327326
N = 3
328-
out1 = random_fusion(I, N)
329-
out2 = random_fusion(I, N)
327+
out1 = random_fusion(I, Val(N))
328+
out2 = random_fusion(I, Val(N))
330329
in1 = rand(collect((out1...)))
331330
in2 = rand(collect((out2...)))
332331
tp = (in1, in2) # messy solution but it works
333332
while isempty(tp)
334-
out1 = random_fusion(I, N)
335-
out2 = random_fusion(I, N)
333+
out1 = random_fusion(I, Val(N))
334+
out2 = random_fusion(I, Val(N))
336335
in1 = rand(collect((out1...)))
337336
in2 = rand(collect((out2...)))
338337
tp = (in1, in2)
@@ -409,22 +408,22 @@ using .TestSetup
409408
N = 4
410409
end
411410
if isa(UnitStyle(I), SimpleUnit)
412-
out = random_fusion(I, N)
411+
out = random_fusion(I, Val(N))
413412
numtrees = count(n -> true, fusiontrees((out..., map(dual, out)...)))
414413
while !(0 < numtrees < 100)
415-
out = random_fusion(I, N)
414+
out = random_fusion(I, Val(N))
416415
numtrees = count(n -> true, fusiontrees((out..., map(dual, out)...)))
417416
end
418417
incoming = rand(collect((out...)))
419418
f1 = rand(collect(fusiontrees(out, incoming, ntuple(n -> rand(Bool), N))))
420419
f2 = rand(collect(fusiontrees(out[randperm(N)], incoming, ntuple(n -> rand(Bool), N))))
421420
else
422-
out = random_fusion(I, N)
423-
out2 = random_fusion(I, N)
421+
out = random_fusion(I, Val(N))
422+
out2 = random_fusion(I, Val(N))
424423
tp = (out...)
425424
tp2 = (out2...)
426425
while isempty(intersect(tp, tp2)) # guarantee fusion to same coloring
427-
out2 = random_fusion(I, N)
426+
out2 = random_fusion(I, Val(N))
428427
tp2 = (out2...)
429428
end
430429
@test_throws ArgumentError fusiontrees((out..., map(dual, out)...))

0 commit comments

Comments
 (0)