Skip to content

Commit

Permalink
Add docs and tests for infinite graph
Browse files Browse the repository at this point in the history
  • Loading branch information
stecrotti committed Apr 12, 2024
1 parent 6b10975 commit 81d960c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/infinite_regular_factorgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ struct InfiniteRegularFactorGraph{T<:Integer} <: AbstractFactorGraph{T}
kᵢ :: T # variable degree
kₐ :: T # factor degree

function InfiniteRegularFactorGraph(kₐ::T, kᵢ::T) where {T<:Integer}
@doc """
InfiniteRegularFactorGraph(kᵢ, kₐ)
Construct an `InfiniteRegularFactorGraph` with variable degree `kᵢ` and factor degree `kₐ`
"""
function InfiniteRegularFactorGraph(kᵢ::T, kₐ::T) where {T<:Integer}
kᵢ > 0 || throw(ArgumentError("Factor degree must be positive, got $kᵢ"))
kₐ > 0 || throw(ArgumentError("Factor degree must be positive, got $kₐ"))
return new{T}(kᵢ, kₐ)
Expand All @@ -33,5 +38,20 @@ end
function IndexedGraphs.neighbors(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Variable})
return Fill(1, g.kᵢ)
end

edge_indices(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Factor}) = Fill(1, g.kₐ)
edge_indices(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Variable}) = Fill(1, g.kᵢ)
edge_indices(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Variable}) = Fill(1, g.kᵢ)

function IndexedGraphs.inedges(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Factor})
return (IndexedEdge(1,1,1) for _ in 1:g.kₐ)
end
function IndexedGraphs.inedges(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Variable})
return (IndexedEdge(1,1,1) for _ in 1:g.kᵢ)

end
function IndexedGraphs.outedges(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Factor})
return (IndexedEdge(1,1,1) for _ in 1:g.kₐ)
end
function IndexedGraphs.outedges(g::InfiniteRegularFactorGraph, ::FactorGraphVertex{Variable})
return (IndexedEdge(1,1,1) for _ in 1:g.kᵢ)
end
25 changes: 25 additions & 0 deletions test/infinite_regular_factorgraph.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
kᵢ = 3
kₐ = 4
g = InfiniteRegularFactorGraph(kᵢ, kₐ)

@testset "Basics" begin
@test degree(g, variable(100)) == kᵢ
@test degree(g, factor(12)) == kₐ
@test nfactors(g) == 1
@test nvariables(g) == 1
@test ne(g) == 1
@test variables(g) == 1:1
@test factors(g) == 1:1
@test all(edges(g)) do (i, j, id)
i == 1 && j == 1 && id == 1
end
end

@testset "Edge indices" begin
@test all(variables(g)) do i
idx.(collect(inedges(g, variable(i)))) == edge_indices(g, variable(i))
end
@test all(factors(g)) do a
idx.(collect(outedges(g, factor(a)))) == edge_indices(g, factor(a))
end
end
7 changes: 3 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ using Random
Aqua.test_ambiguities(FactorGraphs)
end

@testset "FactorGraphs" begin
include("factorgraph.jl")
include("generators.jl")
end
include("factorgraph.jl")
include("generators.jl")
include("infinite_regular_factorgraph.jl")

nothing

0 comments on commit 81d960c

Please sign in to comment.