User:Hkm/Fokker block code: Difference between revisions
Created page with "This code can be used to find Fokker blocks. <syntaxhighlight=python> import numpy as np from fractions import Fraction from itertools import combinations, product from math import gcd from functools import reduce from temper.lib_temper.temper import cokernel, defactored_hnf from temper.lib_temper.optimize import lstsq from temper.lib_temper.subgroup import p_limit from temper.lib_temper.interval import factors def prime_factors(n): factors = [] for d in range(..." |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This code can be used to find Fokker blocks. | This code can be used to find Fokker blocks. It requires the [https://github.com/Sin-tel/temper/tree/main/lib_temper lib_temper] library made by [[sintel]]. | ||
<syntaxhighlight=python> | <syntaxhighlight lang=python> | ||
import numpy as np | import numpy as np | ||
from fractions import Fraction | from fractions import Fraction | ||
Line 7: | Line 7: | ||
from functools import reduce | from functools import reduce | ||
from | from lib_temper.temper import cokernel, defactored_hnf | ||
from | from lib_temper.optimize import lstsq | ||
from | from lib_temper.subgroup import p_limit | ||
from | from lib_temper.interval import factors | ||
def prime_factors(n): | def prime_factors(n): | ||
Line 183: | Line 183: | ||
if not (low_count <= num_notes <= high_count): | if not (low_count <= num_notes <= high_count): | ||
continue | continue | ||
chroma_sizes = [abs(np.dot(s, np.array(t)) * 1200) for t in tmonzo_basis] | |||
avg_chroma_size = np.mean(chroma_sizes) | |||
# To find the longest diagonal, we check all 2^(d-1) diagonals | |||
longest_diagonal_len = 0 | |||
if dim > 0: | |||
for i in range(2**(dim)): | |||
signs = np.array([(1 if (i >> j) & 1 else -1) for j in range(dim-1)]) | |||
diagonal = np.sum(basis_vectors[1:] * signs[:, np.newaxis], axis=0) | |||
longest_diagonal_len = max(longest_diagonal_len, np.linalg.norm(diagonal)) | |||
result, val, is_strong = fokker_block( | result, val, is_strong = fokker_block( | ||
Line 200: | Line 211: | ||
'val': val, | 'val': val, | ||
'note_count': len(result), | 'note_count': len(result), | ||
'avg_chroma_size': avg_chroma_size, | |||
'longest_diagonal': longest_diagonal_len, | |||
}) | }) | ||
except ValueError: | except ValueError: | ||
continue | continue | ||
return strong_blocks | return sorted(strong_blocks, key=lambda x: x['longest_diagonal']) | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
Line 215: | Line 228: | ||
# print(*fokker_block("1029/1024", "[-4 -1 0 2>"), sep="\n") | # print(*fokker_block("1029/1024", "[-4 -1 0 2>"), sep="\n") | ||
print(*find_strong_blocks((12, 20), "441/440, 540/539")[:5], sep="\n\n") | |||
# print(*find_strong_blocks((9, 9), "", "2.3.5"), sep="\n\n") | # print(*find_strong_blocks((9, 9), "", "2.3.5"), sep="\n\n") | ||
print(*find_strong_blocks((4, 26), "49/48", offset=(-2,)), sep="\n\n") | # print(*find_strong_blocks((4, 26), "49/48", offset=(-2,)), sep="\n\n") | ||
</syntaxhighlight> | </syntaxhighlight> |