l3lab/ntp-mathlib huggingface dataset:
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => 

Proof state:
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
| ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2 * ↑(Finset.card t) +
    ∑ x in t, ((f x * ↑(Finset.card t) - ∑ i in t, f i) / ↑(Finset.card t)) ^ 2

Next tactic:
enter [2, 2, x]; rw [div_pow]
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => 

Proof state:
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
| ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2 * ↑(Finset.card t) +
    ∑ x in t, ((f x * ↑(Finset.card t) - ∑ i in t, f i) / ↑(Finset.card t)) ^ 2

Next tactic:
enter [2, 2, x]
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => enter [2, 2, x]; 

Proof state:
case h
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x✝ : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x✝
hs : x✝ ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x✝ ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x✝ ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x✝ ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
x : ι
| ((f x * ↑(Finset.card t) - ∑ i in t, f i) / ↑(Finset.card t)) ^ 2

Next tactic:
rw [div_pow]
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => enter [2, 2, x]; rw [div_pow]
  

Proof state:
case inr
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
⊢ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2 * ↑(Finset.card t) +
      ∑ x in t, (f x * ↑(Finset.card t) - ∑ i in t, f i) ^ 2 / ↑(Finset.card t) ^ 2 ≤
    ∑ i in t, f i ^ 2

