diff --git a/docs/src/ref.md b/docs/src/ref.md index 899a4d4..91c4562 100644 --- a/docs/src/ref.md +++ b/docs/src/ref.md @@ -99,6 +99,18 @@ ConfigSampler `GenericTensorNetworks` also exports the [`Polynomial`](https://juliamath.github.io/Polynomials.jl/stable/polynomials/polynomial/#Polynomial-2) and [`LaurentPolynomial`](https://juliamath.github.io/Polynomials.jl/stable/polynomials/polynomial/#Polynomials.LaurentPolynomial) types defined in package `Polynomials`. + +For reading the properties from the above element types, one can use the following functions. + +```@docs +read_size +read_config +read_size_count +read_size_config +``` + +The following functions are for saving and loading configurations. + ```@docs StaticBitVector StaticElementVector diff --git a/src/arithematics.jl b/src/arithematics.jl index 9da77ba..087ba35 100644 --- a/src/arithematics.jl +++ b/src/arithematics.jl @@ -933,18 +933,66 @@ post_invert_exponent(t::TropicalNumbers.TropicalTypes) = inv(t) post_invert_exponent(t::ExtendedTropical{K}) where K = ExtendedTropical{K}(map(i->inv(t.orders[i]), K:-1:1)) # Data reading +""" + read_size(x) + +Read the size information from the generic element. The input argument `x` can be +- [`Tropical`](@ref) +- [`ExtendedTropical`](@ref) + +The relevant properties are +- [`SizeMax`](@ref) +- [`SizeMin`](@ref) +""" read_size(x::Tropical) = x.n read_size(x::ExtendedTropical) = x.orders +""" + read_size_count(x) + +Read the size and counting information from the generic element. The input argument `x` can be +- [`CountingTropical`](@ref) +- [`TruncatedPoly`](@ref) +- `Polynomial` defined in Polynomials.jl +- `LaurentPolynomial` defined in Polynomials.jl + +The relevant properties are +- [`CountingMax`](@ref) +- [`CountingMin`](@ref) +- [`GraphPolynomial`](@ref) +""" read_size_count(x::CountingTropical{TV, T}) where {TV, T<:Real} = x.n => x.c read_size_count(x::TruncatedPoly{K, T}) where {K, T<:Real} = [(x.maxorder-K+i => x.coeffs[i]) for i=1:K] read_size_count(x::Polynomial) = [(i-1 => x.coeffs[i]) for i=1:length(x.coeffs)] +read_size_count(x::LaurentPolynomial) = [(i-1+x.order[] => x.coeffs[i]) for i=1:length(x.coeffs)] -read_size_config(x::TruncatedPoly{K, T}) where {K, T<:Union{ConfigEnumerator, ConfigSampler}} = [(x.maxorder-K+i => read_config(x.coeffs[i])) for i=1:K] -read_size_config(x::CountingTropical{TV, T}) where {TV, T<:Union{ConfigEnumerator, ConfigSampler}} = x.n => read_config(x.c) +""" + read_size_config(x) -read_size_singleconfig(x::TruncatedPoly{K, T}) where {K, T<:ConfigSampler} = [(x.maxorder-K+i => x.coeffs[i].data) for i=1:K] +Read the size and configuration information from the generic element. The input argument `x` can be +- [`TruncatedPoly`](@ref), with coefficients being [`ConfigEnumerator`](@ref), [`SumProductTree`](@ref) or [`ConfigSampler`](@ref) +- [`CountingTropical`](@ref), with coefficients being [`ConfigEnumerator`](@ref), [`SumProductTree`](@ref) or [`ConfigSampler`](@ref) +The relevant properties are +- [`ConfigsMax`](@ref) +- [`ConfigsMin`](@ref) +- [`SingleConfigMax`](@ref) +- [`SingleConfigMin`](@ref) +""" +read_size_config(x::TruncatedPoly{K, T}) where {K, T<:Union{ConfigEnumerator, SumProductTree, ConfigSampler}} = [(x.maxorder-K+i => read_config(x.coeffs[i])) for i=1:K] +read_size_config(x::CountingTropical{TV, T}) where {TV, T<:Union{ConfigEnumerator, SumProductTree, ConfigSampler}} = x.n => read_config(x.c) + +""" + read_config(x) + +Read the configuration information from the generic element. The input argument `x` can be +- [`ConfigEnumerator`](@ref) +- [`SumProductTree`](@ref) +- [`ConfigSampler`](@ref) + +The relevant properties are +- [`ConfigsAll`](@ref) +""" read_config(x::ConfigEnumerator) = x.data read_config(x::SumProductTree) = collect(x) read_config(x::ConfigSampler) = x.data \ No newline at end of file diff --git a/test/interfaces.jl b/test/interfaces.jl index 83283e4..5ad39de 100644 --- a/test/interfaces.jl +++ b/test/interfaces.jl @@ -14,6 +14,7 @@ using Graphs, Test, Random res3 = solve(gp, CountingMax(Single))[] res4 = solve(gp, CountingMax(2))[] res5 = solve(gp, GraphPolynomial(; method = :polynomial))[] + res5b = solve(gp, GraphPolynomial(; method = :laurent))[] res6 = solve(gp, SingleConfigMax())[] res7 = solve(gp, ConfigsMax(;bounded=false))[] res8 = solve(gp, ConfigsMax(2; bounded=false))[] @@ -35,6 +36,8 @@ using Graphs, Test, Random @test res4.maxorder == 4 == res4nc[end].first && res4.coeffs[1] == 30 == res4nc[1].second && res4.coeffs[2]==5 == res4nc[2].second @test read_size_count(res5) == [0=>1.0, 1=>10.0, 2=>30.0, 3=>30.0, 4=>5.0] @test res5 == Polynomial([1.0, 10.0, 30, 30, 5]) + @test read_size_count(res5b) == [0=>1.0, 1=>10.0, 2=>30.0, 3=>30.0, 4=>5.0] + @test res5b == LaurentPolynomial([1.0, 10.0, 30, 30, 5]) res6n, res6c = read_size_config(res6) res7n, res7c = read_size_config(res7) @test res6.c.data ∈ res7.c.data @@ -44,10 +47,15 @@ using Graphs, Test, Random res8nc = read_size_config(res8) @test res8nc[1].second == res8.coeffs[1].data @test all(x->sum(x) == 3, res8.coeffs[1].data) && all(x->sum(x) == 4, res8.coeffs[2].data) && length(res8.coeffs[1].data) == 30 && length(res8.coeffs[2].data) == 5 + res9c = read_config(res9) + @test res9c == res9.data @test length(unique(res9.data)) == 76 && all(c->is_independent_set(g, c), res9.data) @test res10 ≈ res5 @test res11 == res5 - @test res12.c.data ∈ res13.c.data + res12n, res12c = read_size_config(res12) + @test res12n == res12.n + @test res12c == res12.c.data + @test res12.c.data ∈ res13.c.data @test res13.c == res7.c @test res14.maxorder == 4 && res14.coeffs[1]==30 && res14.coeffs[2] == 30 && res14.coeffs[3]==5 @test all(x->sum(x) == 2, res15.coeffs[1].data) && all(x->sum(x) == 3, res15.coeffs[2].data) && all(x->sum(x) == 4, res15.coeffs[3].data) && @@ -139,25 +147,30 @@ end @test length(res1) == length(res2) @test Set(res2 |> collect) == Set(res1 |> collect) - res1 = solve(gp, ConfigsMax(; tree_storage=true))[].c + res1 = solve(gp, ConfigsMax(; tree_storage=true))[] res2 = solve(gp, ConfigsMax(; tree_storage=false))[].c - @test res1 isa SumProductTree && res2 isa ConfigEnumerator - @test length(res1) == length(res2) - @test Set(res2 |> collect) == Set(res1 |> collect) + res1n, res1c = read_size_config(res1) + @test res1c == collect(res1.c) + @test res1.c isa SumProductTree && res2 isa ConfigEnumerator + @test length(res1.c) == length(res2) + @test Set(res2 |> collect) == Set(res1.c |> collect) - res1s = solve(gp, ConfigsMax(2; tree_storage=true))[].coeffs + res1s = solve(gp, ConfigsMax(2; tree_storage=true))[] res2s = solve(gp, ConfigsMax(2; tree_storage=false))[].coeffs - for (res1, res2) in zip(res1s, res2s) + res1ncs = read_size_config(res1s) + @test length(res1ncs) == 2 + @test res1ncs[1].second == collect(res1s.coeffs[1]) + for (res1, res2) in zip(res1s.coeffs, res2s) @test res1 isa SumProductTree && res2 isa ConfigEnumerator @test length(res1) == length(res2) @test Set(res2 |> collect) == Set(res1 |> collect) end - res1 = solve(gp, ConfigsMin(; tree_storage=true))[].c + res1 = solve(gp, ConfigsMin(; tree_storage=true))[] res2 = solve(gp, ConfigsMin(; tree_storage=false))[].c - @test res1 isa SumProductTree && res2 isa ConfigEnumerator - @test length(res1) == length(res2) - @test Set(res2 |> collect) == Set(res1 |> collect) + @test res1.c isa SumProductTree && res2 isa ConfigEnumerator + @test length(res1.c) == length(res2) + @test Set(res2 |> collect) == Set(res1.c |> collect) res1s = solve(gp, ConfigsMin(2; tree_storage=true))[].coeffs res2s = solve(gp, ConfigsMin(2; tree_storage=false))[].coeffs