Skip to content

Commit 4293110

Browse files
committed
Fix edge-case in split_connected_components
1 parent 2b5ceae commit 4293110

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PeriodicGraphs"
22
uuid = "18c5b727-b240-4874-878a-f2e242435bab"
33
authors = ["Lionel Zoubritzky lionel.zoubritzky@gmail.com"]
4-
version = "1.0.0"
4+
version = "1.0.1"
55

66
[deps]
77
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
@@ -11,6 +11,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1111

1212
[compat]
1313
Graphs = "1.3"
14+
LinearAlgebra = "1.10"
1415
PrecompileTools = "1"
1516
StaticArrays = "0.12, 1.0"
1617
julia = "1.6"

src/algorithms/dimensionality.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,22 @@ function _explore_one_component!(expected, encountered, visited, graph::Periodic
352352
end
353353
vec::Vector{SVector{N,Int}} = collect(recordedperiodicities)
354354
catenationmat, d = normal_basis(vec)
355+
detmat = Int(det(catenationmat))
356+
if detmat < 0 && N 2
357+
# Swap the two first columns to make the determinant positive.
358+
# This is not crucial to the algorithm, but it yields simpler offsets in the end.
359+
for i in 1:N
360+
catenationmat[i,1], catenationmat[i,2] = catenationmat[i,2], catenationmat[i,1]
361+
end
362+
end
355363
invmcatenationmat = inv(SMatrix{N,N,Rational{Int},N*N}(catenationmat))
356364
for (j, (src, (dst, ofs))) in enumerate(newedges)
357365
newedges[j] = PeriodicEdge{N}(src, dst, Int.(invmcatenationmat*ofs))
358366
end
359367

360368
idx = minimum(keys(component))
361369
@assert expected[idx] == 0
362-
expected[idx] = Int(det(catenationmat))
370+
expected[idx] = abs(detmat)
363371
encountered[idx] = (PeriodicGraph{N}(length(Q), newedges), [OffsetVertexIterator(nullofs, Q)], catenationmat, d)
364372
idx
365373
end
@@ -431,8 +439,17 @@ function split_connected_components(graph::PeriodicGraph{N}) where N
431439
refmap = first(vmaps).nlist
432440
invmat = inv(SMatrix{N,N,Rational{Int},N*N}(mat))
433441
dumb_indices = findall(i -> begin onei = zeros(Int, N); onei[i] = 1; mat * onei == onei end, 1:N)
434-
for h in 1:1000
435-
x = reverse_hash_position(h, Val{N}())
442+
h = 0
443+
MAXh = 1000
444+
oneaxes = ntuple(i -> SVector{N,Int}(ntuple(j -> Int(j == i), Val(N))), Val(N))
445+
while true
446+
h += 1
447+
x = if h MAXh
448+
reverse_hash_position(h, Val{N}())
449+
else
450+
u, v = fldmod1(h - MAXh, N)
451+
NtoZ(u)*oneaxes[v]
452+
end
436453
all(i -> iszero(x[i]), dumb_indices) || continue
437454
attempt_x = true
438455
for ofsvmap in vmaps
@@ -459,7 +476,7 @@ function split_connected_components(graph::PeriodicGraph{N}) where N
459476
length(vmaps) == ex && break
460477
end
461478
end
462-
length(vmaps) < ex && error("Could not complete the splitting of connected components")
479+
@assert length(vmaps) == ex
463480
end
464481
encountered[keep]
465482
end

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
77
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
88

99
[compat]
10-
Aqua = "0.5"
10+
Aqua = "0.8"
1111
Combinatorics = "1.0"
1212
Graphs = "1.3"
1313
StaticArrays = "0.12, 1.0"

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,20 @@ end
746746
@test length(vmaps) == 2
747747
@test vmaps[1] == PeriodicVertex2D[(1, (0,0)), (3, (-1,0)), (4, (-1,1)), (2, (-1,1))]
748748
@test vmaps[2] == PeriodicVertex2D[(1, (1,-1)), (3, (0,-1)), (4, (0,0)), (2, (0,0))]
749+
750+
u2D = PeriodicGraph("2 1 1 0 2 2 2 0 1 2 2 1 0")
751+
splits = split_connected_components(u2D)
752+
@test length(splits) == 2
753+
subgraph, vmaps, mat, dim = splits[1]
754+
@test dim == 1
755+
@test mat == SA[1 0; 0 2]
756+
@test subgraph == PeriodicGraph("2 1 1 0 1")
757+
@test vmaps == [PeriodicVertex2D[(1, (0,0))], PeriodicVertex2D[(1, (0,-1))]]
758+
subgraph, vmaps, mat, dim = splits[2]
759+
@test dim == 2
760+
@test mat == SA[1 0; 0 1]
761+
@test subgraph == PeriodicGraph("2 1 1 0 1 1 1 1 0")
762+
@test vmaps == [PeriodicVertex2D[(2, (0,0))]]
749763
end
750764

751765
@testset "Dimension change" begin

0 commit comments

Comments
 (0)