Documentation

Mathlib.Data.Multiset.Filter

Filtering multisets by a predicate #

Main definitions #

Multiset.filter #

def Multiset.filter {α : Type u_1} (p : αProp) [DecidablePred p] (s : Multiset α) :

Filter p s returns the elements in s (with the same multiplicities) which satisfy p, and removes the rest.

Equations
Instances For
    @[simp]
    theorem Multiset.filter_coe {α : Type u_1} (p : αProp) [DecidablePred p] (l : List α) :
    filter p l = (List.filter (fun (b : α) => decide (p b)) l)
    @[simp]
    theorem Multiset.filter_zero {α : Type u_1} (p : αProp) [DecidablePred p] :
    filter p 0 = 0
    theorem Multiset.filter_congr {α : Type u_1} {p q : αProp} [DecidablePred p] [DecidablePred q] {s : Multiset α} :
    (∀ xs, p x q x)filter p s = filter q s
    @[simp]
    theorem Multiset.filter_add {α : Type u_1} (p : αProp) [DecidablePred p] (s t : Multiset α) :
    filter p (s + t) = filter p s + filter p t
    @[simp]
    theorem Multiset.filter_le {α : Type u_1} (p : αProp) [DecidablePred p] (s : Multiset α) :
    filter p s s
    @[simp]
    theorem Multiset.filter_subset {α : Type u_1} (p : αProp) [DecidablePred p] (s : Multiset α) :
    filter p s s
    theorem Multiset.filter_le_filter {α : Type u_1} (p : αProp) [DecidablePred p] {s t : Multiset α} (h : s t) :
    filter p s filter p t
    theorem Multiset.monotone_filter_right {α : Type u_1} (s : Multiset α) p q : αProp [DecidablePred p] [DecidablePred q] (h : ∀ (b : α), p bq b) :
    filter p s filter q s
    @[simp]
    theorem Multiset.filter_cons_of_pos {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} (s : Multiset α) :
    p afilter p (a ::ₘ s) = a ::ₘ filter p s
    @[simp]
    theorem Multiset.filter_cons_of_neg {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} (s : Multiset α) :
    ¬p afilter p (a ::ₘ s) = filter p s
    @[simp]
    theorem Multiset.mem_filter {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} :
    a filter p s a s p a
    theorem Multiset.of_mem_filter {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} (h : a filter p s) :
    p a
    theorem Multiset.mem_of_mem_filter {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} (h : a filter p s) :
    a s
    theorem Multiset.mem_filter_of_mem {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} {l : Multiset α} (m : a l) (h : p a) :
    a filter p l
    theorem Multiset.filter_eq_self {α : Type u_1} {p : αProp} [DecidablePred p] {s : Multiset α} :
    filter p s = s as, p a
    theorem Multiset.filter_eq_nil {α : Type u_1} {p : αProp} [DecidablePred p] {s : Multiset α} :
    filter p s = 0 as, ¬p a
    theorem Multiset.le_filter {α : Type u_1} {p : αProp} [DecidablePred p] {s t : Multiset α} :
    s filter p t s t as, p a
    theorem Multiset.filter_cons {α : Type u_1} {p : αProp} [DecidablePred p] {a : α} (s : Multiset α) :
    filter p (a ::ₘ s) = (if p a then {a} else 0) + filter p s
    theorem Multiset.filter_singleton {α : Type u_1} {a : α} (p : αProp) [DecidablePred p] :
    @[simp]
    theorem Multiset.filter_filter {α : Type u_1} (p : αProp) [DecidablePred p] (q : αProp) [DecidablePred q] (s : Multiset α) :
    filter p (filter q s) = filter (fun (a : α) => p a q a) s
    theorem Multiset.filter_comm {α : Type u_1} (p : αProp) [DecidablePred p] (q : αProp) [DecidablePred q] (s : Multiset α) :
    filter p (filter q s) = filter q (filter p s)
    theorem Multiset.filter_add_filter {α : Type u_1} (p : αProp) [DecidablePred p] (q : αProp) [DecidablePred q] (s : Multiset α) :
    filter p s + filter q s = filter (fun (a : α) => p a q a) s + filter (fun (a : α) => p a q a) s
    theorem Multiset.filter_add_not {α : Type u_1} (p : αProp) [DecidablePred p] (s : Multiset α) :
    filter p s + filter (fun (a : α) => ¬p a) s = s
    theorem Multiset.filter_map {α : Type u_1} {β : Type v} (p : αProp) [DecidablePred p] (f : βα) (s : Multiset β) :
    filter p (map f s) = map f (filter (p f) s)
    theorem Multiset.map_filter' {α : Type u_1} {β : Type v} (p : αProp) [DecidablePred p] {f : αβ} (hf : Function.Injective f) (s : Multiset α) [DecidablePred fun (b : β) => ∃ (a : α), p a f a = b] :
    map f (filter p s) = filter (fun (b : β) => ∃ (a : α), p a f a = b) (map f s)
    theorem Multiset.card_filter_le_iff {α : Type u_1} (s : Multiset α) (P : αProp) [DecidablePred P] (n : ) :
    (filter P s).card n s's, n < s'.cardas', ¬P a

    Simultaneously filter and map elements of a multiset #

    def Multiset.filterMap {α : Type u_1} {β : Type v} (f : αOption β) (s : Multiset α) :

    filterMap f s is a combination filter/map operation on s. The function f : α → Option β is applied to each element of s; if f a is some b then b is added to the result, otherwise a is removed from the resulting multiset.

    Equations
    Instances For
      @[simp]
      theorem Multiset.filterMap_coe {α : Type u_1} {β : Type v} (f : αOption β) (l : List α) :
      filterMap f l = (List.filterMap f l)
      @[simp]
      theorem Multiset.filterMap_zero {α : Type u_1} {β : Type v} (f : αOption β) :
      filterMap f 0 = 0
      @[simp]
      theorem Multiset.filterMap_cons_none {α : Type u_1} {β : Type v} {f : αOption β} (a : α) (s : Multiset α) (h : f a = none) :
      @[simp]
      theorem Multiset.filterMap_cons_some {α : Type u_1} {β : Type v} (f : αOption β) (a : α) (s : Multiset α) {b : β} (h : f a = some b) :
      theorem Multiset.filterMap_eq_map {α : Type u_1} {β : Type v} (f : αβ) :
      theorem Multiset.filterMap_filterMap {α : Type u_1} {β : Type v} {γ : Type u_2} (f : αOption β) (g : βOption γ) (s : Multiset α) :
      filterMap g (filterMap f s) = filterMap (fun (x : α) => (f x).bind g) s
      theorem Multiset.map_filterMap {α : Type u_1} {β : Type v} {γ : Type u_2} (f : αOption β) (g : βγ) (s : Multiset α) :
      map g (filterMap f s) = filterMap (fun (x : α) => Option.map g (f x)) s
      theorem Multiset.filterMap_map {α : Type u_1} {β : Type v} {γ : Type u_2} (f : αβ) (g : βOption γ) (s : Multiset α) :
      filterMap g (map f s) = filterMap (g f) s
      theorem Multiset.filter_filterMap {α : Type u_1} {β : Type v} (f : αOption β) (p : βProp) [DecidablePred p] (s : Multiset α) :
      filter p (filterMap f s) = filterMap (fun (x : α) => Option.filter (fun (b : β) => decide (p b)) (f x)) s
      theorem Multiset.filterMap_filter {α : Type u_1} {β : Type v} (p : αProp) [DecidablePred p] (f : αOption β) (s : Multiset α) :
      filterMap f (filter p s) = filterMap (fun (x : α) => if p x then f x else none) s
      @[simp]
      theorem Multiset.filterMap_some {α : Type u_1} (s : Multiset α) :
      @[simp]
      theorem Multiset.mem_filterMap {α : Type u_1} {β : Type v} (f : αOption β) (s : Multiset α) {b : β} :
      b filterMap f s as, f a = some b
      theorem Multiset.map_filterMap_of_inv {α : Type u_1} {β : Type v} (f : αOption β) (g : βα) (H : ∀ (x : α), Option.map g (f x) = some x) (s : Multiset α) :
      map g (filterMap f s) = s
      theorem Multiset.filterMap_le_filterMap {α : Type u_1} {β : Type v} (f : αOption β) {s t : Multiset α} (h : s t) :

      countP #

      theorem Multiset.countP_eq_card_filter {α : Type u_1} (p : αProp) [DecidablePred p] (s : Multiset α) :
      countP p s = (filter p s).card
      @[simp]
      theorem Multiset.countP_filter {α : Type u_1} (p : αProp) [DecidablePred p] (q : αProp) [DecidablePred q] (s : Multiset α) :
      countP p (filter q s) = countP (fun (a : α) => p a q a) s
      theorem Multiset.countP_eq_countP_filter_add {α : Type u_1} (s : Multiset α) (p q : αProp) [DecidablePred p] [DecidablePred q] :
      countP p s = countP p (filter q s) + countP p (filter (fun (a : α) => ¬q a) s)
      theorem Multiset.countP_map {α : Type u_1} {β : Type v} (f : αβ) (s : Multiset α) (p : βProp) [DecidablePred p] :
      countP p (map f s) = (filter (fun (a : α) => p (f a)) s).card
      theorem Multiset.filter_attach {α : Type u_1} (s : Multiset α) (p : αProp) [DecidablePred p] :
      filter (fun (a : { a : α // a s }) => p a) s.attach = map (Subtype.map id ) (filter p s).attach

      Multiplicity of an element #

      @[simp]
      theorem Multiset.count_filter_of_pos {α : Type u_1} [DecidableEq α] {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} (h : p a) :
      count a (filter p s) = count a s
      @[simp]
      theorem Multiset.count_filter_of_neg {α : Type u_1} [DecidableEq α] {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} (h : ¬p a) :
      count a (filter p s) = 0
      theorem Multiset.count_filter {α : Type u_1} [DecidableEq α] {p : αProp} [DecidablePred p] {a : α} {s : Multiset α} :
      count a (filter p s) = if p a then count a s else 0
      theorem Multiset.count_map {α : Type u_3} {β : Type u_4} (f : αβ) (s : Multiset α) [DecidableEq β] (b : β) :
      count b (map f s) = (filter (fun (a : α) => b = f a) s).card
      theorem Multiset.count_map_eq_count {α : Type u_1} {β : Type v} [DecidableEq α] [DecidableEq β] (f : αβ) (s : Multiset α) (hf : Set.InjOn f {x : α | x s}) (x : α) (H : x s) :
      count (f x) (map f s) = count x s

      Multiset.map f preserves count if f is injective on the set of elements contained in the multiset

      theorem Multiset.count_map_eq_count' {α : Type u_1} {β : Type v} [DecidableEq α] [DecidableEq β] (f : αβ) (s : Multiset α) (hf : Function.Injective f) (x : α) :
      count (f x) (map f s) = count x s

      Multiset.map f preserves count if f is injective

      theorem Multiset.filter_eq' {α : Type u_1} [DecidableEq α] (s : Multiset α) (b : α) :
      filter (fun (x : α) => x = b) s = replicate (count b s) b
      theorem Multiset.filter_eq {α : Type u_1} [DecidableEq α] (s : Multiset α) (b : α) :
      filter (Eq b) s = replicate (count b s) b

      Subtraction #

      @[simp]
      theorem Multiset.filter_sub {α : Type u_1} [DecidableEq α] (p : αProp) [DecidablePred p] (s t : Multiset α) :
      filter p (s - t) = filter p s - filter p t
      @[simp]
      theorem Multiset.sub_filter_eq_filter_not {α : Type u_1} [DecidableEq α] (p : αProp) [DecidablePred p] (s : Multiset α) :
      s - filter p s = filter (fun (a : α) => ¬p a) s
      @[simp]
      theorem Multiset.map_le_map_iff {α : Type u_1} {β : Type v} {f : αβ} (hf : Function.Injective f) {s t : Multiset α} :
      map f s map f t s t
      def Multiset.mapEmbedding {α : Type u_1} {β : Type v} (f : α β) :

      Associate to an embedding f from α to β the order embedding that maps a multiset to its image under f.

      Equations
      Instances For
        @[simp]
        theorem Multiset.mapEmbedding_apply {α : Type u_1} {β : Type v} (f : α β) (s : Multiset α) :
        (mapEmbedding f) s = map (⇑f) s
        theorem Multiset.count_eq_card_filter_eq {α : Type u_1} [DecidableEq α] (s : Multiset α) (a : α) :
        count a s = (filter (fun (x : α) => a = x) s).card
        @[simp]
        theorem Multiset.map_count_True_eq_filter_card {α : Type u_1} (s : Multiset α) (p : αProp) [DecidablePred p] :
        count True (map p s) = (filter p s).card

        Mapping a multiset through a predicate and counting the Trues yields the cardinality of the set filtered by the predicate. Note that this uses the notion of a multiset of Props - due to the decidability requirements of count, the decidability instance on the LHS is different from the RHS. In particular, the decidability instance on the left leaks Classical.decEq. See here for more discussion.

        theorem Multiset.filter_attach' {α : Type u_1} (s : Multiset α) (p : { a : α // a s }Prop) [DecidableEq α] [DecidablePred p] :
        filter p s.attach = map (Subtype.map id ) (filter (fun (x : α) => ∃ (h : x s), p x, h) s).attach
        theorem Multiset.Nodup.filter {α : Type u_1} (p : αProp) [DecidablePred p] {s : Multiset α} :
        theorem Multiset.Nodup.erase_eq_filter {α : Type u_1} [DecidableEq α] (a : α) {s : Multiset α} :
        s.Nodups.erase a = Multiset.filter (fun (x : α) => x a) s
        theorem Multiset.Nodup.filterMap {α : Type u_1} {β : Type v} {s : Multiset α} (f : αOption β) (H : ∀ (a a' : α), bf a, b f a'a = a') :
        s.Nodup(filterMap f s).Nodup
        theorem Multiset.Nodup.mem_erase_iff {α : Type u_1} [DecidableEq α] {a b : α} {l : Multiset α} (d : l.Nodup) :
        a l.erase b a b a l
        theorem Multiset.Nodup.not_mem_erase {α : Type u_1} [DecidableEq α] {a : α} {s : Multiset α} (h : s.Nodup) :
        as.erase a