User:2^67-1/Sandbox
Jump to navigation
Jump to search
A program to find increasingly better EDns in a given subgroup with a given equave ('increasingly better' is given by decreasing relative error)
import math
def fracmult(a,b):
g = math.gcd(a[0]*b[0],a[1]*b[1])
return [a[0]*b[0]//g,a[1]*b[1]//g]
def reciprocal(a):
return [a[1],a[0]]
def equavereduce(a,b):
while b[0]*a[1] > a[0]*b[1]:
b = fracmult(b,reciprocal(a))
while b[0] < b[1]:
b = fracmult(b,a)
return b
def tonalitydiamond(a,b,c):
return [equavereduce(a,b),equavereduce(a,reciprocal(b)),equavereduce(a,c),equavereduce(a,reciprocal(c)),equavereduce(a,fracmult(b,reciprocal(c))),equavereduce(a,fracmult(c,reciprocal(b)))]
def cents(a):
return 1200*math.log2(a[0]/a[1])
def centslist(a):
c = []
for i in a:
c.append(cents(i))
return c
def ednerror(a,e,equave):
step = cents(equave)/e
approx = round(a/step,0)*step
error = math.fabs(a-approx)
return error
def ednerrorlist(a,e,equave):
error = []
for i in a:
error.append(ednerror(cents(i),e,equave))
return error
def euc(a):
total = 0
for i in a:
total = total + i**2
return total**(1/2)
def avg(a):
total = 0
for i in a:
total = total + i
return total/len(a)
best = 10**10
for i in range(1, 100000):
if avg(ednerrorlist(tonalitydiamond([3,2],[5,2],[7,4]),i,[3,2]))*i < best:
best = avg(ednerrorlist(tonalitydiamond([3,2],[5,2],[7,4]),i,[3,2]))*i
print(i)