Browse Source

Fix Python code style

Martin Thoma 9 years ago
parent
commit
59b3d42774

+ 0 - 75
source-code/Pseudocode/Calculate-Legendre/calculateLegendre.py

@@ -1,75 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-def isPrime(a):
-    return all(a % i for i in xrange(2, a))
-
-# http://stackoverflow.com/a/14793082/562769
-def factorize(n):
-    factors = []
-
-    p = 2
-    while True:
-        while(n % p == 0 and n > 0): #while we can divide by smaller number, do so
-            factors.append(p)
-            n = n / p
-        p += 1  #p is not necessary prime, but n%p == 0 only for prime numbers
-        if p > n / p:
-            break
-    if n > 1:
-        factors.append(n)
-    return factors
-
-def calculateLegendre(a, p):
-	"""
-	Calculate the legendre symbol (a, p) with p is prime.
-	The result is either -1, 0 or 1
-
-	>>> calculateLegendre(3, 29)
-	-1
-	>>> calculateLegendre(111, 41) # Beispiel aus dem Skript, S. 114
-	-1
-	>>> calculateLegendre(113, 41) # Beispiel aus dem Skript, S. 114
-	1
-	>>> calculateLegendre(2, 31)
-	1
-	>>> calculateLegendre(5, 31)
-	1
-	>>> calculateLegendre(150, 1009) # http://math.stackexchange.com/q/221223/6876
-	1
-	>>> calculateLegendre(25, 1009) # http://math.stackexchange.com/q/221223/6876
-	1
-	>>> calculateLegendre(2, 1009) # http://math.stackexchange.com/q/221223/6876
-	1
-	>>> calculateLegendre(3, 1009) # http://math.stackexchange.com/q/221223/6876
-	1
-	"""
-	if a >= p or a < 0:
-		return calculateLegendre(a % p, p)
-	elif a == 0 or a == 1:
-		return a
-	elif a == 2:
-		if p%8 == 1 or p%8 == 7:
-			return 1
-		else:
-			return -1
-	elif a == p-1:
-		if p%4 == 1:
-			return 1
-		else:
-			return -1
-	elif not isPrime(a):
-		factors = factorize(a)
-		product = 1
-		for pi in factors:
-			product *= calculateLegendre(pi, p)
-		return product
-	else:
-		if ((p-1)/2)%2==0 or ((a-1)/2)%2==0:
-			return calculateLegendre(p, a)
-		else:
-			return (-1)*calculateLegendre(p, a)
-
-if __name__ == "__main__":
-	import doctest
-	doctest.testmod()

+ 94 - 0
source-code/Pseudocode/Calculate-Legendre/calculate_legendre.py

@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def is_prime(a):
+    """
+    Check if `a` is a prime number.
+
+    Parameters
+    ----------
+    a : int, a >= 2
+    """
+    return all(a % i for i in xrange(2, a))
+
+
+# http://stackoverflow.com/a/14793082/562769
+def factorize(n):
+    factors = []
+
+    p = 2
+    while True:
+        # while we can divide by smaller number, do so
+        while(n % p == 0 and n > 0):
+            factors.append(p)
+            n = n / p
+        p += 1  # p is not necessary prime, but n%p == 0 only for prime numbers
+        if p > n / p:
+            break
+    if n > 1:
+        factors.append(n)
+    return factors
+
+
+def calculate_legendre(a, p):
+    """
+    Calculate the legendre symbol (a, p) with p is prime.
+    The result is either -1, 0 or 1
+
+    >>> calculate_legendre(3, 29)
+    -1
+    >>> calculate_legendre(111, 41) # Beispiel aus dem Skript, S. 114
+    -1
+    >>> calculate_legendre(113, 41) # Beispiel aus dem Skript, S. 114
+    1
+    >>> calculate_legendre(2, 31)
+    1
+    >>> calculate_legendre(5, 31)
+    1
+
+    # http://math.stackexchange.com/q/221223/6876
+    >>> calculate_legendre(150, 1009)
+    1
+
+    # http://math.stackexchange.com/q/221223/6876
+    >>> calculate_legendre(25, 1009)
+    1
+
+    # http://math.stackexchange.com/q/221223/6876
+    >>> calculate_legendre(2, 1009)
+    1
+
+    # http://math.stackexchange.com/q/221223/6876
+    >>> calculate_legendre(3, 1009)
+    1
+    """
+    if a >= p or a < 0:
+        return calculate_legendre(a % p, p)
+    elif a == 0 or a == 1:
+        return a
+    elif a == 2:
+        if p % 8 == 1 or p % 8 == 7:
+            return 1
+        else:
+            return -1
+    elif a == p-1:
+        if p % 4 == 1:
+            return 1
+        else:
+            return -1
+    elif not is_prime(a):
+        factors = factorize(a)
+        product = 1
+        for pi in factors:
+            product *= calculate_legendre(pi, p)
+        return product
+    else:
+        if ((p-1)/2) % 2 == 0 or ((a-1)/2) % 2 == 0:
+            return calculate_legendre(p, a)
+        else:
+            return (-1)*calculate_legendre(p, a)
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()

+ 0 - 18
source-code/Pseudocode/Cholesky-Zerlegung/CholeskyZerlegung.py

@@ -1,18 +0,0 @@
-def getL(A):
-    n = len(A)
-    L = [[0 for i in range(n)] for j in range(n)]
-    print(L)
-    print("")
- 
-    for k in range(n):
-        L[k][k] = (A[k][k] - sum([L[k][i]**2 for i in range(k)]))**0.5
-        for i in range(k+1, n):
-            L[i][k] = (A[i][k] 
-                        - sum([L[i][j]*L[k][j] for j in range(k)])) \
-                      / L[k][k]
-            print("L_%i%i = A%i%i - sum(L_...)/L_%i%i) = %i" % (i, k, i, k, k, k, L[i][k]))
-    return L
-
-A = [[1,2,3],[2,8,14],[3,14,34]]
-
-print getL(A)

+ 22 - 0
source-code/Pseudocode/Cholesky-Zerlegung/cholesky_zerlegung.py

@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def get_l(A):
+    n = len(A)
+    L = [[0 for i in range(n)] for j in range(n)]
+    print(L)
+    print("")
+
+    for k in range(n):
+        L[k][k] = (A[k][k] - sum([L[k][i]**2 for i in range(k)]))**0.5
+        for i in range(k+1, n):
+            L[i][k] = ((A[i][k] - sum([L[i][j]*L[k][j] for j in range(k)]))
+                       / L[k][k])
+            print("L_%i%i = A%i%i - sum(L_...)/L_%i%i) = %i" %
+                  (i, k, i, k, k, k, L[i][k]))
+    return L
+
+A = [[1, 2, 3], [2, 8, 14], [3, 14, 34]]
+
+print(get_l(A))

+ 18 - 3
source-code/Pseudocode/Euklidischer-Algorithmus/basiswechsel.py

@@ -1,7 +1,22 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+
 def euklid(b, Z):
+    """
+    Euclids algorithm to change the basis.
+
+    Returns
+    -------
+    dict
+        A dictionary mapping the i-th position of the new number to its value,
+        where higher numbers are more significant.
+
+    Examples
+    --------
+    >>> euklid(3, 5)
+    {1: 1, 0: 2}
+    """
     p = 0
     while b**p <= Z:
         p = p+1
@@ -9,14 +24,14 @@ def euklid(b, Z):
 
     y = {}
     while Z != 0 and i > -5:
-        y[i] = Z  // b**i
+        y[i] = Z // b**i
         R = Z % b**i
         Z = R
-        i = i -1
+        i = i - 1
     return y
 
 if __name__ == "__main__":
     r = euklid(16, 15741.233)
     print("Result:")
-    for key in sorted(r.iterkeys(),reverse=True):
+    for key in sorted(r.iterkeys(), reverse=True):
         print "%s: %s" % (key, r[key])