Skip to content

Commit

Permalink
2to3, blackify code...
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus committed Jan 8, 2024
1 parent 5b7483f commit 1fd13f1
Show file tree
Hide file tree
Showing 53 changed files with 444 additions and 382 deletions.
1 change: 1 addition & 0 deletions DoubleAndAdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def bits(n):
yield n & 1
n >>= 1


def double_and_add(n, x):
"""
Returns the result of n * x, computed using
Expand Down
13 changes: 7 additions & 6 deletions FormalDerivative.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
def FormalDerivative(Fx):
return [(x[0] * x[1], x[1] - 1) for x in Fx if x[1] > 0]
return [(x[0] * x[1], x[1] - 1) for x in Fx if x[1] > 0]


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

fx = [(-1,6),(1,0)]
print(polyrep(fx))

fx = [(-1, 6), (1, 0)]
print((polyrep(fx)))
fx = FormalDerivative(fx)
print(polyrep(fx))
print((polyrep(fx)))
39 changes: 20 additions & 19 deletions GaussGF2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
# Author Dario Clavijo 2021
# based on https://www.cs.umd.edu/~gasarch/TOPICS/factoring/fastgauss.pdf, page2


def Gauss_GF2(A):
h = len(A)
m = len(A[0])
marks = [False] * h
for j in range(0,m):
for i in range(0,h):
if A[i][j] == 1:
marks[i] = True
for k in range(j+1,m):
if A[i][k] == 1:
A[i][k] = (A[i][j] ^ A[i][k])
break
return marks, A

if __name__ == "__main__":
A = [[1,1,0,0],[1,1,0,1],[0,1,1,1],[0,0,1,0],[0,0,0,1]]
marks,A = Gauss_GF2(A)
print(marks)
for row in A:
print(row)
h = len(A)
m = len(A[0])
marks = [False] * h
for j in range(0, m):
for i in range(0, h):
if A[i][j] == 1:
marks[i] = True
for k in range(j + 1, m):
if A[i][k] == 1:
A[i][k] = A[i][j] ^ A[i][k]
break
return marks, A


if __name__ == "__main__":
A = [[1, 1, 0, 0], [1, 1, 0, 1], [0, 1, 1, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
marks, A = Gauss_GF2(A)
print(marks)
for row in A:
print(row)
16 changes: 8 additions & 8 deletions anomaly_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def test():
MAD anomaly detection: [(5, 12, 3.8333333333333344)]
"""
S = [2, 3, 5, 2, 3, 12, 5, 3, 4]
print("Series:", S)
print("Mean:", mean(S))
print("StdDev:", stddev(S))
print("Bounds:", bounds(S))
print("simple anomaly detection:", list(simple_anonaly_detection(S)))
print("zvalues:", list(zvalue(S)))
print("zvalue anomaly detection:", list(zvalue_anomaly_detection(S)))
print("MAD anomaly detection:", list(MAD_anomaly_detection(S)))
print(("Series:", S))
print(("Mean:", mean(S)))
print(("StdDev:", stddev(S)))
print(("Bounds:", bounds(S)))
print(("simple anomaly detection:", list(simple_anonaly_detection(S))))
print(("zvalues:", list(zvalue(S))))
print(("zvalue anomaly detection:", list(zvalue_anomaly_detection(S))))
print(("MAD anomaly detection:", list(MAD_anomaly_detection(S))))


if __name__ == "__main__":
Expand Down
16 changes: 8 additions & 8 deletions archimedes_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def approximation(sides):
pi_guess = (inside + outside) / 2
accuracy = (1 - (pi_guess - pi) / pi) * 100

print "sides:", sides
print "angle:", angle
print "inside:", inside
print "outside:", outside,
print "pi_guess:", pi_guess
print "accuracy:", accuracy
print("sides:", sides)
print("angle:", angle)
print("inside:", inside)
print("outside:", outside, end=" ")
print("pi_guess:", pi_guess)
print("accuracy:", accuracy)


for i in xrange(10):
approximation(10 ** i)
for i in range(10):
approximation(10**i)
27 changes: 14 additions & 13 deletions barret.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@

class BarretReduccer():
class BarretReduccer:
"""
https://en.wikipedia.org/wiki/Barrett_reduction
"""

def __init__(self, m):
if m <=0: raise ValueError("Modulus must be positive")
if m & (m - 1) == 0: raise ValueError("Modulus must not be a power of 2")
if m <= 0:
raise ValueError("Modulus must be positive")
if m & (m - 1) == 0:
raise ValueError("Modulus must not be a power of 2")
self.m = m
self.shift = m.bit_length() << 1
self.q = (1 << self.shift) // m

def reduce(self, a):
#if 0 <= a <= self.m**2: raise ValueError("a must be >0 and < n^2")
# if 0 <= a <= self.m**2: raise ValueError("a must be >0 and < n^2")
a -= ((a * self.q) >> self.shift) * self.m
if a >= self.m: a -= self.m
if a >= self.m:
a -= self.m
return a

def test():
br = BarretReduccer(12)
print(br.reduce(6))
print(br.reduce(13))
print(br.reduce(18))



def test():
br = BarretReduccer(12)
print((br.reduce(6)))
print((br.reduce(13)))
print((br.reduce(18)))
8 changes: 4 additions & 4 deletions binGCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def gcd(a, b):


def test():
print(gcd(115, 5))
print(gcd(5, 7))
print(gcd(10, 4))
print((gcd(115, 5)))
print((gcd(5, 7)))
print((gcd(10, 4)))

