Catlab.jl icon indicating copy to clipboard operation
Catlab.jl copied to clipboard

Exponentials in FinSet

Open slwu89 opened this issue 3 years ago • 2 comments

Given two FinSets A, B is there some computation that creates the set of all functions in Hom(A, B)? For example if A has 5 elements and B has 2, there should be 32 functions in the set f: A->B.

slwu89 avatar Nov 04 '21 18:11 slwu89

This is not a generalized solution but putting together some code, I wrote this up:

using Combinatorics

A = [1, 2, 3, 4, 5]
B = ["x", "y"]

src = combinations([A..., B[1]]) |> collect |> z -> filter(x -> typeof(x) === Vector{Any}, z) |> unique

homset = []
for val in src
	push!(homset, ((val[1:length(val) - 1], val[end]), (setdiff(A, val[1:length(val) - 1]), setdiff(B, [val[end]]))))
end
push!(homset, ((A, "y"), (Int, "x")))

This would give the specified 32 function mapping in a list of tuples of tuples @slwu89 . I have been trying to think about this problem for most of this evening - examined power sets, Cartesian products, posets, etc. - and am struggling to figure out what is the correct mathematical tooling is here that can be generalized to $N$ number of sets. Offhand, I am not sure what to do but would love to experiment some. Any thoughts or direction @epatters and co? I can also bring this up during weekly devs call. Thanks!

TheCedarPrince avatar Jun 06 '22 03:06 TheCedarPrince

Hi I just noticed the good first issue label, and I am interested. I was thinking in something like this:

function exponential_set(A::FinSet, B::FinSet)
  """
  Function that calculate the functions from A to B.
  """
  cartesian_product = collect.(Iterators.product(Iterators.repeated(B, length(A))...))
  FinDomFunction.(reshape(cartesian_product, :), Ref(A), Ref(B))
end

A = FinSet(3)
B = FinSet(["A", "B"])

julia> exponential_set(A, B)
8-element Vector{Catlab.CategoricalAlgebra.FinSets.FinDomFunctionVector{String, Vector{String}, Catlab.CategoricalAlgebra.FinSets.FinSetCollection{Vector{String}, String}}}:
 FinFunction(["A", "A", "A"], 3, 2)
 FinFunction(["B", "A", "A"], 3, 2)
 FinFunction(["A", "B", "A"], 3, 2)
 FinFunction(["B", "B", "A"], 3, 2)
 FinFunction(["A", "A", "B"], 3, 2)
 FinFunction(["B", "A", "B"], 3, 2)
 FinFunction(["A", "B", "B"], 3, 2)
 FinFunction(["B", "B", "B"], 3, 2)

Basically I just compute the product $B\times B\times \cdots \times B$, $|A|$ times and then collect the functions accordingly.

jcvillaquira avatar Apr 07 '23 03:04 jcvillaquira