Skip to content

Commit 1fd13f1

Browse files
committed
2to3, blackify code...
1 parent 5b7483f commit 1fd13f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+444
-382
lines changed

DoubleAndAdd.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def bits(n):
99
yield n & 1
1010
n >>= 1
1111

12+
1213
def double_and_add(n, x):
1314
"""
1415
Returns the result of n * x, computed using

FormalDerivative.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
def FormalDerivative(Fx):
2-
return [(x[0] * x[1], x[1] - 1) for x in Fx if x[1] > 0]
2+
return [(x[0] * x[1], x[1] - 1) for x in Fx if x[1] > 0]
33

44

55
def polyrep(Fx):
6-
s = ["%dx ^ %d" % x for x in Fx]
7-
return " + ".join(s)
6+
s = ["%dx ^ %d" % x for x in Fx]
7+
return " + ".join(s)
88

9-
fx = [(-1,6),(1,0)]
10-
print(polyrep(fx))
9+
10+
fx = [(-1, 6), (1, 0)]
11+
print((polyrep(fx)))
1112
fx = FormalDerivative(fx)
12-
print(polyrep(fx))
13+
print((polyrep(fx)))

GaussGF2.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
# Author Dario Clavijo 2021
33
# based on https://www.cs.umd.edu/~gasarch/TOPICS/factoring/fastgauss.pdf, page2
44

5+
56
def Gauss_GF2(A):
6-
h = len(A)
7-
m = len(A[0])
8-
marks = [False] * h
9-
for j in range(0,m):
10-
for i in range(0,h):
11-
if A[i][j] == 1:
12-
marks[i] = True
13-
for k in range(j+1,m):
14-
if A[i][k] == 1:
15-
A[i][k] = (A[i][j] ^ A[i][k])
16-
break
17-
return marks, A
18-
19-
if __name__ == "__main__":
20-
A = [[1,1,0,0],[1,1,0,1],[0,1,1,1],[0,0,1,0],[0,0,0,1]]
21-
marks,A = Gauss_GF2(A)
22-
print(marks)
23-
for row in A:
24-
print(row)
7+
h = len(A)
8+
m = len(A[0])
9+
marks = [False] * h
10+
for j in range(0, m):
11+
for i in range(0, h):
12+
if A[i][j] == 1:
13+
marks[i] = True
14+
for k in range(j + 1, m):
15+
if A[i][k] == 1:
16+
A[i][k] = A[i][j] ^ A[i][k]
17+
break
18+
return marks, A
2519

20+
21+
if __name__ == "__main__":
22+
A = [[1, 1, 0, 0], [1, 1, 0, 1], [0, 1, 1, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
23+
marks, A = Gauss_GF2(A)
24+
print(marks)
25+
for row in A:
26+
print(row)

anomaly_detection.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ def test():
107107
MAD anomaly detection: [(5, 12, 3.8333333333333344)]
108108
"""
109109
S = [2, 3, 5, 2, 3, 12, 5, 3, 4]
110-
print("Series:", S)
111-
print("Mean:", mean(S))
112-
print("StdDev:", stddev(S))
113-
print("Bounds:", bounds(S))
114-
print("simple anomaly detection:", list(simple_anonaly_detection(S)))
115-
print("zvalues:", list(zvalue(S)))
116-
print("zvalue anomaly detection:", list(zvalue_anomaly_detection(S)))
117-
print("MAD anomaly detection:", list(MAD_anomaly_detection(S)))
110+
print(("Series:", S))
111+
print(("Mean:", mean(S)))
112+
print(("StdDev:", stddev(S)))
113+
print(("Bounds:", bounds(S)))
114+
print(("simple anomaly detection:", list(simple_anonaly_detection(S))))
115+
print(("zvalues:", list(zvalue(S))))
116+
print(("zvalue anomaly detection:", list(zvalue_anomaly_detection(S))))
117+
print(("MAD anomaly detection:", list(MAD_anomaly_detection(S))))
118118

119119

120120
if __name__ == "__main__":

archimedes_pi.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def approximation(sides):
1414
pi_guess = (inside + outside) / 2
1515
accuracy = (1 - (pi_guess - pi) / pi) * 100
1616

17-
print "sides:", sides
18-
print "angle:", angle
19-
print "inside:", inside
20-
print "outside:", outside,
21-
print "pi_guess:", pi_guess
22-
print "accuracy:", accuracy
17+
print("sides:", sides)
18+
print("angle:", angle)
19+
print("inside:", inside)
20+
print("outside:", outside, end=" ")
21+
print("pi_guess:", pi_guess)
22+
print("accuracy:", accuracy)
2323

2424

25-
for i in xrange(10):
26-
approximation(10 ** i)
25+
for i in range(10):
26+
approximation(10**i)

