Documentation

Init.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
    @[implicit_reducible]
    def instDecidableEqRat.decEq (x✝ x✝¹ : Rat) :
    Decidable (x✝ = x✝¹)
    Instances For
      @[implicit_reducible]
      Instances For
        @[implicit_reducible]
        @[implicit_reducible]
        @[implicit_reducible]
        instance instReprRat :
        theorem Rat.den_pos (self : Rat) :
        0 < self.den
        @[inline]
        def Rat.maybeNormalize (num : Int) (den g : Nat) (dvd_num : g num) (dvd_den : g den) (den_nz : den / g 0) (reduced : (num / 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.

        Instances For
          theorem Rat.normalize.dvd_num {num : Int} {den g : Nat} (e : g = num.natAbs.gcd den) :
          g num
          theorem Rat.normalize.dvd_den {num : Int} {den g : Nat} (e : g = num.natAbs.gcd den) :
          g den
          theorem Rat.normalize.den_nz {num : Int} {den g : Nat} (den_nz : den 0) (e : g = num.natAbs.gcd den) :
          den / g 0
          theorem Rat.normalize.reduced {num : Int} {den g : Nat} (den_nz : den 0) (e : g = num.natAbs.gcd den) :
          (num / g).natAbs.Coprime (den / g)
          @[inline]
          def Rat.normalize (num : Int) (den : Nat := 1) (den_nz : den 0 := by decide) :

          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.

          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.

            Instances For
              def Rat.ofInt (num : Int) :

              Embedding of Int in the rational numbers.

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

                Is this rational number integral?

                Instances For
                  def Rat.divInt :
                  IntIntRat

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

                  Instances For

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

                    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.)

                      Instances For
                        @[implicit_reducible]
                        def Rat.blt (a b : Rat) :

                        Rational number strictly less than relation, as a Bool.

                        Instances For
                          @[implicit_reducible]
                          instance Rat.instLT :
                          @[implicit_reducible]
                          instance Rat.instDecidableLt (a b : Rat) :
                          Decidable (a < b)
                          @[implicit_reducible]
                          instance Rat.instLE :
                          @[implicit_reducible]
                          instance Rat.instDecidableLe (a b : Rat) :
                          @[implicit_reducible]
                          instance Rat.instMin :
                          @[implicit_reducible]
                          instance Rat.instMax :
                          @[irreducible]
                          def Rat.mul (a b : Rat) :

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

                          Instances For
                            @[implicit_reducible]
                            instance Rat.instMul :
                            @[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.)

                            Instances For
                              @[implicit_reducible]
                              instance Rat.instInv :
                              def Rat.pow (q : Rat) (n : Nat) :
                              Instances For
                                @[implicit_reducible]
                                def Rat.zpow (q : Rat) (i : Int) :
                                Instances For
                                  @[implicit_reducible]
                                  def Rat.div :
                                  RatRatRat

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

                                  Instances For
                                    @[implicit_reducible]
                                    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.

                                    theorem Rat.add.aux (a b : Rat) {g ad bd : Nat} (hg : g = a.den.gcd b.den) (had : ad = a.den / g) (hbd : bd = b.den / g) :
                                    have den := ad * b.den; have num := a.num * bd + b.num * ad; num.natAbs.gcd g = num.natAbs.gcd den
                                    @[irreducible]
                                    def Rat.add (a b : Rat) :

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

                                    Instances For
                                      @[implicit_reducible]
                                      instance Rat.instAdd :
                                      def Rat.neg (a : Rat) :

                                      Negation of rational numbers.

                                      Instances For
                                        @[implicit_reducible]
                                        instance Rat.instNeg :
                                        theorem Rat.sub.aux (a b : Rat) {g ad bd : Nat} (hg : g = a.den.gcd b.den) (had : ad = a.den / g) (hbd : bd = b.den / g) :
                                        have den := ad * b.den; have num := a.num * bd - b.num * ad; num.natAbs.gcd g = num.natAbs.gcd den
                                        @[irreducible]
                                        def Rat.sub (a b : Rat) :

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

                                        Instances For
                                          @[implicit_reducible]
                                          instance Rat.instSub :
                                          def Rat.floor (a : Rat) :

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

                                          Instances For
                                            def Rat.ceil (a : Rat) :

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

                                            Instances For
                                              def Rat.abs (a : Rat) :

                                              The absolute value of a rational number a is a if a ≥ 0 and -a if a ≤ 0.

                                              Instances For