Next tactic:
rw [div_pow, ← sum_div, ← mul_div_right_comm _ (t.card : 𝕜), ← add_div,
    div_le_iff (sq_pos_of_ne_zero _ htcard.ne')]
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => enter [2, 2, x]; rw [div_pow]
  rw [div_pow, ← sum_div, ← mul_div_right_comm _ (t.card : 𝕜), ← add_div,
    div_le_iff (sq_pos_of_ne_zero _ htcard.ne')]
  

Proof state:
case inr
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
⊢ (∑ i in t, f i) ^ 2 * ↑(Finset.card t) + ∑ x in t, (f x * ↑(Finset.card t) - ∑ i in t, f i) ^ 2 ≤
    (∑ i in t, f i ^ 2) * ↑(Finset.card t) ^ 2

Next tactic:
simp_rw [sub_sq, sum_add_distrib, sum_const, nsmul_eq_mul, sum_sub_distrib, mul_pow, ← sum_mul,
    ← mul_sum, ← sum_mul]
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => enter [2, 2, x]; rw [div_pow]
  rw [div_pow, ← sum_div, ← mul_div_right_comm _ (t.card : 𝕜), ← add_div,
    div_le_iff (sq_pos_of_ne_zero _ htcard.ne')]
  simp_rw [sub_sq, sum_add_distrib, sum_const, nsmul_eq_mul, sum_sub_distrib, mul_pow, ← sum_mul,
    ← mul_sum, ← sum_mul]
  

Proof state:
case inr
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
⊢ (∑ i in t, f i) ^ 2 * ↑(Finset.card t) +
      ((∑ x in t, f x ^ 2) * ↑(Finset.card t) ^ 2 - 2 * ((∑ i in t, f i) * ↑(Finset.card t)) * ∑ i in t, f i +
        ↑(Finset.card t) * (∑ i in t, f i) ^ 2) ≤
    (∑ i in t, f i ^ 2) * ↑(Finset.card t) ^ 2

Next tactic:
ring_nf
theorem add_div_le_sum_sq_div_card (hst : s ⊆ t) (f : ι → 𝕜) (d : 𝕜) (hx : 0 ≤ x)
    (hs : x ≤ |(∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card|)
    (ht : d ≤ ((∑ i in t, f i) / t.card) ^ 2) :
    d + s.card / t.card * x ^ 2 ≤ (∑ i in t, f i ^ 2) / t.card := by
  obtain hscard | hscard := (s.card.cast_nonneg : (0 : 𝕜) ≤ s.card).eq_or_lt
  · simpa [← hscard] using ht.trans sum_div_card_sq_le_sum_sq_div_card
  have htcard : (0 : 𝕜) < t.card := hscard.trans_le (Nat.cast_le.2 (card_le_of_subset hst))
  have h₁ : x ^ 2 ≤ ((∑ i in s, f i) / s.card - (∑ i in t, f i) / t.card) ^ 2 :=
    sq_le_sq.2 (by rwa [abs_of_nonneg hx])
  have h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / t.card)) / s.card) ^ 2 := by
    apply h₁.trans
    rw [sum_sub_distrib, sum_const, nsmul_eq_mul, sub_div, mul_div_cancel_left _ hscard.ne']
  apply (add_le_add_right ht _).trans
  rw [← mul_div_right_comm, le_div_iff htcard, add_mul, div_mul_cancel _ htcard.ne']
  have h₃ := mul_sq_le_sum_sq hst (fun i => (f i - (∑ j in t, f j) / t.card)) h₂ hscard.ne'
  apply (add_le_add_left h₃ _).trans
  -- Porting note: was
  -- `simp [← mul_div_right_comm _ (t.card : 𝕜), sub_div' _ _ _ htcard.ne', ← sum_div, ← add_div,`
  -- `  mul_pow, div_le_iff (sq_pos_of_ne_zero _ htcard.ne'), sub_sq, sum_add_distrib, ← sum_mul,`
  -- `  ← mul_sum]`
  simp_rw [sub_div' _ _ _ htcard.ne']
  conv_lhs => enter [2, 2, x]; rw [div_pow]
  rw [div_pow, ← sum_div, ← mul_div_right_comm _ (t.card : 𝕜), ← add_div,
    div_le_iff (sq_pos_of_ne_zero _ htcard.ne')]
  simp_rw [sub_sq, sum_add_distrib, sum_const, nsmul_eq_mul, sum_sub_distrib, mul_pow, ← sum_mul,
    ← mul_sum, ← sum_mul]
  ring_nf; 

Proof state:
case inr
α : Type u_1
inst✝³ : DecidableEq α
inst✝² : Fintype α
P : Finpartition univ
u : Finset α
ε : ℝ
l : ℕ
ι : Type u_2
𝕜 : Type u_3
inst✝¹ : LinearOrderedField 𝕜
r : ι → ι → Prop
inst✝ : DecidableRel r
s t : Finset ι
x : 𝕜
hst : s ⊆ t
f : ι → 𝕜
d : 𝕜
hx : 0 ≤ x
hs : x ≤ |(∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)|
ht : d ≤ ((∑ i in t, f i) / ↑(Finset.card t)) ^ 2
hscard : 0 < ↑(Finset.card s)
htcard : 0 < ↑(Finset.card t)
h₁ : x ^ 2 ≤ ((∑ i in s, f i) / ↑(Finset.card s) - (∑ i in t, f i) / ↑(Finset.card t)) ^ 2
h₂ : x ^ 2 ≤ ((∑ i in s, (f i - (∑ j in t, f j) / ↑(Finset.card t))) / ↑(Finset.card s)) ^ 2
h₃ : ↑(Finset.card s) * x ^ 2 ≤ ∑ i in t, (fun i => f i - (∑ j in t, f j) / ↑(Finset.card t)) i ^ 2
⊢ ↑(Finset.card t) ^ 2 * ∑ x in t, f x ^ 2 ≤ ↑(Finset.card t) ^ 2 * ∑ x in t, f x ^ 2

Next tactic:
rfl
example (ε : ℝ) (l : ℕ) : 0 < SzemerediRegularity.initialBound ε l := by 

Proof state:
α : Type u_1
inst✝¹ : DecidableEq α
inst✝ : Fintype α
P : Finpartition univ
u : Finset α
ε✝ ε : ℝ
l : ℕ
⊢ 0 < initialBound ε l

Next tactic:
positivity
example (ε : ℝ) (l : ℕ) : 0 < SzemerediRegularity.bound ε l := by 

Proof state:
α : Type u_1
inst✝¹ : DecidableEq α
inst✝ : Fintype α
P : Finpartition univ
u : Finset α
ε✝ ε : ℝ
l : ℕ
⊢ 0 < bound ε l

Next tactic:
positivity
private theorem adE_of_eq_eq_zero (i : B) (h : A i i = 2) : adE R A ⟨i, i⟩ = 0 := by
  

Proof state:
R : Type u
B : Type v
inst✝² : CommRing R
inst✝¹ : DecidableEq B
inst✝ : Fintype B
A : Matrix B B ℤ
i : B
h : A i i = 2
⊢ adE R A (i, i) = 0

Next tactic:
have h' : (-2 : ℤ).toNat = 0 := rfl
private theorem adE_of_eq_eq_zero (i : B) (h : A i i = 2) : adE R A ⟨i, i⟩ = 0 := by
  have h' : (-2 : ℤ).toNat = 0 := rfl
  

Proof state:
R : Type u
B : Type v
inst✝² : CommRing R
inst✝¹ : DecidableEq B
inst✝ : Fintype B
A : Matrix B B ℤ
i : B
h : A i i = 2
h' : Int.toNat (-2) = 0
⊢ adE R A (i, i) = 0

Next tactic:
simp [adE, h, h']
private theorem adF_of_eq_eq_zero (i : B) (h : A i i = 2) : adF R A ⟨i, i⟩ = 0 := by
  

Proof state:
R : Type u
B : Type v
inst✝² : CommRing R
inst✝¹ : DecidableEq B
inst✝ : Fintype B
A : Matrix B B ℤ
i : B
h : A i i = 2
⊢ adF R A (i, i) = 0

Next tactic:
have h' : (-2 : ℤ).toNat = 0 := rfl
private theorem adF_of_eq_eq_zero (i : B) (h : A i i = 2) : adF R A ⟨i, i⟩ = 0 := by
  have h' : (-2 : ℤ).toNat = 0 := rfl
  

Proof state:
R : Type u
B : Type v
inst✝² : CommRing R
inst✝¹ : DecidableEq B
inst✝ : Fintype B
A : Matrix B B ℤ
i : B
h : A i i = 2
h' : Int.toNat (-2) = 0
⊢ adF R A (i, i) = 0

Next tactic:
simp [adF, h, h']
theorem choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ (n ^ r : α) / r ! := by
  

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ ↑(choose n r) ≤ ↑n ^ r / ↑r !

Next tactic:
rw [le_div_iff']
theorem choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ (n ^ r : α) / r ! := by
  rw [le_div_iff']
  · 

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ ↑r ! * ↑(choose n r) ≤ ↑n ^ r

Next tactic:
norm_cast
theorem choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ (n ^ r : α) / r ! := by
  rw [le_div_iff']
  · norm_cast
    

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ r ! * choose n r ≤ n ^ r

Next tactic:
rw [← Nat.descFactorial_eq_factorial_mul_choose]
theorem choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ (n ^ r : α) / r ! := by
  rw [le_div_iff']
  · norm_cast
    rw [← Nat.descFactorial_eq_factorial_mul_choose]
    

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ descFactorial n r ≤ n ^ r

Next tactic:
exact n.descFactorial_le_pow r
theorem choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ (n ^ r : α) / r ! := by
  rw [le_div_iff']
  · norm_cast
    rw [← Nat.descFactorial_eq_factorial_mul_choose]
    exact n.descFactorial_le_pow r
  

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ 0 < ↑r !

Next tactic:
exact mod_cast r.factorial_pos
theorem pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ) ^ r : α) / r ! ≤ n.choose r := by
  

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ ↑(n + 1 - r) ^ r / ↑r ! ≤ ↑(choose n r)

Next tactic:
rw [div_le_iff']
theorem pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ) ^ r : α) / r ! ≤ n.choose r := by
  rw [div_le_iff']
  · 

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ ↑(n + 1 - r) ^ r ≤ ↑r ! * ↑(choose n r)

Next tactic:
norm_cast
theorem pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ) ^ r : α) / r ! ≤ n.choose r := by
  rw [div_le_iff']
  · norm_cast
    

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ (n + 1 - r) ^ r ≤ r ! * choose n r

Next tactic:
rw [← Nat.descFactorial_eq_factorial_mul_choose]
theorem pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ) ^ r : α) / r ! ≤ n.choose r := by
  rw [div_le_iff']
  · norm_cast
    rw [← Nat.descFactorial_eq_factorial_mul_choose]
    

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ (n + 1 - r) ^ r ≤ descFactorial n r

Next tactic:
exact n.pow_sub_le_descFactorial r
theorem pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ) ^ r : α) / r ! ≤ n.choose r := by
  rw [div_le_iff']
  · norm_cast
    rw [← Nat.descFactorial_eq_factorial_mul_choose]
    exact n.pow_sub_le_descFactorial r
  

Proof state:
α : Type u_1
inst✝ : LinearOrderedSemifield α
r n : ℕ
⊢ 0 < ↑r !

Next tactic:
exact mod_cast r.factorial_pos
@[simp]
theorem J_transpose : (J l R)ᵀ = -J l R := by
  

Proof state:
l : Type u_1
R : Type u_2
inst✝¹ : DecidableEq l
inst✝ : CommRing R
⊢ (J l R)ᵀ = -J l R

Next tactic:
rw [J, fromBlocks_transpose, ← neg_one_smul R (fromBlocks _ _ _ _ : Matrix (l ⊕ l) (l ⊕ l) R),
    fromBlocks_smul, Matrix.transpose_zero, Matrix.transpose_one, transpose_neg]
@[simp]
theorem J_transpose : (J l R)ᵀ = -J l R := by
  rw [J, fromBlocks_transpose, ← neg_one_smul R (fromBlocks _ _ _ _ : Matrix (l ⊕ l) (l ⊕ l) R),
    fromBlocks_smul, Matrix.transpose_zero, Matrix.transpose_one, transpose_neg]
  

Proof state:
l : Type u_1
R : Type u_2
inst✝¹ : DecidableEq l
inst✝ : CommRing R
⊢ fromBlocks 0 1 (-1ᵀ) 0 = fromBlocks (-1 • 0) (-1 • -1) (-1 • 1) (-1 • 0)

Next tactic:
simp [fromBlocks]
theorem J_squared : J l R * J l R = -1 := by
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ J l R * J l R = -1

Next tactic:
rw [J, fromBlocks_multiply]
theorem J_squared : J l R * J l R = -1 := by
  rw [J, fromBlocks_multiply]
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ fromBlocks (0 * 0 + -1 * 1) (0 * -1 + -1 * 0) (1 * 0 + 0 * 1) (1 * -1 + 0 * 0) = -1

Next tactic:
simp only [Matrix.zero_mul, Matrix.neg_mul, zero_add, neg_zero, Matrix.one_mul, add_zero]
theorem J_squared : J l R * J l R = -1 := by
  rw [J, fromBlocks_multiply]
  simp only [Matrix.zero_mul, Matrix.neg_mul, zero_add, neg_zero, Matrix.one_mul, add_zero]
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ fromBlocks (-1) 0 0 (-1) = -1

Next tactic:
rw [← neg_zero, ← Matrix.fromBlocks_neg, ← fromBlocks_one]
theorem J_inv : (J l R)⁻¹ = -J l R := by
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ (J l R)⁻¹ = -J l R

Next tactic:
refine' Matrix.inv_eq_right_inv _
theorem J_inv : (J l R)⁻¹ = -J l R := by
  refine' Matrix.inv_eq_right_inv _
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ J l R * -J l R = 1

Next tactic:
rw [Matrix.mul_neg, J_squared]
theorem J_inv : (J l R)⁻¹ = -J l R := by
  refine' Matrix.inv_eq_right_inv _
  rw [Matrix.mul_neg, J_squared]
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ - -1 = 1

Next tactic:
exact neg_neg 1
theorem J_det_mul_J_det : det (J l R) * det (J l R) = 1 := by
  

Proof state:
l : Type u_1
R : Type u_2
inst✝² : DecidableEq l
inst✝¹ : CommRing R
inst✝ : Fintype l
⊢ det (J l R) * det (J l R) = 1

Next tactic:
rw [← det_mul, J_squared, ← one_smul R (-1 : Matrix _ _ R), smul_neg, ← neg_smul, det_smul,
    Fintype.card_sum, det_one, mul_one]