Skip to content

Instantly share code, notes, and snippets.

@arseniiv
Created December 27, 2023 18:59
Show Gist options
  • Select an option

  • Save arseniiv/361cb4b851ac340b761627f1b0b90afa to your computer and use it in GitHub Desktop.

Select an option

Save arseniiv/361cb4b851ac340b761627f1b0b90afa to your computer and use it in GitHub Desktop.
List intervals in various edo/edX scales that are close to each other
from collections import defaultdict
from dataclasses import dataclass
from fractions import Fraction
from itertools import pairwise
from typing import Final, Sequence
@dataclass
class EdIntervals:
ed_fractions: list[Fraction]
sources: dict[Fraction, list[int]]
def sorted_intervals(ed_sizes: Sequence[int]) -> EdIntervals:
# generate a single period without 0\N and N\N because trivial
ed_fractions = set()
sources = defaultdict(list)
for ed_size in ed_sizes:
for step_count in range(1, ed_size):
x = Fraction(step_count, ed_size)
ed_fractions.add(x)
sources[x].append(ed_size)
return EdIntervals(list(sorted(ed_fractions)), sources)
def print_confluences(data: EdIntervals, max_error: Fraction) -> None:
def cents(x: Fraction) -> str:
return f'{x * 1200 :.2f}¢'
def in_ed(x: Fraction, ed_size: int) -> str:
return f'{x * ed_size}\\{ed_size}'
def full_info(x: Fraction) -> str:
return f'{cents(x)} [{", ".join(in_ed(x, n)
for n in data.sources[x])}]'
for x1, x2 in pairwise(data.ed_fractions):
if (delta := x2 - x1) >= max_error:
continue
print(f'{full_info(x1)} is {cents(delta)} away from {full_info(x2)}')
def main() -> None:
CENT: Final = Fraction(1, 1200)
intervals = sorted_intervals([12, 17, 22, 24])
print_confluences(intervals, 10 * CENT)
if __name__ == '__main__':
main()
@arseniiv

Copy link
Copy Markdown
Author

This is at least Python 3.10 because walrus was used. Can downgrade the version even more.

With these settings, the output is:

50.00¢ [1\24] is 4.55¢ away from 54.55¢ [1\22]
100.00¢ [1\12, 2\24] is 9.09¢ away from 109.09¢ [2\22]
141.18¢ [2\17] is 8.82¢ away from 150.00¢ [3\24]
211.76¢ [3\17] is 6.42¢ away from 218.18¢ [4\22]
272.73¢ [5\22] is 9.63¢ away from 282.35¢ [4\17]
350.00¢ [7\24] is 2.94¢ away from 352.94¢ [5\17]
490.91¢ [9\22] is 3.21¢ away from 494.12¢ [7\17]
494.12¢ [7\17] is 5.88¢ away from 500.00¢ [5\12, 10\24]
545.45¢ [10\22] is 4.55¢ away from 550.00¢ [11\24]
650.00¢ [13\24] is 4.55¢ away from 654.55¢ [12\22]
700.00¢ [7\12, 14\24] is 5.88¢ away from 705.88¢ [10\17]
705.88¢ [10\17] is 3.21¢ away from 709.09¢ [13\22]
847.06¢ [12\17] is 2.94¢ away from 850.00¢ [17\24]
917.65¢ [13\17] is 9.63¢ away from 927.27¢ [17\22]
981.82¢ [18\22] is 6.42¢ away from 988.24¢ [14\17]
1050.00¢ [21\24] is 8.82¢ away from 1058.82¢ [15\17]
1090.91¢ [20\22] is 9.09¢ away from 1100.00¢ [11\12, 22\24]
1145.45¢ [21\22] is 4.55¢ away from 1150.00¢ [23\24]

Basically don’t specify an MedX if an (M×N)edX is already present (like 12 is superfluous here with 24 already present) and the output will be cleaner.

@arseniiv

Copy link
Copy Markdown
Author

P. S. Sorry this is all lies, this is all edX for X = 2 only because the cents are calculated with this in mind. Sorry I forgot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment