Zulip Chat Archive

Stream: Is there code for X?

Topic: Factorisation of squarefree naturals


Yaël Dillies (Sep 25 2023 at 14:22):

I'm having trouble putting the pieces together. Do we have anything related to the prime factorisation of a squarefree natural?

import Mathlib.Algebra.Squarefree
import Mathlib.RingTheory.Int.Basic

open scoped BigOperators

namespace Nat
variable {m n : } {s : Finset }

lemma prod_support_factorization_of_squarefree (hn : Squarefree n) :
     p in (factorization n).support, p = n := by
  sorry

lemma support_factorization_prod (hs :  p  s, p.Prime) :
    (factorization $  p in s, p).support = s := by
  sorry

lemma support_factorization_div_gcd (hm : Squarefree m) (hn : Squarefree n) :
    (factorization $ m / m.gcd n).support =
      (factorization m).support \ (factorization n).support := by
  sorry

end Nat

Yaël Dillies (Sep 25 2023 at 14:23):

This is probably a question for @Eric Rodriguez

Bolton Bailey (Sep 25 2023 at 14:24):

I feel like the first lemma is true for more than just squarefree numbers

Yaël Dillies (Sep 25 2023 at 14:25):

No. (factorization 4).support = {2}.

Bolton Bailey (Sep 25 2023 at 14:27):

Oh is it not Nat.factorization? Sorry.

Bolton Bailey (Sep 25 2023 at 14:27):

docs#Nat.factorization_prod_pow_eq_self would be the lemma for Nat.factorization

Yaël Dillies (Sep 25 2023 at 14:28):

It is. And Nat.factorization 4 is the Finsupp with value 2 at 2 and 0 elsewhere, so its support is {2}.

Bolton Bailey (Sep 25 2023 at 14:29):

Oh I understand now, it's without the exponent, thanks.

Yaël Dillies (Sep 25 2023 at 14:32):

Also we really need primeFactors : Finset ℕ := (Nat.factorization n).support.

Yaël Dillies (Sep 25 2023 at 14:32):

Let me write that quickly.

Arend Mellendijk (Sep 25 2023 at 14:57):

See also https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/toFinsetFactors/near/391003733

Arend Mellendijk (Sep 25 2023 at 15:01):

There's docs#Nat.prod_factors_toFinset_of_squarefree for the first one

Yaël Dillies (Sep 25 2023 at 15:03):

I got the first two. What about the third?

Eric Rodriguez (Sep 25 2023 at 15:09):

in your MWE it's picking up docs#factorization, btw

Yaël Dillies (Sep 25 2023 at 15:09):

Are they not equal, actually?

Eric Rodriguez (Sep 25 2023 at 15:11):

probably? api is disjoint though

Eric Rodriguez (Sep 25 2023 at 15:12):

@loogle factorization, Nat.factorization

loogle (Sep 25 2023 at 15:12):

:shrug: nothing found

Yaël Dillies (Sep 25 2023 at 15:14):

Ugh, time for a refactor, I guess.

Eric Rodriguez (Sep 25 2023 at 15:15):

i mean but then you ruin the fact that Nat.factorization is computable

Eric Rodriguez (Sep 25 2023 at 15:16):

i take that back and instead say computable efficiently

Eric Rodriguez (Sep 25 2023 at 15:16):

I'm not sure if factorization runs in a decent time for e.g. the Gaussian integers

Eric Rodriguez (Sep 25 2023 at 15:16):

noncomputable, never mind

Yaël Dillies (Sep 25 2023 at 15:16):

Yeah, it's a Finsupp

Eric Rodriguez (Sep 25 2023 at 15:17):

still computable, we don't have the lean3 poisoning

Yaël Dillies (Sep 25 2023 at 15:17):

It's not even like you can evaluate it (there's currently no representation for Finsupp). If you want decent pretty-printing results, you should use docs#Nat.factors.

Eric Rodriguez (Sep 25 2023 at 15:18):

image.png

Eric Rodriguez (Sep 25 2023 at 15:18):

@Eric Wieser made this recently iirc

Yaël Dillies (Sep 25 2023 at 15:19):

I was not made aware!

Eric Wieser (Sep 25 2023 at 15:19):

Note that the notation it prints isn't computable!

Eric Wieser (Sep 25 2023 at 15:20):

So you can't actually paste it and evaluate it, but you can paste it for use in a proof

Eric Wieser (Sep 25 2023 at 15:20):

The new notation should be visible in things like docs#Finsupp.single_add

