-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a FloorSemiring instance to NNRat (#13548)
Also some missing `norm_cast` lemmas for inequalities of casts between different types.
- Loading branch information
1 parent
63525bc
commit bfd332a
Showing
5 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/- | ||
Copyright (c) 2024 Eric Wieser. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Eric Wieser | ||
-/ | ||
import Mathlib.Data.Rat.Floor | ||
|
||
/-! | ||
# Floor Function for Non-negative Rational Numbers | ||
## Summary | ||
We define the `FloorSemiring` instance on `ℚ≥0`, and relate its operators to `NNRat.cast`. | ||
Note that we cannot talk about `Int.fract`, which currently only works for rings. | ||
## Tags | ||
nnrat, rationals, ℚ≥0, floor | ||
-/ | ||
|
||
namespace NNRat | ||
|
||
instance : FloorSemiring ℚ≥0 where | ||
floor q := ⌊q.val⌋₊ | ||
ceil q := ⌈q.val⌉₊ | ||
floor_of_neg h := by simpa using h.trans zero_lt_one | ||
gc_floor {a n} h := by rw [← NNRat.coe_le_coe, Nat.le_floor_iff] <;> norm_cast | ||
gc_ceil {a b} := by rw [← NNRat.coe_le_coe, Nat.ceil_le]; norm_cast | ||
|
||
@[simp, norm_cast] | ||
theorem floor_coe (q : ℚ≥0) : ⌊(q : ℚ)⌋₊ = ⌊q⌋₊ := rfl | ||
|
||
@[simp, norm_cast] | ||
theorem ceil_coe (q : ℚ≥0) : ⌈(q : ℚ)⌉₊ = ⌈q⌉₊ := rfl | ||
|
||
@[simp, norm_cast] | ||
theorem coe_floor (q : ℚ≥0) : ↑⌊q⌋₊ = ⌊(q : ℚ)⌋ := Int.ofNat_floor_eq_floor q.coe_nonneg | ||
|
||
@[simp, norm_cast] | ||
theorem coe_ceil (q : ℚ≥0) : ↑⌈q⌉₊ = ⌈(q : ℚ)⌉ := Int.ofNat_ceil_eq_ceil q.coe_nonneg | ||
|
||
protected theorem floor_def (q : ℚ≥0) : ⌊q⌋₊ = q.num / q.den := by | ||
rw [← Int.natCast_inj, NNRat.coe_floor, Rat.floor_def, Int.ofNat_ediv, den_coe, num_coe] | ||
|
||
section Semifield | ||
|
||
variable {K} [LinearOrderedSemifield K] [FloorSemiring K] | ||
|
||
@[simp, norm_cast] | ||
theorem floor_cast (x : ℚ≥0) : ⌊(x : K)⌋₊ = ⌊x⌋₊ := | ||
(Nat.floor_eq_iff x.cast_nonneg).2 (mod_cast (Nat.floor_eq_iff x.cast_nonneg).1 (Eq.refl ⌊x⌋₊)) | ||
|
||
@[simp, norm_cast] | ||
theorem ceil_cast (x : ℚ≥0) : ⌈(x : K)⌉₊ = ⌈x⌉₊ := by | ||
obtain rfl | hx := eq_or_ne x 0 | ||
· simp | ||
· refine (Nat.ceil_eq_iff ?_).2 (mod_cast (Nat.ceil_eq_iff ?_).1 (Eq.refl ⌈x⌉₊)) <;> simpa | ||
|
||
end Semifield | ||
|
||
section Field | ||
|
||
variable {K} [LinearOrderedField K] [FloorRing K] | ||
|
||
@[simp, norm_cast] | ||
theorem intFloor_cast (x : ℚ≥0) : ⌊(x : K)⌋ = ⌊(x : ℚ)⌋ := by | ||
rw [Int.floor_eq_iff (α := K), ← coe_floor] | ||
norm_cast | ||
norm_cast | ||
rw [Nat.cast_add_one, ← Nat.floor_eq_iff (zero_le _)] | ||
|
||
@[simp, norm_cast] | ||
theorem intCeil_cast (x : ℚ≥0) : ⌈(x : K)⌉ = ⌈(x : ℚ)⌉ := by | ||
rw [Int.ceil_eq_iff, ← coe_ceil, sub_lt_iff_lt_add] | ||
constructor | ||
· have := NNRat.cast_strictMono (K := K) <| Nat.ceil_lt_add_one <| zero_le x | ||
rw [NNRat.cast_add, NNRat.cast_one] at this | ||
refine Eq.trans_lt ?_ this | ||
norm_cast | ||
· rw [Int.cast_natCast, NNRat.cast_le_natCast] | ||
exact Nat.le_ceil _ | ||
|
||
end Field | ||
|
||
@[norm_cast] | ||
theorem floor_natCast_div_natCast (n d : ℕ) : ⌊(↑n / ↑d : ℚ≥0)⌋₊ = n / d := | ||
Rat.natFloor_natCast_div_natCast n d | ||
|
||
end NNRat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters