diff --git a/chapter-03/binseq/main.py b/chapter-03/binseq/main.py index c39ee1b..4819a15 100644 --- a/chapter-03/binseq/main.py +++ b/chapter-03/binseq/main.py @@ -2,7 +2,10 @@ from binseq import REAL from math import isqrt # integer sqrt # Theorem 3.1 (2) +# Es gibt eine berechenbare Folge (qn)n∈N rationaler Zahlen, die schnell gegen x konvergiert. +# d.h. |x − qi| < 2^(−i) für alle i ∈ N. +# bin_seq(1/3) = 0.010101... def one_third(n): if n == 1: return "." if n % 2 == 0: return "0" diff --git a/chapter-03/decidable/__pycache__/decidable.cpython-313.pyc b/chapter-03/decidable/__pycache__/decidable.cpython-313.pyc deleted file mode 100644 index d03a1f4..0000000 Binary files a/chapter-03/decidable/__pycache__/decidable.cpython-313.pyc and /dev/null differ diff --git a/chapter-03/decidable/main.py b/chapter-03/decidable/main.py index 3ee7393..9309044 100644 --- a/chapter-03/decidable/main.py +++ b/chapter-03/decidable/main.py @@ -2,6 +2,7 @@ from decidable import REAL from gmpy2 import mpq # Theorem 3.1 (5) +# Es gibt eine ganze Zahl z ∈ Z und eine entscheidbare Menge A ⊆ N mit x = z + xA, wobei xA := SUMi∈A 2^(−i−1) print(REAL.from_rational(mpq(1,3)).as_str(10)) print(REAL.from_rational(mpq(41,5)).as_str(10)) diff --git a/chapter-03/dedekint/__pycache__/dedekint.cpython-313.pyc b/chapter-03/dedekint/__pycache__/dedekint.cpython-313.pyc deleted file mode 100644 index 4ab8701..0000000 Binary files a/chapter-03/dedekint/__pycache__/dedekint.cpython-313.pyc and /dev/null differ diff --git a/chapter-03/dedekint/main.py b/chapter-03/dedekint/main.py index 15fd838..4bf32b8 100644 --- a/chapter-03/dedekint/main.py +++ b/chapter-03/dedekint/main.py @@ -3,6 +3,7 @@ from dedekint import REAL from gmpy2 import mpq # Theorem 3.1 (4) +# Die Menge {q ∈ Q | q < x} ist eine entscheidbare Menge rationaler Zahlen (Dedekind’scher Schnitt) def sqrt_two(q): if q <= 0 or q * q < 2: return True diff --git a/chapter-03/interval/__pycache__/interval.cpython-313.pyc b/chapter-03/interval/__pycache__/interval.cpython-313.pyc new file mode 100644 index 0000000..63dadc4 Binary files /dev/null and b/chapter-03/interval/__pycache__/interval.cpython-313.pyc differ diff --git a/chapter-03/interval/interval.py b/chapter-03/interval/interval.py new file mode 100644 index 0000000..17aef63 --- /dev/null +++ b/chapter-03/interval/interval.py @@ -0,0 +1,25 @@ +class REAL: # nested.py + def __init__(self, l, u): + self.lower = l + self.upper = u + + def asString(self, n): + l = self.lower(n) + u = self.upper(n) + return ("[ " + l.numerator.digits() + "/" + l.denominator.digits() + + ", " + u.numerator.digits() + "/" + u.denominator.digits() + "]") + + def __add__(self, y): + return REAL_add(self, y) + + +class REAL_add(REAL): + def __init__(self, x, y): + self.x = x + self.y = y + + def lower(self, n): + return self.x.lower(n) + self.y.lower(n) + + def upper(self, n): + return self.x.upper(n) + self.y.upper(n) diff --git a/chapter-03/interval/main.py b/chapter-03/interval/main.py new file mode 100644 index 0000000..80cfb61 --- /dev/null +++ b/chapter-03/interval/main.py @@ -0,0 +1,17 @@ +from interval import REAL +from gmpy2 import mpq # rationale Zahlen + + +def lower_one(n): + return mpq(1,1)-mpq(1,n) +def upper_one(n): + return mpq(1,1)+mpq(1,n) + +x = REAL(lower_one,upper_one); y = x + x + +print ("x: "+x.asString(10)) +print ("y: "+y.asString(10)) + + +print ("x: "+x.asString(10000)) +print ("y: "+y.asString(10000)) \ No newline at end of file diff --git a/chapter-03/ratseq/binseq.py b/chapter-03/ratseq/binseq.py new file mode 100644 index 0000000..dea2445 --- /dev/null +++ b/chapter-03/ratseq/binseq.py @@ -0,0 +1,11 @@ +from collections.abc import Callable + +class REAL: + def __init__(self, f: Callable[[int], str]) -> None: + self.binseq = f + + def as_string(self, w: int) -> str: + s = "" + for n in range(w): + s += self.binseq(n) + return s \ No newline at end of file diff --git a/chapter-03/ratseq/main.py b/chapter-03/ratseq/main.py new file mode 100644 index 0000000..e9aa7f3 --- /dev/null +++ b/chapter-03/ratseq/main.py @@ -0,0 +1,66 @@ +from gmpy2 import mpq # rationale Zahlen +from binseq import REAL as REAL_bs +from ratseq import REAL as REAL_ra + + +def ra_oneThird(p): + return mpq(1, 3) + + +x = REAL_ra(ra_oneThird) +print("x: " + x.asString(-50)) + +# Theorem 3.1 (3) +# Es gibt eine berechenbare Intervallschachtelung von Intervallen mit rationalen Endpunkten, die gegen x konvergiert +# d.h. es gibt berechen- +# bare Folgen (an)n∈N und (bn)n∈N + +class REAL_ra_from_bs(REAL_ra): # Subklasse + def __init__(self, x): + self.x = x + + def approx(self, p): + n = 0 + q = mpq(0, 1) + v = mpq(1, 1) + while True: + c = self.x.binseq(n) + if c == "-": v = mpq(-1, 1) + if c == ".": break + q = 2 * q + if c == "1": q += v + n = n + 1 + k = 1 + while k <= -p: + v = v / 2 + c = self.x.binseq(n + k) + if c == "1": q += v + k = k + 1 + return q + + +def bs_one_third(n): + if n == 1: return "." + if n % 2 == 0: return "0" + return "1" + + +def bs_almost_one(n): + if n == 0: return "1" + if n == 1: return "." + if n < 40: return "0" + return "1" + + +# Create binseq +y1 = REAL_bs(bs_almost_one) +# Create rational approx of the created binseq +z1 = REAL_ra_from_bs(y1) + +# Create binseq +y2 = REAL_bs(bs_one_third) +# Create rational approx of the created binseq +z2 = REAL_ra_from_bs(y2) + +print("y1: " + y1.as_string(50) + "\nz1: " + z1.asString(-50)) +print("y2: " + y2.as_string(50) + "\nz2: " + z2.asString(-50)) \ No newline at end of file diff --git a/chapter-03/ratseq/ratseq.py b/chapter-03/ratseq/ratseq.py new file mode 100644 index 0000000..470c135 --- /dev/null +++ b/chapter-03/ratseq/ratseq.py @@ -0,0 +1,6 @@ +class REAL: + def __init__(self, f): self.approx = f + + def asString(self, p): + q = self.approx(p) + return q.numerator.digits() + "/" + q.denominator.digits() \ No newline at end of file