From ed440c24962cf4a38ad977193ca8fa5a556795da Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Mon, 30 Oct 2023 16:52:36 -0400 Subject: [PATCH] Add `variables` and `image` functions --- src/interface/function.jl | 14 ++++++++++++++ src/library/function/abstract.jl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/interface/function.jl b/src/interface/function.jl index 15ac5e7..a5f6cbc 100644 --- a/src/interface/function.jl +++ b/src/interface/function.jl @@ -53,6 +53,20 @@ function term_head end """ function term_tail end +@doc raw""" + variables(f::AbstractPBF) + +Returns an ordered vector of variables used by ``f \in \mathscr{F}``. +""" +function variables end + +@doc raw""" + image(f::AbstractPBF) + +Returns the image of ``f \in \mathscr{F}`` i. e. the set of values taken by ``f`` as a sorted vector. +""" +function image end + @doc raw""" maxgap(f::AbstractPBF{V,T}) where {V,T} diff --git a/src/library/function/abstract.jl b/src/library/function/abstract.jl index 0469b06..a1931c0 100644 --- a/src/library/function/abstract.jl +++ b/src/library/function/abstract.jl @@ -6,6 +6,36 @@ function Base.one(::F) where {V,T,F<:AbstractPBF{V,T}} return one(F) end +function variables(f::AbstractPBF{V,T}) where {V,T} + x = Set{V}() + + for ω in keys(f) + union!(x, ω) + end + + return sort!(collect(x); lt = varlt) +end + +function image(f::AbstractPBF{V,T}) where {V,T} + v = variables(f) + n = length(v) + u = Vector{Int}(undef, n) + x = Dict{V,Int}(vi => 0 for vi in v) + y = Vector{T}() + + for i = 0:(2^n - 1) + digits!(u, i; base = 2) + + for (vi, ui) in zip(v, u) + x[vi] = ui + end + + push!(y, f(x)) + end + + return unique!(sort!(y; lt = varlt)) +end + function bounds(f::AbstractPBF) return (lowerbound(f), upperbound(f)) end