Skip to content

Commit

Permalink
'Refactored by Sourcery'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourcery AI committed Nov 27, 2023
1 parent bb7e804 commit 0b15853
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 120 deletions.
2 changes: 1 addition & 1 deletion FormalDerivative.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def FormalDerivative(Fx):
return [(x[0] * x[1], x[1] - 1) for x in Fx if (x[1]-1) > -1]
return [(x[0] * x[1], x[1] - 1) for x in Fx if x[1] > 0]


def polyrep(Fx):
Expand Down
10 changes: 2 additions & 8 deletions binGCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ def gcd(a, b):
if b == 0:
return a
if a & 1 == 1:
if b & 1 == 0:
return gcd(a, b >> 1)
else:
return gcd(abs(a - b), min(a, b))
return gcd(a, b >> 1) if b & 1 == 0 else gcd(abs(a - b), min(a, b))
else:
if b & 1 == 1:
return gcd(a >> 1, b)
else:
return 2 * gcd(a >> 1, b >> 1)
return gcd(a >> 1, b) if b & 1 == 1 else 2 * gcd(a >> 1, b >> 1)


def test():
Expand Down
5 changes: 1 addition & 4 deletions dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ def Fk(signal, k):

def nyquist_norm(histogram):
N = len(histogram)
tmp = []
for n in range(0, N / 2):
tmp.append(histogram[n] * 2)
return tmp
return [histogram[n] * 2 for n in range(0, N / 2)]


print nyquist_norm(dft_slow(signal))
3 changes: 1 addition & 2 deletions dx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@


def __df(f, x, h):
r = (f(x + h / 2) - f(x - h / 2)) / h
return r
return (f(x + h / 2) - f(x - h / 2)) / h


def derivative(f):
Expand Down
10 changes: 3 additions & 7 deletions e.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@

def direct_e():
x = 6444429920 + 0.22 # or int(0x1801e3260,16) + 0.22
ret = (1 + (1.0 / x)) ** x
return ret
return (1 + (1.0 / x)) ** x


# taylor aproximation of e


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


# by limits definition:
Expand All @@ -49,7 +45,7 @@ def test():
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))
Expand Down
17 changes: 8 additions & 9 deletions fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
pi_i = (-2j) * complex(gmpy2.const_pi())