barret.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
2-
class BarretReduccer():
1+
class BarretReduccer:
32
"""
43
https://en.wikipedia.org/wiki/Barrett_reduction
54
"""
5+
66
def __init__(self, m):
7-
if m <=0: raise ValueError("Modulus must be positive")
8-
if m & (m - 1) == 0: raise ValueError("Modulus must not be a power of 2")
7+
if m <= 0:
8+
raise ValueError("Modulus must be positive")
9+
if m & (m - 1) == 0:
10+
raise ValueError("Modulus must not be a power of 2")
911
self.m = m
1012
self.shift = m.bit_length() << 1
1113
self.q = (1 << self.shift) // m
1214

1315
def reduce(self, a):
14-
#if 0 <= a <= self.m**2: raise ValueError("a must be >0 and < n^2")
16+
# if 0 <= a <= self.m**2: raise ValueError("a must be >0 and < n^2")
1517
a -= ((a * self.q) >> self.shift) * self.m
16-
if a >= self.m: a -= self.m
18+
if a >= self.m:
19+
a -= self.m
1720
return a
1821

19-
def test():
20-
br = BarretReduccer(12)
21-
print(br.reduce(6))
22-
print(br.reduce(13))
23-
print(br.reduce(18))
2422

25-
26-
23+
def test():
24+
br = BarretReduccer(12)
25+
print((br.reduce(6)))
26+
print((br.reduce(13)))
27+
print((br.reduce(18)))

binGCD.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def gcd(a, b):
1818

1919

2020
def test():
21-
print(gcd(115, 5))
22-
print(gcd(5, 7))
23-
print(gcd(10, 4))
21+
print((gcd(115, 5)))
22+
print((gcd(5, 7)))
23+
print((gcd(10, 4)))
2424

2525
a = (
2626
37975227936943673922808872755445627854565536638199
@@ -30,7 +30,7 @@ def test():
3030
40094690950920881030683735292761468389214899724061
3131
* 5846418214406154678836553182979162384198610505601062333
3232
)
33-
print(gcd(a, b))
33+
print((gcd(a, b)))
3434

3535

3636
if __name__ == "__main__":

binary_polinomial_factoring.sage

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test():
6464
ff += 1
6565
else:
6666
nf += 1
67-
print(n, i, ff, nf, f)
67+
print((n, i, ff, nf, f))
6868
n += 1
6969

7070

birthdayparadox.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
def bpp(g,w):
2-
"""
3-
Compute the probability of all elements in group g in the whole set w to have unique birthdays.
4-
"""
5-
proba = 1
6-
for i in range(w, w - g, -1):
7-
proba *= (i/w)
8-
return proba
1+
def bpp(g, w):
2+
"""
3+
Compute the probability of all elements in group g in the whole set w to have unique birthdays.
4+
"""
5+
proba = 1
6+
for i in range(w, w - g, -1):
7+
proba *= i / w
8+
return proba
99

1010

11-
print(bpp(23,365))
11+
print((bpp(23, 365)))

chirp_zeta_transform.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Given the following paper: https://www.nature.com/articles/s41598-019-50234-9
22

3-
#TODO: Implimentand test functions
3+
# TODO: Implimentand test functions
44
# Found function implementation: ToeplitzMultiplyE
55
# Suplemental paper (at bottom of previous link):
66
# https://static-content.springer.com/esm/art%3A10.1038%2Fs41598-019-50234-9/MediaObjects/41598_2019_50234_MOESM1_ESM.pdf
@@ -13,36 +13,42 @@
1313
#
1414
# Depends on FFT and IFFT (import fftw)
1515

16-
def CZT(x,M,W,A):
16+
17+
def CZT(x, M, W, A):
1718
N = len(x)
18-
X, r , c = [] * N, [] * N, [] * M
19+
X, r, c = [] * N, [] * N, [] * M
1920
for k in range(0, N - 1):
2021
k2 = k * k
2122
X[k] = W[((k2) >> 1)] * A[-k] * x[k]
2223
r[k] = W[-((k2) >> 1)]
23-
for k in range(0,M-1):
24+
for k in range(0, M - 1):
2425
c[k] = W[-((k * k) >> 1)]
25-
X = ToeplitzMultiplyE(r,c,X)
26+
X = ToeplitzMultiplyE(r, c, X)
2627
for k in range(0, M - 1):
2728
X[k] = W[((k * k) >> 1)] * X[k]
2829
return X
2930

