The dyadic rationals #
Constructs the dyadic rationals as an ordered ring, equipped with a compatible embedding into the rationals.
The number of trailing zeros in the binary representation of i
.
Equations
- i.trailingZeros = if h : i = 0 then 0 else Int.trailingZeros.aux i.natAbs i h ⋯ 0
Instances For
Equations
- One or more equations did not get rendered due to their size.
- instDecidableEqDyadic.decEq Dyadic.zero Dyadic.zero = isTrue ⋯
- instDecidableEqDyadic.decEq Dyadic.zero (Dyadic.ofOdd n k hn) = isFalse ⋯
- instDecidableEqDyadic.decEq (Dyadic.ofOdd n k hn) Dyadic.zero = isFalse ⋯
Instances For
Returns the dyadic number representation of i * 2 ^ (-exp)
.
Equations
- Dyadic.ofIntWithPrec i prec = if h : i = 0 then Dyadic.zero else Dyadic.ofOdd (i >>> i.trailingZeros) (prec - ↑i.trailingZeros) ⋯
Instances For
Convert an integer to a dyadic number (which will necessarily have non-positive precision).
Equations
Instances For
Equations
- Dyadic.instOfNat n = { ofNat := Dyadic.ofInt ↑n }
Equations
- Dyadic.instIntCast = { intCast := Dyadic.ofInt }
Equations
- Dyadic.instNatCast = { natCast := fun (x : Nat) => Dyadic.ofInt ↑x }
Add two dyadic numbers.
Equations
- One or more equations did not get rendered due to their size.
- Dyadic.zero.add y = y
- x.add Dyadic.zero = x
Instances For
Multiply two dyadic numbers.
Equations
- Dyadic.zero.mul y = Dyadic.zero
- x.mul Dyadic.zero = Dyadic.zero
- (Dyadic.ofOdd n₁ k₁ hn₁).mul (Dyadic.ofOdd n₂ k₂ hn₂) = Dyadic.ofOdd (n₁ * n₂) (k₁ + k₂) ⋯
Instances For
Multiply two dyadic numbers.
Equations
- Dyadic.zero.pow i = if i = 0 then 1 else 0
- (Dyadic.ofOdd n k hn).pow i = Dyadic.ofOdd (n ^ i) (k * ↑i) ⋯
Instances For
Negate a dyadic number.
Equations
- Dyadic.zero.neg = Dyadic.zero
- (Dyadic.ofOdd n k hn).neg = Dyadic.ofOdd (-n) k ⋯
Instances For
Shift a dyadic number left by i
bits.
Equations
- Dyadic.zero.shiftLeft i = Dyadic.zero
- (Dyadic.ofOdd n k hn).shiftLeft i = Dyadic.ofOdd n (k - i) hn
Instances For
Shift a dyadic number right by i
bits.
Equations
- Dyadic.zero.shiftRight i = Dyadic.zero
- (Dyadic.ofOdd n k hn).shiftRight i = Dyadic.ofOdd n (k + i) hn
Instances For
Equations
- Dyadic.instHShiftLeftInt = { hShiftLeft := Dyadic.shiftLeft }
Equations
- Dyadic.instHShiftRightInt = { hShiftRight := Dyadic.shiftRight }
Convert a dyadic number to a rational number.
Equations
- Dyadic.zero.toRat = 0
- (Dyadic.ofOdd n (Int.ofNat k) hn).toRat = { num := n, den := 2 ^ k, den_nz := ⋯, reduced := ⋯ }
- (Dyadic.ofOdd n (Int.negSucc k) hn).toRat = ↑(n * ↑(2 ^ (k + 1)))
Instances For
The "precision" of a dyadic number, i.e. in n * 2^(-p)
with n
odd the precision is p
.
Equations
- Dyadic.zero.precision = none
- (Dyadic.ofOdd n k hn).precision = some k
Instances For
Convert a rational number x
to the greatest dyadic number with precision at most prec
which is less than or equal to x
.
Equations
- x.toDyadic (Int.ofNat n) = Dyadic.ofIntWithPrec (x.num <<< n / ↑x.den) (Int.ofNat n)
- x.toDyadic (Int.negSucc n) = Dyadic.ofIntWithPrec (x.num / ↑(x.den <<< (n + 1))) (Int.negSucc n)
Instances For
Rounds a dyadic rational x
down to the greatest dyadic number with precision at most prec
which is less than or equal to x
.
Equations
- Dyadic.zero.roundDown prec = Dyadic.zero
- (Dyadic.ofOdd n k hn).roundDown prec = match k - prec with | Int.ofNat l => Dyadic.ofIntWithPrec (n >>> l) prec | Int.negSucc a => Dyadic.ofOdd n k hn
Instances For
Determine if a dyadic rational is strictly less than another.
Equations
- Dyadic.zero.blt Dyadic.zero = false
- Dyadic.zero.blt (Dyadic.ofOdd n k hn) = decide (0 < n)
- (Dyadic.ofOdd n k hn).blt Dyadic.zero = decide (n < 0)
- (Dyadic.ofOdd a a_1 a_2).blt (Dyadic.ofOdd b b_1 b_2) = match b_1 - a_1 with | Int.ofNat l => decide (a <<< l < b) | Int.negSucc l => decide (a < b <<< (l + 1))
Instances For
Determine if a dyadic rational is less than or equal to another.
Equations
- Dyadic.zero.ble Dyadic.zero = true
- Dyadic.zero.ble (Dyadic.ofOdd n k hn) = decide (0 ≤ n)
- (Dyadic.ofOdd n k hn).ble Dyadic.zero = decide (n ≤ 0)
- (Dyadic.ofOdd a a_1 a_2).ble (Dyadic.ofOdd b b_1 b_2) = match b_1 - a_1 with | Int.ofNat l => decide (a <<< l ≤ b) | Int.negSucc l => decide (a ≤ b <<< (l + 1))
Instances For
Equations
- x✝¹.instDecidableLT x✝ = inferInstanceAs (Decidable (x✝¹.blt x✝ = true))
Equations
- x✝¹.instDecidableLE x✝ = inferInstanceAs (Decidable (x✝¹.ble x✝ = true))
roundUp x prec
is the least dyadic number with precision at most prec
which is greater than or equal to x
.
Equations
- Dyadic.zero.roundUp prec = Dyadic.zero
- (Dyadic.ofOdd n k hn).roundUp prec = match k - prec with | Int.ofNat l => Dyadic.ofIntWithPrec (-(-n) >>> l) prec | Int.negSucc a => Dyadic.ofOdd n k hn