def fft(X):
if (N:=len(X)) == 1:
if (N:=len(X)) == 1:
return X
else:
N2 = N >> 1
E = fft(X[::2])
O = fft(X[1::2])
for k in range(0, N2):
q = exp((pi_i * k) / N ) * O[k]
X[k] = E[k] + q
X[k + N2] = E[k] - q
N2 = N >> 1
E = fft(X[::2])
O = fft(X[1::2])
for k in range(0, N2):
q = exp((pi_i * k) / N ) * O[k]
X[k] = E[k] + q
X[k + N2] = E[k] - q
return X
27 changes: 13 additions & 14 deletions legendre_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def factor(n0):
while n % i == 0:
n = n // i
factors.append(i)
if len(factors) == 0:
if not factors:
factors = [n]
print("factor(%d)=%s" % (n0, str(factors)))
return factors
Expand All @@ -42,7 +42,7 @@ def legendre_naive(p, q):
def legendre_prop0(p, q):
print("legendre_prop0(%d|%d)" % (p, q))
phi = (p - 1) * (q - 1)
_pow = pow(int(-1), int(phi // 4))
_pow = pow(-1, int(phi // 4))
return _pow * legendre_naive(q, p)


Expand Down Expand Up @@ -79,18 +79,17 @@ def _legendre(p, q):
ret = legendre_naive(q, p)
if p1 == 2:
ret = legendre_prop1(p1, q)
else:
if p1 > 2:
fp1 = factor(p1)[::-1]
tmp = 1
for f in fp1:
# while tmp2 > 1:
if f == 2:
tmp *= legendre_prop1(f, q)
else:
tmp *= _legendre(q, f)
# tmp = legendre_prop2(p1,q)
ret = tmp
elif p1 > 2:
fp1 = factor(p1)[::-1]
tmp = 1
for f in fp1:
# while tmp2 > 1:
if f == 2:
tmp *= legendre_prop1(f, q)
else:
tmp *= _legendre(q, f)
# tmp = legendre_prop2(p1,q)
ret = tmp
print("<legendre(%d|%d) r=%d -> %d" % (p, q, r, ret))
return ret

Expand Down
10 changes: 2 additions & 8 deletions math_rels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ def sgn(x):


def max(a, b):
if b > a:
return b
else:
return a
return max(b, a)


def min(a, b):
if b < a:
return b
else:
return a
return min(b, a)


def close(a, b, e):
Expand Down
8 changes: 2 additions & 6 deletions mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
# AM => GM => HM
# arithmetric mean
def AM(data):
accum = 0
for i in range(0, len(data) - 1):
accum += data[i]
accum = sum(data[i] for i in range(0, len(data) - 1))
return accum / len(data)


Expand All @@ -21,7 +19,5 @@ def GM(data):

# harmonic mean
def HM(data):
accum = 0
for i in range(0, len(data) - 1):
accum += 1 / data[i]
accum = sum(1 / data[i] for i in range(0, len(data) - 1))
return len(data) * (accum ** -1)
20 changes: 10 additions & 10 deletions newton_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def newtons_method(
epsilon, # Do not divide by a number smaller than this
max_iterations, # The maximum number of iterations to execute
):
for i in range(max_iterations):
y = f(x0)
yprime = f_prime(x0)
if abs(yprime) < epsilon: # Stop if the denominator is too small
break
x1 = x0 - y / yprime # Do Newton's computation
if abs(x1 - x0) <= tolerance: # Stop when the result is within the desired tolerance
return x1 # x1 is a solution within tolerance and maximum number of iterations
x0 = x1 # Update x0 to start the process again
return None
for _ in range(max_iterations):
y = f(x0)
yprime = f_prime(x0)
if abs(yprime) < epsilon: # Stop if the denominator is too small
break
x1 = x0 - y / yprime # Do Newton's computation
if abs(x1 - x0) <= tolerance: # Stop when the result is within the desired tolerance
return x1 # x1 is a solution within tolerance and maximum number of iterations
x0 = x1 # Update x0 to start the process again
return None
8 changes: 2 additions & 6 deletions polysolv.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def poly_synthetic_div(P, Q):
R = [0] * lP

for i in range(0, lP):
if i == 0:
R[i] = P[i]
else:
R[i] = P[i] + Q * R[i - 1]
R[i] = P[i] if i == 0 else P[i] + Q * R[i - 1]
return R


Expand All @@ -57,8 +54,7 @@ def get_rationals(poly, grade):
tmp.append(div)
tmp2 = []
for r in set(sorted(tmp)):
tmp2.append(r)
tmp2.append(-r)
tmp2.extend((r, -r))
return tmp2


Expand Down
6 changes: 1 addition & 5 deletions ramanujan_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
ipi2 = (2j) * complex(gmpy2.const_pi())

def c(q,n):
sum_ = 0
nipi2 = ipi2 * n
for a in range(1, q+1):
if gcd(a,q) == 1:
sum_ += exp(nipi2 * (a/q))
return sum_
return sum(exp(nipi2 * (a/q)) for a in range(1, q+1) if gcd(a,q) == 1)

print([c(1,n).real for n in range(1, 31)])
2 changes: 1 addition & 1 deletion redc.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test():
from functools import reduce
p = 2
TMP=[]
for i in range(1,210):
for _ in range(1,210):
p = int(gmpy2.next_prime(p))
TMP.append(MontgomeryReducer(p,gmpy=gmpy2))
C = reduce(mul,TMP)
Expand Down
14 changes: 4 additions & 10 deletions ruffini.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

# tells if a n is prime or not
def isPrime(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False

return True
return all(n % i != 0 for i in range(2, int(n ** 0.5) + 1))


# finds the prime factors of n
Expand All @@ -29,16 +25,14 @@ def factors(n):
if isPrime(num):
factor.append(num)
break
factor = sorted(factor)
return factor
return sorted(factor)


# add the negative factors to try
def addnegatives(D):
nD = []
for i in D:
nD.append(-abs(i))
nD.append(abs(i))
nD.extend((-abs(i), abs(i)))
return nD


Expand All @@ -52,7 +46,7 @@ def ruffini_step(P, D):
tmpPoli.append(P[j])
else:
tmpPoli.append(P[j] + (D[i] * tmpPoli[j - 1]))
if tmpPoli[len(tmpPoli) - 1] == 0:
if tmpPoli[-1] == 0:
return [D[i], tmpPoli]


Expand Down
2 changes: 1 addition & 1 deletion seqmult.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def SeqMult(s):
if l % 2 != 0:
s += [1]
l += 1
s = list(map(mul, s[0 : l // 2], s[l // 2 :]))
s = list(map(mul, s[:l // 2], s[l // 2 :]))
l = len(s)
return s[0]

Expand Down
14 changes: 2 additions & 12 deletions sieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@


def sieve_Erathostenes(n):
sieves = []
primes = []

# initialize
for l in range(0, n):
sieves.append(True)

sieves = [True for _ in range(0, n)]
# sieving
for i in range(2, int(math.sqrt(n))):
if sieves[i] == True:
for j in range(0, n):
h = (i ** 2) + (j * i)
if h < n:
sieves[h] = False
# filtering
for k in range(2, n):
if sieves[k] == True:
primes.append(k)
return primes
return [k for k in range(2, n) if sieves[k] == True]


print sieve_Erathostenes(121)
10 changes: 2 additions & 8 deletions sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@

# absolute value of x
def _abs(x):
if x >= 0:
return x
else:
return -x
return x if x >= 0 else -x


# sign of x returns: [-1,1]
def sign(x):
if x == 0:
return 0
else:
return x / _abs(x)
return 0 if x == 0 else x / _abs(x)


def almostEqual(a, b, epsilon):
Expand Down
6 changes: 2 additions & 4 deletions test_inv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ def compute_modinv_1_n(n, p):
https://codeforces.com/blog/entry/83075
"""
inv = [0, 1]
for i in range(2, n):
inv.append((p - p // i) * inv[p % i] % p)
inv.extend((p - p // i) * inv[p % i] % p for i in range(2, n))
return inv

@timing
def compute_modinv_gmpy_1_n(n, p):
inv = [0]
for i in range(1, n):
inv.append(int(invert(i, p)))
inv.extend(int(invert(i, p)) for i in range(1, n))
return inv


Expand Down
5 changes: 1 addition & 4 deletions vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ def normalize(vec):


def magnitude(vec):
res = 0
for n in range(0, len(vec)):
res += vec[n] * vec[n]

res = sum(vec[n] * vec[n] for n in range(0, len(vec)))
return math.sqrt(res)


Expand Down

0 comments on commit 0b15853

Please sign in to comment.