The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Int.Log

40 declarations. Mathlib docs · Source

Integer logarithms in a field with respect to a natural base

This file defines two ℤ-valued analogs of the logarithm of r : R with base b : ℕ:

  • Int.log b r: Lower logarithm, or floor log. Greatest k such that ↑b^k ≤ r.
  • Int.clog b r: Upper logarithm, or ceil log. Least k such that r ≤ ↑b^k.

Note that Int.log gives the position of the left-most non-zero digit:

#eval (Int.log 10 (0.09 : ℚ), Int.log 10 (0.10 : ℚ), Int.log 10 (0.11 : ℚ))
--    (-2,                    -1,                    -1)
#eval (Int.log 10 (9 : ℚ),    Int.log 10 (10 : ℚ),   Int.log 10 (11 : ℚ))
--    (0,                     1,                     1)

which means it can be used for computing digit expansions

import Data.Fin.VecNotation
import Mathlib.Data.Rat.Floor

def digits (b : ℕ) (q : ℚ) (n : ℕ) : ℕ :=
  ⌊q * ((b : ℚ) ^ (n - Int.log b q))⌋₊ % b

#eval digits 10 (1/7) ∘ ((↑) : Fin 8 → ℕ)
-- ![1, 4, 2, 8, 5, 7, 1, 4]

Main results

  • For Int.log:
    • Int.zpow_log_le_self, Int.lt_zpow_succ_log_self: the bounds formed by Int.log, (b : R) ^ log b r ≤ r < (b : R) ^ (log b r + 1).
    • Int.zpow_log_gi: the Galois coinsertion between zpow and Int.log.
  • For Int.clog:
    • Int.zpow_pred_clog_lt_self, Int.self_le_zpow_clog: the bounds formed by Int.clog, (b : R) ^ (clog b r - 1) < r ≤ (b : R) ^ clog b r.
    • Int.clog_zpow_gi: the Galois insertion between Int.clog and zpow.
  • Int.neg_log_inv_eq_clog, Int.neg_clog_inv_eq_log: the link between the two definitions.

def Int.log E3 #

{R : Type u_1} → [inst : Semifield R] → [inst_1 : LinearOrder R] → [FloorSemiring R] → ℕ → R → ℤ

The greatest power of b such that b ^ log b r ≤ r.

theorem Int.log_of_one_le_right E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ) {r : R},
  1 ≤ r → Int.log b r = ↑(Nat.log b ⌊r⌋₊)

theorem Int.log_of_right_le_one E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ) {r : R},
  r ≤ 1 → Int.log b r = -↑(Nat.clog b ⌈r⁻¹⌉₊)

theorem Int.log_natCast E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b n : ℕ),
  Int.log b ↑n = ↑(Nat.log b n)

theorem Int.log_ofNat E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b n : ℕ)
  [inst_3 : n.AtLeastTwo], Int.log b (OfNat.ofNat n) = ↑(Nat.log b (OfNat.ofNat n))

theorem Int.log_of_left_le_one E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] {b : ℕ},
  b ≤ 1 → ∀ (r : R), Int.log b r = 0

theorem Int.log_one_right E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ), Int.log b 1 = 0

theorem Int.log_zero_left E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (r : R), Int.log 0 r = 0

theorem Int.log_one_left E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (r : R), Int.log 1 r = 0

theorem Int.log_of_right_le_zero E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) {r : R}, r ≤ 0 → Int.log b r = 0

theorem Int.zpow_log_le_self E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ} {r : R}, 1 < b → 0 < r → ↑b ^ Int.log b r ≤ r

theorem Int.lt_zpow_succ_log_self E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ (r : R), r < ↑b ^ (Int.log b r + 1)

theorem Int.log_zero_right E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ), Int.log b 0 = 0

theorem Int.log_zpow E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ (z : ℤ), Int.log b (↑b ^ z) = z

theorem Int.log_mono_right E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ} {r₁ r₂ : R}, 0 < r₁ → r₁ ≤ r₂ → Int.log b r₁ ≤ Int.log b r₂

def Int.zpowLogGi E1 #

(R : Type u_1) →
  [inst : Semifield R] →
    [inst_1 : LinearOrder R] →
      [inst_2 : FloorSemiring R] →
        [inst_3 : IsStrictOrderedRing R] →
          {b : ℕ} → (hb : 1 < b) → GaloisCoinsertion (fun z ↦ ⟨↑b ^ z, ⋯⟩) fun r ↦ Int.log b ↑r

