|
|
|
|
Quelle Defs.lean
Sprache: Lean
|
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kevin Kappelmann
-/
module
public import Mathlib.Algebra.Order.Ring.Cast
import Mathlib.Order.Int.LeastGreatest
/-!
# Floor and ceil
We define the natural- and integer-valued floor and ceil functions on linearly ordered rings.
We also provide `positivity` extensions to handle floor and ceil.
## Main definitions
* `FloorSemiring`: An ordered semiring with natural-valued floor and ceil.
* `Nat.floor a`: Greatest natural `n` such that `n ≤ a`. Equal to `0` if `a < 0`.
* `Nat.ceil a`: Least natural `n` such that `a ≤ n`.
* `FloorRing`: A linearly ordered ring with integer-valued floor and ceil.
* `Int.floor a`: Greatest integer `z` such that `z ≤ a`.
* `Int.ceil a`: Least integer `z` such that `a ≤ z`.
* `Int.fract a`: Fractional part of `a`, defined as `a - floor a`.
## Notation
* `⌊a⌋₊` is `Nat.floor a`.
* `⌈a⌉₊` is `Nat.ceil a`.
* `⌊a⌋` is `Int.floor a`.
* `⌈a⌉` is `Int.ceil a`.
The index `₊` in the notations for `Nat.floor` and `Nat.ceil` is used in analogy to the notation
for `nnNorm`.
## TODO
`LinearOrder` can be relaxed to `PartialOrder` in many lemmas.
## Tags
rounding, floor, ceil
-/
@[expose] public section
assert_not_exists Finset
variable {α β : Type*}
/-! ### Floor semiring -/
/-- A `FloorSemiring` is an ordered semiring over `α` with a function
`floor : α → ℕ` satisfying `∀ (n : ℕ) (x : α), n ≤ ⌊x⌋ ↔ (n : α) ≤ x`.
Note that many lemmas require a `LinearOrder`. Please see the above `TODO`. -/
class FloorSemiring (α) [Semiring α] [PartialOrder α] where
/-- `FloorSemiring.floor a` computes the greatest natural `n` such that `(n : α) ≤ a`. -/
floor : α → ℕ
/-- `FloorSemiring.ceil a` computes the least natural `n` such that `a ≤ (n : α)`. -/
ceil : α → ℕ
/-- `FloorSemiring.floor` of a negative element is zero. -/
floor_of_neg {a : α} (ha : a < 0) : floor a = 0
/-- A natural number `n` is smaller than `FloorSemiring.floor a` iff its coercion to `α` is
smaller than `a`. -/
gc_floor {a : α} {n : ℕ} (ha : 0 ≤ a) : n ≤ floor a ↔ (n : α) ≤ a
/-- `FloorSemiring.ceil` is the lower adjoint of the coercion `↑ : ℕ → α`. -/
gc_ceil : GaloisConnection ceil (↑)
instance : FloorSemiring ℕ where
floor := id
ceil := id
floor_of_neg ha := (Nat.not_lt_zero _ ha).elim
gc_floor _ := by
rw [Nat.cast_id, id_def]
gc_ceil n a := by
rw [Nat.cast_id, id_def]
namespace FloorSemiring
variable [Semiring α] [PartialOrder α] [FloorSemiring α]
/-- A version of `Nat.mono_cast` for floor semirings, whose addition is not necessarily monotone. -/
theorem natCast_mono : Monotone (Nat.cast : ℕ → α) :=
fun _ _ h ↦ (gc_ceil _ _).mp <| h.trans' <| (gc_ceil _ _).mpr (le_refl _)
/-- A version of `Nat.cast_nonneg'` for floor semirings, whose addition is not necessarily
monotone. -/
theorem natCast_nonneg (n : ℕ) : 0 ≤ (n : α) := by
simpa using natCast_mono n.zero_le
/-- A version of `Nat.strictMono_cast` for floor semirings, whose addition is not necessarily
monotone. -/
theorem natCast_strictMono : StrictMono (Nat.cast : ℕ → α) := by
refine strictMono_nat_of_lt_succ fun n => (natCast_mono (Nat.le_succ n)).lt_of_ne fun hn => ?_
replace hn (k : ℕ) : ((n + k : ℕ) : α) = n := by
induction k with | zero => rfl | succ k k_ih => grind
have h : n + (floor (n : α) + 1) ≤ floor (n : α) := (gc_floor (natCast_nonneg n)).mpr (hn _).le
grind
theorem natCast_pos {n : ℕ} : 0 < (n : α) ↔ 0 < n := by
rw [← Nat.cast_zero, natCast_strictMono.lt_iff_lt]
theorem natCast_lt_iff {m n : ℕ} : (m : α) < (n : α) ↔ m < n :=
natCast_strictMono.lt_iff_lt
theorem natCast_le_iff {m n : ℕ} : (m : α) ≤ (n : α) ↔ m ≤ n :=
natCast_strictMono.le_iff_le
instance : ZeroLEOneClass α := ⟨by simpa only [Nat.cast_one] using natCast_nonneg 1⟩
instance : CharZero α := ⟨natCast_strictMono.injective⟩
end FloorSemiring
namespace Nat
section OrderedSemiring
variable [Semiring α] [PartialOrder α] [FloorSemiring α] {a : α} {n : ℕ}
/-- `⌊a⌋₊` is the greatest natural `n` such that `n ≤ a`. If `a` is negative, then `⌊a⌋₊ = 0`. -/
def floor : α → ℕ :=
FloorSemiring.floor
/-- `⌈a⌉₊` is the least natural `n` such that `a ≤ n` -/
def ceil : α → ℕ :=
FloorSemiring.ceil
@[simp]
theorem floor_nat : (Nat.floor : ℕ → ℕ) = id :=
rfl
@[simp]
theorem ceil_nat : (Nat.ceil : ℕ → ℕ) = id :=
rfl
@[inherit_doc]
notation "⌊" a "⌋₊" => Nat.floor a
@[inherit_doc]
notation "⌈" a "⌉₊" => Nat.ceil a
theorem le_floor_iff (ha : 0 ≤ a) : n ≤ ⌊a⌋₊ ↔ (n : α) ≤ a :=
FloorSemiring.gc_floor ha
theorem le_floor (h : (n : α) ≤ a) : n ≤ ⌊a⌋₊ :=
(le_floor_iff ((FloorSemiring.natCast_nonneg n).trans h)).2 h
theorem gc_ceil_coe : GaloisConnection (ceil : α → ℕ) (↑) :=
FloorSemiring.gc_ceil
@[simp]
theorem ceil_le : ⌈a⌉₊ ≤ n ↔ a ≤ n :=
gc_ceil_coe _ _
end OrderedSemiring
section LinearOrderedSemiring
variable [Semiring α] [LinearOrder α] [FloorSemiring α] {a : α} {n : ℕ}
theorem lt_ceil : n < ⌈a⌉₊ ↔ (n : α) < a :=
lt_iff_lt_of_le_iff_le ceil_le
@[simp]
theorem ceil_pos : 0 < ⌈a⌉₊ ↔ 0 < a := by rw [lt_ceil, cast_zero]
end LinearOrderedSemiring
end Nat
/-! ### Floor rings -/
/-- A `FloorRing` is a linear ordered ring over `α` with a function
`floor : α → ℤ` satisfying `∀ (z : ℤ) (a : α), z ≤ floor a ↔ (z : α) ≤ a`.
-/
class FloorRing (α) [Ring α] [LinearOrder α] where
/-- `FloorRing.floor a` computes the greatest integer `z` such that `(z : α) ≤ a`. -/
floor : α → ℤ
/-- `FloorRing.ceil a` computes the least integer `z` such that `a ≤ (z : α)`. -/
ceil : α → ℤ
/-- `FloorRing.ceil` is the upper adjoint of the coercion `↑ : ℤ → α`. -/
gc_coe_floor : GaloisConnection (↑) floor
/-- `FloorRing.ceil` is the lower adjoint of the coercion `↑ : ℤ → α`. -/
gc_ceil_coe : GaloisConnection ceil (↑)
instance : FloorRing ℤ where
floor := id
ceil := id
gc_coe_floor a b := by
rw [Int.cast_id, id_def]
gc_ceil_coe a b := by
rw [Int.cast_id, id_def]
/-- A `FloorRing` constructor from the `floor` function alone. -/
@[instance_reducible]
def FloorRing.ofFloor (α) [Ring α] [LinearOrder α] [IsOrderedAddMonoid α] (floor : α → ℤ)
(gc_coe_floor : GaloisConnection (↑) floor) : FloorRing α :=
{ floor
ceil := fun a => -floor (-a)
gc_coe_floor
gc_ceil_coe := fun a z => by rw [neg_le, ← gc_coe_floor, Int.cast_neg, neg_le_neg_iff] }
/-- A `FloorRing` constructor from the `ceil` function alone. -/
@[instance_reducible]
def FloorRing.ofCeil (α) [Ring α] [LinearOrder α] [IsOrderedAddMonoid α] (ceil : α → ℤ)
(gc_ceil_coe : GaloisConnection ceil (↑)) : FloorRing α :=
{ floor := fun a => -ceil (-a)
ceil
gc_coe_floor := fun a z => by rw [le_neg, gc_ceil_coe, Int.cast_neg, neg_le_neg_iff]
gc_ceil_coe }
open scoped Classical in
private noncomputable def floorAux
{α} [Ring α] [PartialOrder α] [IsOrderedRing α] [Nontrivial α] {x : α}
(below : ∃ n : ℤ, n ≤ x) (above : ∃ n : ℤ, x ≤ n) :
{n : ℤ // n ≤ x ∧ ∀ m : ℤ, m ≤ x → m ≤ n} := by
let n := Classical.indefiniteDescription _ above
refine Int.greatestOfBdd (P := (· ≤ x)) n.1 (fun m hm ↦ ?_) below
rw [← Int.cast_le (R := α)]
exact hm.trans n.2
/-- See `exists_floor` for a variant which instead assumes an `Archimedean` ring. -/
theorem exists_floor'
{α} [Ring α] [PartialOrder α] [IsOrderedRing α] [Nontrivial α] (x : α)
(below : ∃ n : ℤ, n ≤ x) (above : ∃ n : ℤ, x ≤ n) :
∃ fl : ℤ, ∀ z : ℤ, z ≤ fl ↔ (z : α) ≤ x := by
refine ⟨_, fun n ↦ ⟨?_, (floorAux below above).2.2 _⟩⟩
rw [← Int.cast_le (R := α)]
exact le_trans' (floorAux below above).2.1
/-- Construct a `FloorRing` instance noncomputably, from the hypothesis that every element is
bounded above by a natural number. -/
@[no_expose, instance_reducible]
noncomputable def FloorRing.ofBounded
(α) [Ring α] [LinearOrder α] [IsOrderedRing α] [Nontrivial α]
(bounded : ∀ x : α, ∃ n : ℕ, x ≤ n) : FloorRing α :=
have below (x : α) : ∃ n : ℤ, n ≤ x := by
obtain ⟨n, hn⟩ := bounded (-x)
use -n
simpa [neg_le]
have above (x : α) : ∃ n : ℤ, x ≤ n := by
obtain ⟨n, hn⟩ := bounded x
use n
exact mod_cast hn
.ofFloor _ _ fun n x ↦ (Classical.choose_spec (exists_floor' x (below x) (above x)) n).symm
namespace Int
variable [Ring α] [LinearOrder α] [FloorRing α] {z : ℤ} {a : α}
/-- `Int.floor a` is the greatest integer `z` such that `z ≤ a`. It is denoted with `⌊a⌋`. -/
def floor : α → ℤ :=
FloorRing.floor
/-- `Int.ceil a` is the smallest integer `z` such that `a ≤ z`. It is denoted with `⌈a⌉`. -/
def ceil : α → ℤ :=
FloorRing.ceil
/-- `Int.fract a` the fractional part of `a`, is `a` minus its floor. -/
def fract (a : α) : α :=
a - floor a
@[simp]
theorem floor_int : (Int.floor : ℤ → ℤ) = id :=
rfl
@[simp]
theorem ceil_int : (Int.ceil : ℤ → ℤ) = id :=
rfl
@[simp]
theorem fract_int : (Int.fract : ℤ → ℤ) = 0 :=
funext fun x => by simp [fract]
@[inherit_doc]
notation "⌊" a "⌋" => Int.floor a
@[inherit_doc]
notation "⌈" a "⌉" => Int.ceil a
-- Mathematical notation for `fract a` is usually `{a}`. Let's not even go there.
@[simp]
theorem floorRing_floor_eq : @FloorRing.floor = @Int.floor :=
rfl
@[simp]
theorem floorRing_ceil_eq : @FloorRing.ceil = @Int.ceil :=
rfl
/-! #### Floor -/
theorem gc_coe_floor : GaloisConnection ((↑) : ℤ → α) floor :=
FloorRing.gc_coe_floor
theorem le_floor : z ≤ ⌊a⌋ ↔ (z : α) ≤ a :=
(gc_coe_floor z a).symm
theorem floor_lt : ⌊a⌋ < z ↔ a < z :=
lt_iff_lt_of_le_iff_le le_floor
@[bound]
theorem floor_le (a : α) : (⌊a⌋ : α) ≤ a :=
gc_coe_floor.l_u_le a
theorem floor_le_iff : ⌊a⌋ ≤ z ↔ a < z + 1 := by rw [← lt_add_one_iff, floor_lt]; norm_cast
theorem lt_floor_iff : z < ⌊a⌋ ↔ z + 1 ≤ a := by rw [← add_one_le_iff, le_floor]; norm_cast
theorem floor_nonneg : 0 ≤ ⌊a⌋ ↔ 0 ≤ a := by rw [le_floor, Int.cast_zero]
theorem floor_lt_zero : ⌊a⌋ < 0 ↔ a < 0 := by rw [floor_lt, Int.cast_zero]
/-! #### Ceil -/
theorem gc_ceil_coe : GaloisConnection ceil ((↑) : ℤ → α) :=
FloorRing.gc_ceil_coe
theorem ceil_le : ⌈a⌉ ≤ z ↔ a ≤ z :=
gc_ceil_coe a z
theorem lt_ceil : z < ⌈a⌉ ↔ (z : α) < a :=
lt_iff_lt_of_le_iff_le ceil_le
@[bound]
theorem le_ceil (a : α) : a ≤ ⌈a⌉ :=
gc_ceil_coe.le_u_l a
lemma le_ceil_iff : z ≤ ⌈a⌉ ↔ z - 1 < a := by rw [← sub_one_lt_iff, lt_ceil]; norm_cast
lemma ceil_lt_iff : ⌈a⌉ < z ↔ a ≤ z - 1 := by rw [← le_sub_one_iff, ceil_le]; norm_cast
theorem ceil_nonpos : ⌈a⌉ ≤ 0 ↔ a ≤ 0 := by rw [ceil_le, cast_zero]
@[simp]
theorem ceil_pos : 0 < ⌈a⌉ ↔ 0 < a := by rw [lt_ceil, cast_zero]
end Int
section FloorRingToSemiring
variable [Ring α] [LinearOrder α] [FloorRing α]
/-! #### A floor ring as a floor semiring -/
-- see Note [lower instance priority]
instance (priority := 100) FloorRing.toFloorSemiring : FloorSemiring α where
floor a := ⌊a⌋.toNat
ceil a := ⌈a⌉.toNat
floor_of_neg {_} ha := Int.toNat_of_nonpos (Int.floor_lt.mpr (ha.trans_eq Int.cast_zero.symm)).le
gc_floor {a n} ha := by rw [Int.le_toNat (Int.floor_nonneg.2 ha), Int.le_floor, Int.cast_natCast]
gc_ceil a n := by rw [Int.toNat_le, Int.ceil_le, Int.cast_natCast]
theorem Int.floor_toNat (a : α) : ⌊a⌋.toNat = ⌊a⌋₊ :=
rfl
theorem Int.ceil_toNat (a : α) : ⌈a⌉.toNat = ⌈a⌉₊ :=
rfl
@[simp]
theorem Nat.floor_int : (Nat.floor : ℤ → ℕ) = Int.toNat :=
rfl
@[simp]
theorem Nat.ceil_int : (Nat.ceil : ℤ → ℕ) = Int.toNat :=
rfl
end FloorRingToSemiring
namespace FloorRing
/-! #### `Int.cast` is strictly monotone on floor rings -/
variable [Ring α] [LinearOrder α] [FloorRing α]
/-- A version of `Int.cast_mono` for floor rings, whose addition is not necessarily monotone. -/
theorem intCast_mono : Monotone (Int.cast : ℤ → α) :=
fun _ _ h ↦ (gc_ceil_coe _ _).mp <| h.trans' <| (gc_ceil_coe _ _).mpr (le_refl _)
/-- A version of `Int.cast_strictMono` for floor rings, whose addition is not necessarily
monotone. -/
theorem intCast_strictMono : StrictMono (Int.cast : ℤ → α) := by
obtain ⟨h⟩ : NeZero (1 : α) := inferInstance
refine strictMono_int_of_lt_succ fun n => (intCast_mono (Int.le_add_one (le_refl _))).lt_of_ne ?_
grind
theorem intCast_lt_iff {m n : ℤ} : (m : α) < (n : α) ↔ m < n :=
intCast_strictMono.lt_iff_lt
theorem intCast_le_iff {m n : ℤ} : (m : α) ≤ (n : α) ↔ m ≤ n :=
intCast_strictMono.le_iff_le
end FloorRing
namespace Int
variable [Ring α] [LinearOrder α] [FloorRing α] {a : α}
@[bound]
theorem ceil_nonneg (ha : 0 ≤ a) : 0 ≤ ⌈a⌉ := by
rw [← FloorRing.intCast_strictMono.le_iff_le (β := α), Int.cast_zero]
exact ha.trans (le_ceil a)
@[bound]
theorem floor_nonpos (ha : a ≤ 0) : ⌊a⌋ ≤ 0 := by
rw [← FloorRing.intCast_strictMono.le_iff_le (β := α), Int.cast_zero]
exact (floor_le a).trans ha
end Int
¤ Dauer der Verarbeitung: 0.13 Sekunden
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|
|
|
|
|