chain.py
The file math/src/tonnetz/chain.py. 77 lines, 6 definitions.
The chain-of-fifths coordinate and chains of consecutive fifths.
Top of file: imports
chain_index
functionThe k for which x = fifth * k (mod N). The fifth must generate Z_N.
def chain_index(t: Tonnetz, x: int) -> int: """x = fifth * k (mod N) olacak şekilde k. Beşli Z_N'yi üretmelidir.""" if gcd(t.fifth, t.N) != 1: raise ValueError("Beşli Z_N'nin üreteci değil, zincir koordinatı tanımsız.") return (x * pow(t.fifth, -1, t.N)) % t.Nchain
functionThe pitches of the chain of n consecutive fifths starting at position k0 (in chain order).
def chain(t: Tonnetz, n: int, k0: int = 0) -> list[int]: """k0 konumundan başlayan n ardışık beşlilik zincirin perdeleri (zincir sırasıyla).""" if not 0 <= n <= t.N: raise ValueError("n, 0 ile N arasında olmalı.") return [(t.fifth * (k0 + i)) % t.N for i in range(n)]chain_chords
functionThe chords whose notes all lie inside the chain of n pitches.
def chain_chords(t: Tonnetz, n: int, k0: int = 0) -> list[Chord]: """Bütün sesleri n perdelik zincirin içinde kalan akorlar.""" return t.chords_within(chain(t, n, k0))chain_span
functionThe length of the shortest interval a triangle covers in the chain of fifths (difference of its ends).
In 53-TET, M(k) = {k-8, k, k+1} and m(k) = {k, k+1, k+9}, so it is 9.
def chain_span(t: Tonnetz) -> int: """Bir üçgenin beşli zincirinde kapladığı en kısa aralığın uzunluğu (uç farkı). 53-TET'te M(k) = {k-8, k, k+1} ve m(k) = {k, k+1, k+9}, yani 9. """ spans = [] for c in (Chord("M", 0), Chord("m", 0)): ks = sorted(chain_index(t, p) for p in t.pitches(c)) gaps = [ks[1] - ks[0], ks[2] - ks[1], t.N - ks[2] + ks[0]] spans.append(t.N - max(gaps)) if spans[0] != spans[1]: raise AssertionError("Majör ve minör üçgenlerin zincir açıklığı farklı çıktı.") return spans[0]chain_count_table
functionFor each n, the number of triangles the chain contains (brute force over all values of k0).
If the count depends on k0, "k0_independent" is False.
def chain_count_table(t: Tonnetz) -> list[dict]: """Her n için zincirin içerdiği üçgen sayısı (kaba kuvvet, bütün k0 değerleri). Sayı k0'a bağlı çıkarsa "k0_independent" False olur. """ rows = [] for n in range(t.N + 1): counts = set() for k0 in range(t.N): cs = chain_chords(t, n, k0) counts.add((sum(c.kind == "M" for c in cs), sum(c.kind == "m" for c in cs))) major, minor = min(counts) rows.append( { "n": n, "major": major, "minor": minor, "total": major + minor, "k0_independent": len(counts) == 1, } ) return rowsformula_range
functionThe widest range of n over which the formula total = 2 * (n - span) holds (with non-negative values).
def formula_range(t: Tonnetz) -> tuple[int, int]: """total = 2 * (n - span) formülünün (negatif olmayan değerlerle) tuttuğu en geniş n aralığı.""" span = chain_span(t) ok = [r["n"] for r in chain_count_table(t) if r["total"] == 2 * (r["n"] - span)] lo = hi = span if span not in ok: raise AssertionError("Formül n = span için tutmuyor.") while hi + 1 in ok: hi += 1 return lo, hi