Zulip Chat Archive
Stream: Is there code for X?
Topic: Equivalence classes of a multiset
Jesse Slater (Jan 12 2024 at 21:02):
I am trying to break a multiset into a multiset (or finset) of multisets by an equivalence relation on elements.
def Multiset.classes
(m : Multiset α) (op : α → α → Prop) (equiv : Equivalence op)
: Multiset (Multiset α) := _
Matt Diamond (Jan 13 2024 at 00:49):
This could work, but I wouldn't be surprised if there's a better way:
import Mathlib.Data.Multiset.Powerset
import Mathlib.Data.Finset.Basic
def Multiset.classes {α : Type*} [DecidableEq α]
(m : Multiset α) (op : α → α → Prop) [DecidableRel op] : Finset (Multiset α) :=
(m.powerset.filter (fun a => ∀ x ∈ a, ∀ y ∈ m, y ∈ a ↔ op x y)).toFinset
Matt Diamond (Jan 13 2024 at 01:05):
the proof of Equivalence op
isn't needed for the definition of Multiset.classes
but would be used to prove things about the result
Jesse Slater (Jan 13 2024 at 01:06):
I came up with this:
def Multiset.classes
(m : Multiset α) (eq : α → α → Prop)
[DecidableRel eq] [DecidableEq α] :=
let x := m.powerset
|>.filter (λ p => ∀ a ∈ p, ∀ b ∈ p, eq a b)
x.filter (λ p => ∀ q ∈ x, p ≤ q → p = q)
Jesse Slater (Jan 13 2024 at 01:08):
I have to do a second filter to get rid of subsets, since I want the join of set of classes to be the original set
Antoine Chambert-Loir (Jan 14 2024 at 16:46):
This definition works, but if you had to compute that set, it would be inefficient: if the multiset m
has n
elements, you start by sifting out of its powerset, which has 2 ^ n
elements.
Eric Wieser (Jan 15 2024 at 07:23):
I think it might be better to write the naive algorithm on List
and show that when the relation is symmetric and transitive it is permutation invariant
Eric Wieser (Jan 15 2024 at 07:40):
Though I guess you could also use (m.map (Quot.mk r)).toFinset.map ⟨fun q => m.filter (Quot.mk . = q), sorry⟩
or similar
Last updated: May 02 2025 at 03:31 UTC