Skip to content

Commit

Permalink
Add variables and image functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 30, 2023
1 parent 255e74a commit ed440c2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/interface/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
30 changes: 30 additions & 0 deletions src/library/function/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed440c2

Please sign in to comment.