Skip to content

Instantly share code, notes, and snippets.

View arseniiv's full-sized avatar
🌜

Arsenii A. arseniiv

🌜
View GitHub Profile
@arseniiv
arseniiv / eratosthenes_iter.py
Created August 30, 2025 14:33
Eratosthenes prime sieve using iterators
from __future__ import annotations
from collections import deque
from itertools import count, cycle, islice
from typing import Final, Iterator
from time import perf_counter
def sieve() -> Iterator[int]:
"""
A slightly improved Eratosthenes prime sieve, implemented using iterators.
Cycles start on squares of primes because adding them earlier is unnecessary.
@arseniiv
arseniiv / README.md
Created March 10, 2025 19:49
Comma search tool

This is a simple search tool to find commas between intervals (maybe even irrational ones, though it would try to show them as fractions). You just specify a list of basis intervals to measure against each other, an upper bound in cents to filter interval combinations through, and the desired count. The code then iterates over suitable combinations from smaller to larger taxicab distances in the lattice spanned by the basis, ensuring simpler commas show first. (But which is simpler depends on your basis! 3/2 and 4/3 is one thing, 3 and 2 is another, 9/8 and 32/27 is yet another still.)

The output of

show_commas(['4/3', '5/4', '6/5', '7/6'], under_cents=25, count=15)

here in main is:

  7.712c = 225/224 = (4/3)^-1 (5/4)^2 (6/5)^0 (7/6)^-1 
 21.902c = 875/864 = (4/3)^0 (5/4)^1 (6/5)^-2 (7/6)^1 
@arseniiv
arseniiv / finally-invariant-pga.md
Created November 30, 2024 07:28
Finally I understand how to define PGA invariantly

Finally I understand how to define PGA invariantly

When using framerowks stemming from linear algebra to do calculation, we have to use coordinates (and so, bases). But this is often extraneous for mathematical analysis of what and why we are doing. So when we construct an object that’s independent of any basis chosen, it’s usually desirable to do so without introducing any choice of basis beforehand.

For a long time, an invariant construction of projective geometric algebra, a Clifford algebra approach to calculate with points, lines, segments and so on, and translations of the point space as opposed to just rotations and reflections which an unflavored geometric algebra does, eluded me.

@arseniiv
arseniiv / continued_fraction.py
Created March 26, 2024 22:48
Continued fractions, incl. handling quadratic surds
"""
Reference implementation of continued fraction routines
by 0.5°.
"""
from __future__ import annotations
from dataclasses import InitVar, dataclass
from itertools import pairwise
from math import floor, inf, isinf, isnan, isqrt, lcm, sqrt
from typing import Iterable, Iterator, Literal, Sequence, overload
@arseniiv
arseniiv / blackjack.py
Last active March 20, 2024 21:42
Comparing simple strategies of playing one-suit black jack with individual decks for each player
from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from fractions import Fraction
from typing import Callable, Final, Iterable, Iterator
@dataclass(frozen=True, slots=True)
class Prob[TDom, TProb: (float, Fraction)]:
"""Probability monad."""
@arseniiv
arseniiv / guide.md
Last active March 14, 2024 20:44
a partial tuneBfree guide

Doing stuff with tuneBfree

tuneBfree is naren’s fork of a setBfree tonewheel organ emulation that allows custom tuning using MTS-ESP. It being one of the first microtunable tonewheel organ synths, I thought it might be useful to write a quick post of what one might wish to consider or have in mind tuning it.

MTS-ESP connection basics

An MTS-ESP master plugin like MTS-ESP Mini should be present in your project somewhere. It can be in passthru mode to conserve resources; the only thing you need to be sure of is that it says that a client is connected. If tuneBfree is the sole MTS-ESP-aware plugin in there, that’s it! Load a tuning and it will apply immediately.

Note that hosts like Carla or Element, as far I was able to work them, don’t allow MTS-ESP connection. Either I didn’t do something crucial or you need another more diversely functional DAW.

@arseniiv
arseniiv / subdivide_scale_best.py
Created February 18, 2024 23:25
Sorting subdivided scales by avg variety, filtering out rotated variants
from __future__ import annotations
from collections import defaultdict
from typing import Final, Iterable, Iterator, Mapping, Sequence
import operator
from dataclasses import dataclass
from itertools import accumulate, permutations, product
from math import prod
from fractions import Fraction as F
def avg_variety(scale_steps: Sequence[F]) -> float:
@arseniiv
arseniiv / edo_confluence.py
Created December 27, 2023 18:59
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]]
@arseniiv
arseniiv / scale_approx.py
Created December 23, 2023 19:43
Approximate a scale with a periodic scale with specific step pattern
"""
Fitting a scale to have a given pattern of steps between notes.
Install `sympy` and `more_itertools`.
"""
from typing import Sequence
from collections import defaultdict
from dataclasses import dataclass
from fractions import Fraction
@arseniiv
arseniiv / replacenotes.py
Created October 3, 2023 18:59
replacenotes.py
'''
Replaces notes with flats with enharmonic sharps in SFZ and DSPRESET files.
'''
from pathlib import Path
from typing import Final
import re
DS_EXTS: Final = frozenset(['.dspreset'])
SFZ_EXTS: Final = frozenset(['.sfz'])