Topos deals with the abstract mathematical and structural foundations of music, providing
topological scaffolding that can be instantiated into any musical context.
Generates tuples of elements from any number of input lists in a cyclic manner.
Creates a list of tuples where each tuple contains one element from each input list.
The pairing continues cyclically until the length of the generated list equals
the product of the lengths of all input lists. When the end of any list is reached,
the iteration continues from the beginning of that list, effectively cycling through
the shorter lists until all combinations are created.
This is a form of “cyclic pairing” or “modulo-based pairing” and is
different from computing the Cartesian product.
Parameters:
*lists – Any number of input lists.
Returns:
A tuple of tuples where each inner tuple contains one element
from each input list.
Provides static methods for generating various integer sequences devised
by the Danish composer Per Nørgård, most notably the infinity series
used extensively in his compositional practice.
When start is 0 and step is 1 an optimised pair-wise recurrence is
used. For arbitrary start/step combinations the series is computed
element-wise via inf_num.
Parameters:
start (int, optional) – Index at which to begin the series (default is 0).
size (int, optional) – Number of elements to generate (default is 128).
step (int, optional) – Index step between successive elements (default is 1).
Extends the tripartite construction to an arbitrary seed length and
inversion pattern, producing self-similar integer sequences by
recursively applying delta-inversion rules.
Parameters:
seed (list of int, optional) – Initial values of the series (default is [0,-2,-1]).
inv_pat (list of int, optional) – Cyclic pattern of inversion multipliers applied to successive
deltas (default is [-1,1,1]).
size (int, optional) – Number of elements to generate (default is 128).
This class provides a collection of static methods for performing
common set operations including union, intersection, difference,
and specialized musical set operations like transposition and inversion.
The interval vector represents the number of occurrences of each interval
between pitches in a set. Intervals larger than half the modulus are
inverted to their complements.
Parameters:
set1 (set) – A set of integers representing pitches.
modulus (int, optional) – The modulus for interval calculations (default is 12).
Returns:
An array representing the interval vector with length modulus//2.
Xenakis-style sieve for generating sets through modular arithmetic operations.
A Sieve implements Iannis Xenakis’s concept of sieves, which are mathematical
structures based on modular arithmetic used in algorithmic composition.
A sieve can represent a single modular constraint or complex combinations
of multiple constraints using logical operations.
In Xenakis’s notation, a basic sieve is written as (m,r) where m is the
modulus and r is the residue. Complex sieves combine these using union (∪),
intersection (∩), and complement operations.
Parameters:
modulus (int, optional) – The step size of the arithmetic progression (default is 1).
residue (int, optional) – The starting value of the progression (default is 0).
N (int, optional) – The upper bound for generated values (default is 255).
This implementation provides the foundation for Xenakis sieve theory.
For complex sieve expressions, use the Operations class methods to
combine multiple Sieve instances with logical operations.
A combinatorial set generating all r-combinations from a set of factors.
This class generates all possible combinations of size r from a given set of factors,
useful for combinatorial analysis and set operations. It also creates symbolic
aliases and an associated graph structure for analysis.
Parameters:
factors (tuple, optional) – The factors to combine (default is (‘A’, ‘B’, ‘C’, ‘D’)).
r (int, optional) – The size of each combination (default is 2).
A set of integer partitions with associated graph structures for analysis.
This class generates all partitions of an integer n into exactly k parts,
analyzes their structural properties, and creates various graph representations
for studying relationships between partitions.
graph_type ({'feature_distance', 'decomposition_tree', 'substructure_embedding'}, optional) – The type of graph to construct (default is ‘feature_distance’).
graph_type ({'feature_distance', 'decomposition_tree', 'substructure_embedding'}, optional) – The type of graph to construct (default is ‘feature_distance’).
Generated Collection - A multiplicative collection formed by repeatedly applying a generator.
A multiplicative collection created by starting with an initial value and repeatedly
multiplying by the generator, reducing modulo the period. This is the multiplicative
analog to the Sieve class, useful for generating multiplicative pitch collections
and harmonic series subsets.
Parameters:
generator (Union[str, int, float, Fraction]) – The generator value used for multiplication.
period (Union[str, int, float, Fraction], optional) – The period of equivalence (default is 2).
iterations (int, optional) – Number of times to apply the generator (default is 12).
Renumber the nodes in the graph to consecutive integers.
Parameters:
method (str) – The method to use for renumbering:
- ‘default’: Use sequential numbering
- ‘dfs’: Use depth-first search preorder
- ‘bfs’: Use breadth-first search
Transform a symmetric cost matrix into an undirected graph where
edge weights represent the costs between nodes. Self-loops are
excluded from the resulting graph.
Parameters:
cost_matrix (numpy.ndarray) – Symmetric cost matrix with numeric values. Should be square
with dimensions matching the length of items.
items (List[T]) – List of items corresponding to matrix indices. Used as node
values in the resulting graph.
Returns:
Undirected graph with nodes corresponding to matrix indices
and edge weights equal to the cost matrix values. Only edges
with positive costs are included. Node attributes ‘value’
contain the original items.
Represents a rooted tree where each node has at most one parent.
Built from nested tuple structures and backed by a RustworkX directed graph
inherited from Graph.
Subclasses can override _node_value_attr to use a different attribute
name for the node value (e.g. RhythmTree uses 'proportion' instead of
'label').
Parameters:
root (hashable) – The value for the root node of the tree.
children (tuple) – Nested tuple structure defining the tree’s children.
Each element is either a leaf value or a (value,children) pair.
Graft a subtree onto the tree at the specified leaf node.
Parameters:
target_node (int) – The leaf node where the subtree will be grafted
subtree (Tree) – The Tree instance to graft onto this tree
mode (str, optional) – Grafting mode - either ‘replace’ or ‘adopt’ (default: ‘replace’)
- ‘replace’: Replace the leaf node with subtree root
- ‘adopt’: Keep the leaf node and give it the children from subtree root
Returns:
The root node ID of the grafted subtree (for ‘replace’ mode) or
the target_node ID (for ‘adopt’ mode)
Immutable (D,S) pair representing a rhythmic subdivision group.
D is the top-level duration/divisor and S is a (possibly nested)
tuple of subdivisions. Nested tuples within S are recursively
converted to Group instances, so the entire tree is typed
throughout.
Parameters:
G (tuple) – A two-element tuple (D,S) where D is a numeric duration and
S is a subdivision value or nested tuple of subdivisions.
Rotate the leaf values of a nested subdivision tuple.
Flattens the leaves, performs a left-rotation by n positions, and
re-nests them into the original structure. When preserve_signs is
True, only the absolute values are rotated while the original sign
at each leaf position is retained.
Parameters:
subdivs (tuple) – Arbitrarily nested tuple of numeric values.
n (int, optional) – Number of positions to rotate left (default is 1).
preserve_signs (bool, optional) – If True, rotate absolute values only and reapply the original
signs at each position (default is False).
Returns:
A new nested tuple with the same shape as subdivs and rotated
leaf values.
A lattice provides a discrete sampling of n-dimensional space with integer
coordinates. Nodes are accessed via coordinate tuples but stored internally
as integer node IDs in the underlying RustworkX graph.
resolution (int or list of int) – Number of points along each dimension, or list of resolutions per dimension.
bipolar (bool, optional) – If True, coordinates range from -resolution to +resolution.
If False, coordinates range from 0 to resolution (default is True).
periodic (bool, optional) – Whether to use periodic boundary conditions (default is False).
Perform a random walk on a lattice starting from a given coordinate.
Parameters:
lattice (Lattice) – The lattice to perform the random walk on.
start_coord (Tuple[int, ...]) – Starting coordinate for the random walk.
num_steps (int) – Number of steps to take in the random walk.
max_repeats (Optional[int]) – Maximum number of times any coordinate can be visited. If None, no limit.
seed (Optional[int]) – Random seed for reproducible walks.
avoid_backtrack (bool) – If True, avoid immediately returning to the previous coordinate when possible.
stuck_tolerance (int) – When the walk gets stuck (all neighbors exceed max_repeats),
temporarily allow one extra visit up to this many times before
stopping. Resets after each non-stuck step. Default is 3.
Returns:
List of coordinates representing the random walk path.
Perform a biased random walk with directional preferences.
Parameters:
lattice (Lattice) – The lattice to perform the walk on.
start_coord (Tuple[int, ...]) – Starting coordinate for the walk.
direction_weights (List[float]) – Weights for each dimension direction. Length should be 2 * dimensionality,
where indices [0, 1] are weights for dimension 0 [-1, +1], etc.