Documentation

Std.Data.Rat.Basic

Basics for the Rational Numbers #

theorem Rat.ext_iff (x : Rat) (y : Rat) :
x = y x.num = y.num x.den = y.den
theorem Rat.ext (x : Rat) (y : Rat) (num : x.num = y.num) (den : x.den = y.den) :
x = y
structure Rat :
  • mk' :: (
    • The numerator of the rational number is an integer.

      num : Int
    • The denominator of the rational number is a natural number.

      den : Nat
    • The denominator is nonzero.

      den_nz : autoParam (den 0) _auto✝
    • The numerator and denominator are coprime: it is in "reduced form".

  • )

Rational numbers, implemented as a pair of integers num / den such that the denominator is positive and the numerator and denominator are coprime.

Instances For
    Equations
    Equations
    • One or more equations did not get rendered due to their size.
    instance instReprRat :
    Equations
    • One or more equations did not get rendered due to their size.
    theorem Rat.den_pos (self : Rat) :
    0 < self.den
    @[inline]
    def Rat.maybeNormalize (num : Int) (den : Nat) (g : Nat) (den_nz : den / g 0) (reduced : Nat.coprime (Int.natAbs (Int.div num g)) (den / g)) :

    Auxiliary definition for Rat.normalize. Constructs num / den as a rational number, dividing both num and den by g (which is the gcd of the two) if it is not 1.

    Equations
    theorem Rat.normalize.den_nz {num : Int} {den : Nat} {g : Nat} (den_nz : den 0) (e : g = Nat.gcd (Int.natAbs num) den) :
    den / g 0
    theorem Rat.normalize.reduced {num : Int} {den : Nat} {g : Nat} (den_nz : den 0) (e : g = Nat.gcd (Int.natAbs num) den) :
    Nat.coprime (Int.natAbs (Int.div num g)) (den / g)
    @[inline]
    def Rat.normalize (num : Int) (den : optParam Nat 1) (den_nz : autoParam (den 0) _auto✝) :

    Construct a normalized Rat from a numerator and nonzero denominator. This is a "smart constructor" that divides the numerator and denominator by the gcd to ensure that the resulting rational number is normalized.

    Equations
    • One or more equations did not get rendered due to their size.
    def mkRat (num : Int) (den : Nat) :

    Construct a rational number from a numerator and denominator. This is a "smart constructor" that divides the numerator and denominator by the gcd to ensure that the resulting rational number is normalized, and returns zero if den is zero.

    Equations
    def Rat.ofInt (num : Int) :

    Embedding of Int in the rational numbers.

    Equations
    instance Rat.instOfNatRat {n : Nat} :
    Equations
    • Rat.instOfNatRat = { ofNat := n }
    @[inline]
    def Rat.isInt (a : Rat) :

    Is this rational number integral?

    Equations
    def Rat.divInt :
    IntIntRat

    Form the quotient n / d where n d : Int.

    Equations

    Form the quotient n / d where n d : Int.

    Equations
    def Rat.ofScientific (m : Nat) (s : Bool) (e : Nat) :

    Implements "scientific notation" 123.4e-5 for rational numbers.

    Equations
    def Rat.blt (a : Rat) (b : Rat) :

    Rational number strictly less than relation, as a Bool.

    Equations
    • One or more equations did not get rendered due to their size.
    instance Rat.instLTRat :
    Equations
    instance Rat.instLERat :
    Equations
    def Rat.mul (a : Rat) (b : Rat) :

    Multiplication of rational numbers. (This definition is @[irreducible] because you don't want to unfold it. Use Rat.mul_def instead.)

    Equations
    Equations
    def Rat.inv (a : Rat) :

    The inverse of a rational number. Note: inv 0 = 0. (This definition is @[irreducible] because you don't want to unfold it. Use Rat.inv_def instead.)

    Equations
    def Rat.div :
    RatRatRat

    Division of rational numbers. Note: div a 0 = 0.

    Equations

    Division of rational numbers. Note: div a 0 = 0. Written with a separate function Rat.div as a wrapper so that the definition is not unfolded at .instance transparency.

    Equations
    theorem Rat.add.aux (a : Rat) (b : Rat) {g : Nat} {ad : Nat} {bd : Nat} (hg : g = Nat.gcd a.den b.den) (had : ad = a.den / g) (hbd : bd = b.den / g) :
    let den := ad * b.den; let num := a.num * bd + b.num * ad; Nat.gcd (Int.natAbs num) g = Nat.gcd (Int.natAbs num) den
    def Rat.add (a : Rat) (b : Rat) :

    Addition of rational numbers. (This definition is @[irreducible] because you don't want to unfold it. Use Rat.add_def instead.)

    Equations
    • One or more equations did not get rendered due to their size.
    Equations
    def Rat.neg (a : Rat) :

    Negation of rational numbers.

    Equations
    Equations
    theorem Rat.sub.aux (a : Rat) (b : Rat) {g : Nat} {ad : Nat} {bd : Nat} (hg : g = Nat.gcd a.den b.den) (had : ad = a.den / g) (hbd : bd = b.den / g) :
    let den := ad * b.den; let num := a.num * bd - b.num * ad; Nat.gcd (Int.natAbs num) g = Nat.gcd (Int.natAbs num) den
    def Rat.sub (a : Rat) (b : Rat) :

    Subtraction of rational numbers. (This definition is @[irreducible] because you don't want to unfold it. Use Rat.sub_def instead.)

    Equations
    • One or more equations did not get rendered due to their size.
    Equations
    def Rat.floor (a : Rat) :

    The floor of a rational number a is the largest integer less than or equal to a.

    Equations
    def Rat.ceil (a : Rat) :

    The ceiling of a rational number a is the smallest integer greater than or equal to a.

    Equations
    • Rat.ceil a = if a.den = 1 then a.num else a.num / a.den + 1