Eric Wieser (Sep 25 2023 at 15:21):

Lol I guess not, weird

Yaël Dillies (Sep 25 2023 at 15:21):

Yeah exactly. I knew you added the notation, but I understood it was input-only.

Eric Rodriguez (Sep 25 2023 at 15:44):

assuming example {m n : ℕ} (h : Squarefree m) : Coprime (m / gcd m n) n, here's a solution

Eric Rodriguez (Sep 25 2023 at 15:44):

lemma mono {α M} [CanonicallyOrderedAddMonoid M] : Monotone (Finsupp.support (α := α) (M := M) ·) := by
  intros x y h t ht
  dsimp at ht 
  rw [Finsupp.le_def] at h
  rw [Finsupp.mem_support_iff, pos_iff_ne_zero] at ht
  exact Finsupp.mem_support_iff.mpr (ht.trans_le (h _)).ne'

lemma support_factorization_div_gcd (hm : Squarefree m) (hn : Squarefree n) :
    (Nat.factorization $ m / m.gcd n).support =
      (Nat.factorization m).support \ (Nat.factorization n).support := by
  classical
  rw [factorization_div <| gcd_dvd_left m n]
  set f := factorization m
  set g := factorization n
  set h := factorization (m.gcd n)
  apply subset_antisymm
  · rw [Finset.subset_sdiff]
    refine mono tsub_le_self, ?_
    simp only
    rw [factorization_div <| gcd_dvd_left m n]
    apply factorization_disjoint_of_coprime
    sorry
  · have : f - g  f - h := by
      refine tsub_le_tsub_left ?h f
      apply (factorization_le_iff_dvd (Nat.gcd_ne_zero_left hm.ne_zero) hn.ne_zero).mpr
      exact gcd_dvd_right m n
    exact le_trans (Finsupp.subset_support_tsub) (mono this)

(adding Mathlib.Data.Nat.Factorization.Basic as an import)

Eric Rodriguez (Sep 25 2023 at 15:45):

I'm kind of surprised the first lemma doesn't exist

Eric Rodriguez (Sep 25 2023 at 15:45):

@loogle Monotone, Finsupp.support

loogle (Sep 25 2023 at 15:45):

:shrug: nothing found

Notification Bot (Sep 25 2023 at 16:42):

A message was moved from this topic to #general > Notation for Finsupp by Eric Wieser.

Junyan Xu (Sep 25 2023 at 16:47):

Can you remove hn : Squarefree n?

Eric Rodriguez (Sep 25 2023 at 16:57):

I guess so, unless my little lemma is false without that. (I really doubt it considering I slim_checked it)

Junyan Xu (Sep 25 2023 at 17:14):

Oh, I see that n ≠ 0 is necessary

Yaël Dillies (Sep 25 2023 at 19:03):

This was my proof. I defined primeFactors n as (factorization n).support.

@[simp] lemma mem_primeFactors : p  n.primeFactors  p.Prime  p  n  n  0 := sorry

lemma primeFactors_div_gcd (hm : Squarefree m) (hn  : n  0) :
    primeFactors (m / m.gcd n) = primeFactors m \ primeFactors n := by
  ext p
  have : m / m.gcd n  0 :=
    (Nat.div_ne_zero $ gcd_ne_zero_right hn).2 $ gcd_le_left _ hm.ne_zero.bot_lt
  simp only [mem_primeFactors, ne_eq, this, not_false_eq_true, and_true, not_and, mem_sdiff,
    hm.ne_zero, hn, dvd_div_iff (gcd_dvd_left _ _)]
  refine λ hp  ⟨⟨hp.1, dvd_of_mul_left_dvd hp.2⟩, λ _ hpn  hp.1.not_unit $ hm _ $
    (mul_dvd_mul_right (dvd_gcd (dvd_of_mul_left_dvd hp.2) hpn) _).trans hp.2⟩, λ hp 
      hp.1.1, Coprime.mul_dvd_of_dvd_of_dvd ?_ (gcd_dvd_left _ _) hp.1.2⟩⟩
  rw [coprime_comm, hp.1.1.coprime_iff_not_dvd]
  exact λ hpn  hp.2 hp.1.1 $ hpn.trans $ gcd_dvd_right _ _

Yaël Dillies (Sep 25 2023 at 20:38):

Here is where I use it (Marica-Schönheim), if you're interested: https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/Discrete.20correlation.20inequalities


Last updated: Dec 20 2023 at 11:08 UTC