Documentation

Complexity.Computability.PolyTime.Int.ExactDiv

Plain polynomial-time signed integer division and modular reduction #

Plain Turing.MultiTapeTM.ComputableInPolyTime certificates, exact-division identities, and sign-magnitude code-length bounds for signed binary integer division and remainder over Turing.MultiTapeTM.encIntBin:

All certificates are total (at divisor 0, a / 0 = 0, a.tdiv 0 = 0, a % 0 = a, and a.tmod 0 = a) and build on the binary natural division and modular reduction primitives of PolyTime/Nat/BinaryDiv and PolyTime/Int/BinaryDiv without constructing new Turing machines.

Main definitions #

Main results #

References #

Exact division and power-of-two modular reduction #

def Int.modPowTwo (x : ℤ) (ℓ : ℕ) :

Least nonnegative residue of x modulo 2 ^ ℓ, i.e., x - 2 ^ ℓ * ⌊x / 2 ^ ℓ⌋.

Instances For
    theorem Int.modPowTwo_eq_emod (x : ℤ) (ℓ : ℕ) :
    x.modPowTwo ℓ = x.emod (2 ^ ℓ)
    theorem Int.modPowTwo_eq_sub_mul_ediv (x : ℤ) (ℓ : ℕ) :
    x.modPowTwo ℓ = x - 2 ^ ℓ * x.ediv (2 ^ ℓ)
    theorem Int.modPowTwo_nonneg (x : ℤ) (ℓ : ℕ) :
    0 ≤ x.modPowTwo ℓ
    theorem Int.modPowTwo_lt (x : ℤ) (ℓ : ℕ) :
    x.modPowTwo ℓ < 2 ^ ℓ
    theorem Int.natAbs_modPowTwo_lt (x : ℤ) (ℓ : ℕ) :
    (x.modPowTwo ℓ).natAbs < 2 ^ ℓ
    theorem Int.modPowTwo_eq_of_dvd_sub {x z : ℤ} {ℓ : ℕ} (hz₀ : 0 ≤ z) (hz₁ : z < 2 ^ ℓ) (hdvd : 2 ^ ℓ ∣ x - z) :
    x.modPowTwo ℓ = z

    If z ∈ [0, 2 ^ ℓ) and 2 ^ ℓ divides x - z, then x.modPowTwo ℓ = z.

    theorem Int.ediv_mul_left_exact {d : ℤ} (q : ℤ) (hd : d ≠ 0) :
    (d * q).ediv d = q

    Exact Euclidean division of a left multiple (d * q).ediv d = q for d ≠ 0, stated directly on Int.ediv for rewriting without folding to /.

    theorem Int.ediv_mul_right_exact (q : ℤ) {d : ℤ} (hd : d ≠ 0) :
    (q * d).ediv d = q

    Exact Euclidean division of a right multiple (q * d).ediv d = q for d ≠ 0, stated directly on Int.ediv for rewriting without folding to /.

    Sign-magnitude reductions and magnitude bounds #

    theorem Int.natAbs_emod_lt_of_ne_zero (a : ℤ) {b : ℤ} (hb : b ≠ 0) :

    Sign-magnitude encIntBin code-length bounds #

    Truncated integer division never increases the encIntBin code length of the dividend.

    Euclidean integer division never increases the encIntBin code length of the dividend.

    For b ≠ 0, the Euclidean remainder a.emod b has encIntBin code length at most (encIntBin b).length.

    Unconditionally on b (including b = 0), (encIntBin (a.emod b)).length ≤ max (encIntBin a).length (encIntBin b).length.

    Reducing modulo 2 ^ ℓ produces an encIntBin code of length at most ℓ + 1.

    Plain polynomial-time certificates #

    Truncated-toward-zero division of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_tdiv' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => (left a).tdiv (right a)) input encIntBin

    Truncated-toward-zero division of computed binary integers.

    Truncated-toward-zero remainder of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_tmod' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => (left a).tmod (right a)) input encIntBin

    Truncated-toward-zero remainder of computed binary integers.

    Euclidean division (Int.ediv) of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_ediv' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => (left a).ediv (right a)) input encIntBin

    Euclidean division (Int.ediv) of computed binary integers.

    Euclidean division (/) of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_div' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => left a / right a) input encIntBin

    Euclidean division (/) of computed binary integers.

    Euclidean remainder (Int.emod) of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_emod' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => (left a).emod (right a)) input encIntBin

    Euclidean remainder (Int.emod) of computed binary integers.

    Euclidean remainder (%) of signed binary integers is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_mod' {α : Type} {input : α ↪ List Bool} {left right : α → ℤ} (hleft : ComputableInPolyTime left input encIntBin) (hright : ComputableInPolyTime right input encIntBin) :
    ComputableInPolyTime (fun (a : α) => left a % right a) input encIntBin

    Euclidean remainder (%) of computed binary integers.

    Reduction Int.modPowTwo p.1 p.2 = p.1 % 2 ^ p.2 of a binary integer modulo 2 ^ ℓ (with ℓ in unary) is computable in polynomial time.

    theorem Turing.MultiTapeTM.ComputableInPolyTime.intBin_modPowTwo' {α : Type} {input : α ↪ List Bool} {value : α → ℤ} {exponent : α → ℕ} (hvalue : ComputableInPolyTime value input encIntBin) (hexponent : ComputableInPolyTime exponent input encUnary) :
    ComputableInPolyTime (fun (a : α) => (value a).modPowTwo (exponent a)) input encIntBin

    Reduction Int.modPowTwo (value a) (exponent a) of a computed binary integer modulo a computed unary power of two.