init repo
This commit is contained in:
		@@ -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"
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -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))
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								chapter-03/interval/__pycache__/interval.cpython-313.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								chapter-03/interval/__pycache__/interval.cpython-313.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										25
									
								
								chapter-03/interval/interval.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								chapter-03/interval/interval.py
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
							
								
								
									
										17
									
								
								chapter-03/interval/main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								chapter-03/interval/main.py
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
							
								
								
									
										11
									
								
								chapter-03/ratseq/binseq.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								chapter-03/ratseq/binseq.py
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
							
								
								
									
										66
									
								
								chapter-03/ratseq/main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								chapter-03/ratseq/main.py
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
							
								
								
									
										6
									
								
								chapter-03/ratseq/ratseq.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								chapter-03/ratseq/ratseq.py
									
									
									
									
									
										Normal file
									
								
							@@ -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()
 | 
			
		||||
		Reference in New Issue
	
	Block a user