30-
def ICZT(X,N,W,A):
31+
32+
def ICZT(X, N, W, A):
3133
M = len(X)
3234
if M != N:
33-
raise("M must == to N")
35+
raise ("M must == to N")
3436
n = N
3537
x, p, u, z = [] * n, [] * n, [] * n, [] * n
3638
for k in range(0, n - 1):
3739
x[k] = W[((k * k) >> 1)] * X[k]
3840
p[0] = 1
39-
for k in range(0,n-1):
41+
for k in range(0, n - 1):
4042
p[k] = (p[k - 1]) * (W[k] - 1)
41-
for k in range(0, n-1, 2):
42-
u[k] = ((W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k]))
43-
for k in range(1, n-1, 2):
44-
u[k] = ((W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k])) * -1
45-
for j = range(n-1,0,-1):
43+
for k in range(0, n - 1, 2):
44+
u[k] = (W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (
45+
p[n - k - 1] * p[k]
46+
)
47+
for k in range(1, n - 1, 2):
48+
u[k] = (
49+
(W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k])
50+
) * -1
51+
for j in range(n - 1, 0, -1):
4652
u1[k] = u[k - j]
4753
u2 = u[0] + [0] * (n - 1)
4854
x1 = ToeplitzMultiplyE(u1, z, x)
@@ -54,5 +60,3 @@ def ICZT(X,N,W,A):
5460
for k in range(0, n - 1):
5561
x[k] = A[k] * W[-((k * k) >> 1)] * x[k]
5662
return x
57-
58-

contfrac.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def cont_frac(a, b):
1717
return coef
1818

1919

20-
print(cont_frac(649, 200))
21-
print(cont_frac(17993, 90581))
20+
print((cont_frac(649, 200)))
21+
print((cont_frac(17993, 90581)))

dft.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def dft_slow(signal):
1414

1515
def Fk(signal, k):
1616
Freq = 0.0
17-
for n in xrange(0, N):
17+
for n in range(0, N):
1818
coef = math.exp((-2 * math.pi * k * n) / N)
1919
Freq += signal[n] * coef
2020
return Freq
2121

2222
histogram = []
23-
for k in xrange(0, N):
23+
for k in range(0, N):
2424
histogram.append(Fk(signal, k))
2525
return histogram
2626

@@ -30,4 +30,4 @@ def nyquist_norm(histogram):
3030
return [histogram[n] * 2 for n in range(0, N / 2)]
3131

3232

33-
print nyquist_norm(dft_slow(signal))
33+
print((nyquist_norm(dft_slow(signal))))

dx.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def dx(x):
2020

2121

2222
def g(x):
23-
return x ** 2
23+
return x**2
2424

2525

2626
dg = derivative(g)
@@ -42,10 +42,10 @@ def intf(b, a=0):
4242
return intf
4343

4444

45-
print "g(x)=", [g(x) for x in range(11)]
45+
print(("g(x)=", [g(x) for x in range(11)]))
4646

47-
print "df g(x)=", [dg(x) for x in range(11)]
47+
print(("df g(x)=", [dg(x) for x in range(11)]))
4848

4949
inv1 = integral(derivative(g))
5050

51-
print "integral(df g(x))=", [inv1(x) for x in range(11)]
51+
print(("integral(df g(x))=", [inv1(x) for x in range(11)]))

e.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def direct_e():
1818

1919

2020
def taylor_e():
21-
return sum(1.0 / (math.factorial(n)) for n in xrange(0, 15))
21+
return sum(1.0 / (math.factorial(n)) for n in range(0, 15))
2222

2323

2424
# by limits definition:
@@ -30,29 +30,31 @@ def taylor_e():
3030
def lim_ddx_e(x):
3131
h = 1.0 / (534645555)
3232
return (
33-
(math.e ** x * math.e ** h) - math.e ** x
33+
(math.e**x * math.e**h) - math.e**x
3434
) / h # = math.e * ((math.e ** h - 1.0)/h)
3535

3636

3737
def test():
38-
print(math.e)
39-
print(direct_e())
40-
print(taylor_e())
41-
print(lim_ddx_e(1))
38+
print((math.e))
39+
print((direct_e()))
40+
print((taylor_e()))
41+
print((lim_ddx_e(1)))
4242

4343

44-
i = complex(0,1)
44+
i = complex(0, 1)
4545
pi = math.pi
4646

47+
4748
def exp(n, precision=100):
48-
return sum((n ** x) / math.factorial(x) for x in range(0, precision))
49+
return sum((n**x) / math.factorial(x) for x in range(0, precision))
50+
4951

5052
def test_exp():
51-
print(exp(0))
52-
print(exp(0.5))
53-
print(exp(1))
54-
print(exp(pi))
55-
print(exp(i*pi))
53+
print((exp(0)))
54+
print((exp(0.5)))
55+
print((exp(1)))
56+
print((exp(pi)))
57+
print((exp(i * pi)))
5658

5759

5860
test_exp()

0 commit comments

Comments
 (0)