User:2^67-1/Sandbox: Difference between revisions
Jump to navigation
Jump to search
add less complex approximations to the JIP for completeness and out of curiosity. at least some of these are interesting, for various reasons. |
mNo edit summary |
||
| (6 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
A program to find increasingly better EDns in a given subgroup with a given equave ('increasingly better' is given by decreasing relative error) | |||
<syntaxhighlight lang="python"> | |||
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) | |||
</syntaxhighlight> | |||
== More programs == | |||
<syntaxhighlight lang="python"> | |||
import math | |||
def relativeerror(x): | |||
three = abs(round(x*math.log2(3),0)-x*math.log2(3)) | |||
five = abs(round(x*math.log2(5),0)-x*math.log2(5)) | |||
eleven = abs(round(x*math.log2(11),0)-x*math.log2(11)) | |||
return max(three, five, eleven) | |||
best = 3 | |||
for i in range(1,1000000): | |||
if relativeerror(i) < best: | |||
print(i) | |||
best = relativeerror(i) | |||
</syntaxhighlight> | |||
Output: | |||
<syntaxhighlight lang="python"> | |||
1 | |||
2 | |||
4 | |||
7 | |||
15 | |||
22 | |||
87 | |||
183 | |||
270 | |||
494 | |||
1171 | |||
11105 | |||
42348 | |||
54624 | |||
282560 | |||
</syntaxhighlight> | |||
Latest revision as of 10:59, 18 September 2026
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)
More programs
import math
def relativeerror(x):
three = abs(round(x*math.log2(3),0)-x*math.log2(3))
five = abs(round(x*math.log2(5),0)-x*math.log2(5))
eleven = abs(round(x*math.log2(11),0)-x*math.log2(11))
return max(three, five, eleven)
best = 3
for i in range(1,1000000):
if relativeerror(i) < best:
print(i)
best = relativeerror(i)
Output:
1
2
4
7
15
22
87
183
270
494
1171
11105
42348
54624
282560