Godtone (talk | contribs)
Godtone (talk | contribs)
My Python 3 code: FIX CODE!!! (and add new features)
Line 782: Line 782:
if complements:
if complements:
ivs |= set([ iv(i[1]*2,i[0]) for i in ivs ])
ivs |= set([ iv(i[1]*2,i[0]) for i in ivs ])
return ivs
return sorted(ivs,key=lambda x: x[1]/x[0])


def subgroup(ivs):
def subgroup(ivs):
Line 1,292: Line 1,292:
patent_vals = dict()
patent_vals = dict()
ivs_cache = []
ivs_cache = []
ivs_int_cache = 0
ivs_sum_weights_cache = 1
ivs_sum_weights_cache = 1
# the default badness is the sum of the squares of the errors with each interval's contribution weighted proportional to its odd-limit complexity,
# the default badness is the sum of the squares of the errors with each interval's contribution weighted proportional to its odd-limit complexity,
Line 1,305: Line 1,306:
elif weighting==2 or weighting in ['natural', 'harmonic', 'TE', 'te', 'tenney']:
elif weighting==2 or weighting in ['natural', 'harmonic', 'TE', 'te', 'tenney']:
weighting = lambda x: 1/math.log2(x[0] * x[1]) # mainly for use with prime harmonic interval sets
weighting = lambda x: 1/math.log2(x[0] * x[1]) # mainly for use with prime harmonic interval sets
global ivs_cache
global ivs_int_cache
global ivs_sum_weights_cache
# convert the ivs argument into a valid set of intervals before attempting operations with it
# convert the ivs argument into a valid set of intervals before attempting operations with it
if type(ivs)==int:
if type(ivs)==int:
if ivs%1: # use odd-limit
if ivs==ivs_int_cache: # use cached value to avoid regenerating an entire odd-limit every time
ivs = odd_lim(ivs)
ivs = ivs_cache
else: # use integer-limit
else:
ivs = [x for x in odd_lim(ivs) if x[0] <= ivs]
ivs_int_cache = ivs
elif type(ivs)==list or type(ivs)==set:
if ivs % 2: # use odd-limit if odd
ivs = odd_lim(ivs_int_cache)
else: # use integer-limit if even
ivs = [x for x in odd_lim(ivs_int_cache) if x[0] <= ivs_int_cache]
ivs_cache = ivs
elif type(ivs)==list and type(ivs[0])==int or type(ivs)==set: # specify odd-limit in terms of list/set of odds
ivs = odd_lim(1,[],ivs)
ivs = odd_lim(1,[],ivs)
# else: it's assumed we have a list or set of 2-tuples of integers (a list or set of intervals)
# deduce if using the default behaviour (mean average of sum of badnesss) and if so note it for later
# deduce if using the default behaviour (mean average of sum of badnesss) and if so note it for later
using_avg = False
using_avg = False
global ivs_cache
ivcount = combine
global ivs_sum_weights_cache
if combine in ['avg','mean','average','sum','net']: # (note the mean average is equal to a sum up to a scalar factor)
if combine in ['avg','mean','average','sum','net']: # (note the mean average is equal to a sum up to a scalar factor)
using_avg = combine not in ['sum','net'] # if a pure sum is requested, do not divide by the sum of the weights
using_avg = combine not in ['sum','net'] # if a pure sum is requested, do not divide by the sum of the weights
Line 1,328: Line 1,338:
ivs_cache = ivs
ivs_cache = ivs
ivs_sum_weights_cache = sum([ weighting(x) for x in ivs ])
ivs_sum_weights_cache = sum([ weighting(x) for x in ivs ])
elif combine in ['max','maximum'] or combine==1: # average of top 1 highest badnesses
elif combine in ['max','maximum'] or ivcount==1: # average of top 1 highest badnesses
combine = lambda badnesses: max(badnesses)
combine = lambda badnesses: max(badnesses)
elif combine in ['min','minimum'] or combine==-1: # average of top 1 lowest badnesses
elif combine in ['min','minimum'] or ivcount==-1: # average of top 1 lowest badnesses
combine = lambda badnesses: min(badnesses)
combine = lambda badnesses: min(badnesses)
elif combine > 0: # (unweighted) average of top <combine> (highest) badnesses
elif ivcount > 0: # (unweighted) average of <combine> highest badnesses AKA "judge by worst <ivcount>"
combine = lambda badnesses: sum( sorted(badnesses)[-combine:] )/combine
combine = lambda badnesses: sum( sorted(badnesses)[-ivcount:] )/ivcount
elif combine < 0: # (unweighted) average of bottom <combine> (lowest) badnesses
elif ivcount < 0: # (unweighted) average of <combine> lowest badnesses AKA "judge by best -<ivcount>"
combine = lambda badnesses: sum( sorted(badnesses)[:-combine] )/-combine
combine = lambda badnesses: sum( sorted(badnesses)[:-ivcount] )/-ivcount
# use the cached val if it is present; otherwise, create a 251-prime-limit (255-odd-limit) patent val and cache it.
# use the cached val if it is present; otherwise, create a 251-prime-limit (255-odd-limit) patent val and cache it.
# alternatively, feed the real number of divisions of the octave that generates your val for each val you want to compare.   
# alternatively, feed the real number of divisions of the octave that generates your val for each val you want to compare.   
Line 1,367: Line 1,377:
def optimal_edo_sequence(ivs_or_edo_badness,edo_set=range(2,311+1),weighting=lambda x: iv_complexity(x),combine='avg'):
def optimal_edo_sequence(ivs_or_edo_badness,edo_set=range(2,311+1),weighting=lambda x: iv_complexity(x),combine='avg'):
et_badness_judger = ivs_or_edo_badness
et_badness_judger = ivs_or_edo_badness
if type(ivs_or_edo_badness) in [int,list,set]: # user gave intervals (default et_badness)
if type(ivs_or_edo_badness) in [int,set,list]: # user gave intervals (default et_badness)
ivs = ivs_or_edo_badness
ivs = ivs_or_edo_badness
et_badness_judger = lambda edo: et_badness(ivs,edo,lambda rel_err: rel_err**2,weighting,combine)
et_badness_judger = lambda edo: et_badness(ivs,edo,weighting=weighting,combine=combine)
# else user gave et_badness manually (custom)
# else user gave et_badness manually (custom)
best_edo = et_badness_judger(1)
best_edo = et_badness_judger(1)
Line 1,383: Line 1,393:
# this gives much sparser but also much more interesting lists.
# this gives much sparser but also much more interesting lists.
# default weighting of an interval x is proportional to its odd-limit complexity iv_complexity(x).
# default weighting of an interval x is proportional to its odd-limit complexity iv_complexity(x).
def strict_optimal_edo_sequence(ivs,edo_set=range(2,311+2),weighting=lambda x: iv_complexity(x),combine='avg'):
def strict_optimal_edo_sequence(ivs,edo_set=range(2,311+1),weighting=lambda x: iv_complexity(x),combine='avg'):
return optimal_edo_sequence(lambda edo: et_badness(ivs,edo,lambda rel_err,x,et2: rel_err**2,weighting,combine),edo_set)
return optimal_edo_sequence(lambda edo: et_badness(ivs,edo,lambda rel_err: rel_err**2,weighting,combine),edo_set)
 
</syntaxhighlight>
</syntaxhighlight>