Skip to content

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.

Code, comments and printed output are in Turkish. The explanations are translated.

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 Tonnetz

pitch_at

function

The 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.N

xy

function

The 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

function
def _norm2(i: int, j: int) -> int:    # |i e1 + j e2|^2 = i^2 + i j + j^2    return i * i + i * j + j * j

kernel_basis

function

A 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

function

For 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)]

Every number on this site comes from data produced by the Python core and is checked by tests.

TÜBİTAK 2204-A mathematics project