lattice.py
The file math/src/tonnetz/lattice.py. 65 lines, 5 definitions.
Planar lattice coordinates and the fundamental parallelogram of the torus.
The lattice point (i, j) goes to the pitch i * fifth + j * major third (mod N). In the triangular lattice e1 = (1, 0) is the direction of the fifth and e2 = (1/2, √3/2) the direction of the major third. Major triangles then point up and minor triangles point down.
Top of file: imports
"""Düzlem kafes koordinatları ve torusun temel paralelkenarı.Kafes noktası (i, j) şu perdeye gider: i * beşli + j * büyük üçlü (mod N).Üçgen kafeste e1 = (1, 0) beşli yönü, e2 = (1/2, √3/2) büyük üçlü yönüdür.Bu durumda majör üçgenler yukarı, minör üçgenler aşağı bakar."""from __future__ import annotationsfrom math import sqrtfrom .core import Tonnetzpitch_at
functionThe pitch of the lattice point (i, j): i * fifth + j * major third (mod N).
def pitch_at(t: Tonnetz, i: int, j: int) -> int: """(i, j) kafes noktasının perdesi: i * beşli + j * büyük üçlü (mod N).""" return (i * t.fifth + j * t.b) % t.Nxy
functionThe position of the lattice point (i, j) in the plane (equilateral triangular lattice).
def xy(i: float, j: float) -> tuple[float, float]: """(i, j) kafes noktasının düzlemdeki konumu (eşkenar üçgen kafes).""" return (i + j / 2, j * sqrt(3) / 2)_norm2
functiondef _norm2(i: int, j: int) -> int: # |i e1 + j e2|^2 = i^2 + i j + j^2 return i * i + i * j + j * jkernel_basis
functionA short basis of the lattice points whose pitch is 0 (the kernel lattice).
The basis vectors are the sides of the fundamental parallelogram of the torus. The determinant is ±N.
def kernel_basis(t: Tonnetz) -> tuple[tuple[int, int], tuple[int, int]]: """Perdesi 0 olan kafes noktalarının (çekirdek kafesi) kısa bir tabanı. Taban vektörleri torusun temel paralelkenarının kenarlarıdır. Determinant ±N'dir. """ N = t.N vecs = sorted( ( (i, j) for i in range(-N, N + 1) for j in range(-N, N + 1) if (i, j) != (0, 0) and pitch_at(t, i, j) == 0 ), key=lambda v: (_norm2(*v), -v[0], -v[1]), ) v1 = vecs[0] for v2 in vecs[1:]: det = v1[0] * v2[1] - v1[1] * v2[0] if abs(det) == N: if det < 0: v2 = (-v2[0], -v2[1]) return v1, v2 raise AssertionError("Çekirdek kafesi için taban bulunamadı.")representatives
functionFor each pitch, the lattice point closest to the origin (index = pitch).
def representatives(t: Tonnetz) -> list[tuple[int, int]]: """Her perde için başlangıca en yakın kafes noktası (dizin = perde).""" N = t.N best: dict[int, tuple[int, int]] = {} for i in range(-N, N + 1): for j in range(-N, N + 1): p = pitch_at(t, i, j) key = (_norm2(i, j), -i, -j) if p not in best or key < (_norm2(*best[p]), -best[p][0], -best[p][1]): best[p] = (i, j) return [best[p] for p in range(N)]