a = (
37975227936943673922808872755445627854565536638199
Expand All @@ -30,7 +30,7 @@ def test():
40094690950920881030683735292761468389214899724061
* 5846418214406154678836553182979162384198610505601062333
)
print(gcd(a, b))
print((gcd(a, b)))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion binary_polinomial_factoring.sage
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test():
ff += 1
else:
nf += 1
print(n, i, ff, nf, f)
print((n, i, ff, nf, f))
n += 1


Expand Down
18 changes: 9 additions & 9 deletions birthdayparadox.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def bpp(g,w):
"""
Compute the probability of all elements in group g in the whole set w to have unique birthdays.
"""
proba = 1
for i in range(w, w - g, -1):
proba *= (i/w)
return proba
def bpp(g, w):
"""
Compute the probability of all elements in group g in the whole set w to have unique birthdays.
"""
proba = 1
for i in range(w, w - g, -1):
proba *= i / w
return proba


print(bpp(23,365))
print((bpp(23, 365)))
34 changes: 19 additions & 15 deletions chirp_zeta_transform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Given the following paper: https://www.nature.com/articles/s41598-019-50234-9

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

def CZT(x,M,W,A):

def CZT(x, M, W, A):
N = len(x)
X, r , c = [] * N, [] * N, [] * M
X, r, c = [] * N, [] * N, [] * M
for k in range(0, N - 1):
k2 = k * k
X[k] = W[((k2) >> 1)] * A[-k] * x[k]
r[k] = W[-((k2) >> 1)]
for k in range(0,M-1):
for k in range(0, M - 1):
c[k] = W[-((k * k) >> 1)]
X = ToeplitzMultiplyE(r,c,X)
X = ToeplitzMultiplyE(r, c, X)
for k in range(0, M - 1):
X[k] = W[((k * k) >> 1)] * X[k]
return X

def ICZT(X,N,W,A):

def ICZT(X, N, W, A):
M = len(X)
if M != N:
raise("M must == to N")
raise ("M must == to N")
n = N
x, p, u, z = [] * n, [] * n, [] * n, [] * n
for k in range(0, n - 1):
x[k] = W[((k * k) >> 1)] * X[k]
p[0] = 1
for k in range(0,n-1):
for k in range(0, n - 1):
p[k] = (p[k - 1]) * (W[k] - 1)
for k in range(0, n-1, 2):
u[k] = ((W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k]))
for k in range(1, n-1, 2):
u[k] = ((W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k])) * -1
for j = range(n-1,0,-1):
for k in range(0, n - 1, 2):
u[k] = (W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (
p[n - k - 1] * p[k]
)
for k in range(1, n - 1, 2):
u[k] = (
(W[((k * k) << 1) - ((n << 1) - 1) + n * (n - 1)]) / (p[n - k - 1] * p[k])
) * -1
for j in range(n - 1, 0, -1):
u1[k] = u[k - j]
u2 = u[0] + [0] * (n - 1)
x1 = ToeplitzMultiplyE(u1, z, x)
Expand All @@ -54,5 +60,3 @@ def ICZT(X,N,W,A):
for k in range(0, n - 1):
x[k] = A[k] * W[-((k * k) >> 1)] * x[k]
return x


4 changes: 2 additions & 2 deletions contfrac.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def cont_frac(a, b):
return coef


print(cont_frac(649, 200))
print(cont_frac(17993, 90581))
print((cont_frac(649, 200)))
print((cont_frac(17993, 90581)))
6 changes: 3 additions & 3 deletions dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def dft_slow(signal):

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

histogram = []
for k in xrange(0, N):
for k in range(0, N):
histogram.append(Fk(signal, k))
return histogram

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


print nyquist_norm(dft_slow(signal))
print((nyquist_norm(dft_slow(signal))))
8 changes: 4 additions & 4 deletions dx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def dx(x):


def g(x):
return x ** 2
return x**2


dg = derivative(g)
Expand All @@ -42,10 +42,10 @@ def intf(b, a=0):
return intf


print "g(x)=", [g(x) for x in range(11)]
print(("g(x)=", [g(x) for x in range(11)]))

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

inv1 = integral(derivative(g))

print "integral(df g(x))=", [inv1(x) for x in range(11)]
print(("integral(df g(x))=", [inv1(x) for x in range(11)]))
28 changes: 15 additions & 13 deletions e.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def direct_e():


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


# by limits definition:
Expand All @@ -30,29 +30,31 @@ def taylor_e():
def lim_ddx_e(x):
h = 1.0 / (534645555)
return (
(math.e ** x * math.e ** h) - math.e ** x
(math.e**x * math.e**h) - math.e**x
) / h # = math.e * ((math.e ** h - 1.0)/h)


def test():
print(math.e)
print(direct_e())
print(taylor_e())
print(lim_ddx_e(1))
print((math.e))
print((direct_e()))
print((taylor_e()))
print((lim_ddx_e(1)))


i = complex(0,1)
i = complex(0, 1)
pi = math.pi


def exp(n, precision=100):
return sum((n ** x) / math.factorial(x) for x in range(0, precision))
return sum((n**x) / math.factorial(x) for x in range(0, precision))


def test_exp():
print(exp(0))
print(exp(0.5))
print(exp(1))
print(exp(pi))
print(exp(i*pi))
print((exp(0)))
print((exp(0.5)))
print((exp(1)))
print((exp(pi)))
print((exp(i * pi)))


test_exp()
Loading

0 comments on commit 1fd13f1

Please sign in to comment.