Over suitable subtypes, zpow and Int.log form a Galois coinsertion

theorem Int.lt_zpow_iff_log_lt E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ {x : ℤ} {r : R}, 0 < r → (r < ↑b ^ x ↔ Int.log b r < x)

zpow b and Int.log b (almost) form a Galois connection.

theorem Int.zpow_le_iff_le_log E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ {x : ℤ} {r : R}, 0 < r → (↑b ^ x ≤ r ↔ x ≤ Int.log b r)

zpow b and Int.log b (almost) form a Galois connection.

def Int.clog E3 #

{R : Type u_1} → [inst : Semifield R] → [inst_1 : LinearOrder R] → [FloorSemiring R] → ℕ → R → ℤ

The least power of b such that r ≤ b ^ log b r.

theorem Int.clog_of_one_le_right E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ) {r : R},
  1 ≤ r → Int.clog b r = ↑(Nat.clog b ⌈r⌉₊)

theorem Int.clog_of_right_le_one E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ) {r : R},
  r ≤ 1 → Int.clog b r = -↑(Nat.log b ⌊r⁻¹⌋₊)

theorem Int.clog_natCast E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b n : ℕ),
  Int.clog b ↑n = ↑(Nat.clog b n)

theorem Int.clog_ofNat E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b n : ℕ)
  [inst_3 : n.AtLeastTwo], Int.clog b (OfNat.ofNat n) = ↑(Nat.clog b (OfNat.ofNat n))

theorem Int.clog_one_right E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (b : ℕ), Int.clog b 1 = 0

theorem Int.clog_zero_left E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (r : R), Int.clog 0 r = 0

theorem Int.clog_one_left E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] (r : R), Int.clog 1 r = 0

theorem Int.clog_of_right_le_zero E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) {r : R}, r ≤ 0 → Int.clog b r = 0

theorem Int.clog_inv E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) (r : R), Int.clog b r⁻¹ = -Int.log b r

theorem Int.log_inv E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) (r : R), Int.log b r⁻¹ = -Int.clog b r

theorem Int.neg_log_inv_eq_clog E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) (r : R), -Int.log b r⁻¹ = Int.clog b r

theorem Int.neg_clog_inv_eq_log E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ) (r : R), -Int.clog b r⁻¹ = Int.log b r

theorem Int.clog_of_left_le_one E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] {b : ℕ},
  b ≤ 1 → ∀ (r : R), Int.clog b r = 0

theorem Int.self_le_zpow_clog E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ (r : R), r ≤ ↑b ^ Int.clog b r

theorem Int.zpow_pred_clog_lt_self E3 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ} {r : R}, 1 < b → 0 < r → ↑b ^ (Int.clog b r - 1) < r

theorem Int.clog_zero_right E0 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  (b : ℕ), Int.clog b 0 = 0

theorem Int.clog_zpow E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ (z : ℤ), Int.clog b (↑b ^ z) = z

theorem Int.clog_mono_right E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ} {r₁ r₂ : R}, 0 < r₁ → r₁ ≤ r₂ → Int.clog b r₁ ≤ Int.clog b r₂

def Int.clogZPowGi E1 #

(R : Type u_1) →
  [inst : Semifield R] →
    [inst_1 : LinearOrder R] →
      [inst_2 : FloorSemiring R] →
        [inst_3 : IsStrictOrderedRing R] →
          {b : ℕ} → (hb : 1 < b) → GaloisInsertion (fun r ↦ Int.clog b ↑r) fun z ↦ ⟨↑b ^ z, ⋯⟩

Over suitable subtypes, Int.clog and zpow form a Galois insertion

theorem Int.zpow_lt_iff_lt_clog E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ {x : ℤ} {r : R}, 0 < r → (↑b ^ x < r ↔ x < Int.clog b r)

Int.clog b and zpow b (almost) form a Galois connection.

theorem Int.le_zpow_iff_clog_le E1 #

∀ {R : Type u_1} [inst : Semifield R] [inst_1 : LinearOrder R] [inst_2 : FloorSemiring R] [IsStrictOrderedRing R]
  {b : ℕ}, 1 < b → ∀ {x : ℤ} {r : R}, 0 < r → (r ≤ ↑b ^ x ↔ Int.clog b r ≤ x)

Int.clog b and zpow b (almost) form a Galois connection.