Documentation

Batteries.Data.Rat.Basic

Basics for the Rational Numbers #

structure Rat :

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

  • mk' :: (
    • num : Int

      The numerator of the rational number is an integer.

    • den : Nat

      The denominator of the rational number is a natural number.

    • den_nz : self.den 0

      The denominator is nonzero.

    • reduced : self.num.natAbs.Coprime self.den

      The numerator and denominator are coprime: it is in "reduced form".

  • )
Instances For
    theorem Rat.den_nz (self : Rat) :
    self.den 0

    The denominator is nonzero.

    theorem Rat.reduced (self : Rat) :
    self.num.natAbs.Coprime self.den

    The numerator and denominator are coprime: it is in "reduced form".

    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 : (num.div g).natAbs.Coprime (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
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem Rat.normalize.den_nz {num : Int} {den : Nat} {g : Nat} (den_nz : den 0) (e : g = num.natAbs.gcd den) :
      den / g 0
      theorem Rat.normalize.reduced {num : Int} {den : Nat} {g : Nat} (den_nz : den 0) (e : g = num.natAbs.gcd den) :
      (num.div g).natAbs.Coprime (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
      Instances For
        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
        Instances For
          def Rat.ofInt (num : Int) :

          Embedding of Int in the rational numbers.

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

            Is this rational number integral?

            Equations
            • a.isInt = (a.den == 1)
            Instances For
              def Rat.divInt :
              IntIntRat

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

              Equations
              Instances For

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

                Equations
                Instances For
                  @[irreducible]
                  def Rat.ofScientific (m : Nat) (s : Bool) (e : Nat) :

                  Implements "scientific notation" 123.4e-5 for rational numbers. (This definition is @[irreducible] because you don't want to unfold it. Use Rat.ofScientific_def, Rat.ofScientific_true_def, or Rat.ofScientific_false_def instead.)

                  Equations
                  Instances For
                    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.
                    Instances For
                      instance Rat.instLT :
                      Equations
                      instance Rat.instDecidableLt (a : Rat) (b : Rat) :
                      Decidable (a < b)
                      Equations
                      instance Rat.instLE :
                      Equations
                      instance Rat.instDecidableLe (a : Rat) (b : Rat) :
                      Equations
                      @[irreducible]
                      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
                      • a.mul b = let g1 := a.num.natAbs.gcd b.den; let g2 := b.num.natAbs.gcd a.den; { num := a.num.div g1 * b.num.div g2, den := a.den / g2 * (b.den / g1), den_nz := , reduced := }
                      Instances For
                        instance Rat.instMul :
                        Equations
                        @[irreducible]
                        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
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          def Rat.div :
                          RatRatRat

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

                          Equations
                          • x✝.div x = x✝ * x.inv
                          Instances For
                            instance Rat.instDiv :

                            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 = a.den.gcd 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; num.natAbs.gcd g = num.natAbs.gcd den
                            @[irreducible]
                            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.
                            Instances For
                              instance Rat.instAdd :
                              Equations
                              def Rat.neg (a : Rat) :

                              Negation of rational numbers.

                              Equations
                              • a.neg = { num := -a.num, den := a.den, den_nz := , reduced := }
                              Instances For
                                instance Rat.instNeg :
                                Equations
                                theorem Rat.sub.aux (a : Rat) (b : Rat) {g : Nat} {ad : Nat} {bd : Nat} (hg : g = a.den.gcd 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; num.natAbs.gcd g = num.natAbs.gcd den
                                @[irreducible]
                                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.
                                Instances For
                                  instance Rat.instSub :
                                  Equations
                                  def Rat.floor (a : Rat) :

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

                                  Equations
                                  • a.floor = if a.den = 1 then a.num else a.num / a.den
                                  Instances For
                                    def Rat.ceil (a : Rat) :

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

                                    Equations
                                    • a.ceil = if a.den = 1 then a.num else a.num / a.den + 1
                                    Instances For
                                      def Rat.toFloat (a : Rat) :

                                      Convert this rational number to a Float value.

                                      Equations
                                      • a.toFloat = a.num.divFloat a.den
                                      Instances For

                                        Convert this floating point number to a rational value.

                                        Equations
                                        Instances For

                                          Convert this floating point number to a rational value, mapping non-finite values (inf, -inf, nan) to 0.

                                          Equations
                                          • a.toRat0 = a.toRat?.getD 0
                                          Instances For