Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
—
Mathematical computations and operations on iterables.
Core mathematical functions for iterables.
def dotproduct(vec1, vec2):
"""
Compute dot product of two vectors.
Args:
vec1: First vector (iterable of numbers)
vec2: Second vector (iterable of numbers)
Returns:
Dot product as a number
"""
def sum_of_squares(iterable):
"""
Compute sum of squares of all items.
Args:
iterable: Iterable of numbers
Returns:
Sum of squares
"""Usage Examples:
from more_itertools import dotproduct, sum_of_squares
# Dot product
dp = dotproduct([1, 2, 3], [4, 5, 6])
# Result: 32 (1*4 + 2*5 + 3*6)
# Sum of squares
sos = sum_of_squares([1, 2, 3, 4])
# Result: 30 (1² + 2² + 3² + 4²)Functions for polynomial mathematics.
def polynomial_eval(coefficients, x):
"""
Evaluate polynomial at given value.
Args:
coefficients: Sequence of polynomial coefficients (constant first)
x: Value at which to evaluate polynomial
Returns:
Value of polynomial at x
"""
def polynomial_from_roots(roots):
"""
Generate polynomial coefficients from roots.
Args:
roots: Sequence of polynomial roots
Returns:
List of coefficients for polynomial with given roots
"""
def polynomial_derivative(coefficients):
"""
Compute derivative coefficients of polynomial.
Args:
coefficients: Sequence of polynomial coefficients
Returns:
List of derivative coefficients
"""Usage Examples:
from more_itertools import polynomial_eval, polynomial_from_roots
# Evaluate polynomial 2x² + 3x + 1 at x = 2
result = polynomial_eval([1, 3, 2], 2)
# Result: 15 (2*4 + 3*2 + 1)
# Polynomial from roots [1, 2] gives (x-1)(x-2) = x² - 3x + 2
coeffs = polynomial_from_roots([1, 2])
# Result: [2, -3, 1]Functions for signal processing and convolution.
def convolve(signal, kernel):
"""
Compute convolution of signal with kernel.
Args:
signal: Input signal (iterable of numbers)
kernel: Convolution kernel (iterable of numbers)
Returns:
Iterator of convolved values
"""
def dft(xlist):
"""
Compute Discrete Fourier Transform.
Args:
xlist: Input sequence (iterable of numbers)
Returns:
Iterator of complex DFT coefficients
"""
def idft(xlist):
"""
Compute Inverse Discrete Fourier Transform.
Args:
xlist: DFT coefficients (iterable of complex numbers)
Returns:
Iterator of real inverse DFT values
"""Functions for matrix mathematics.
def matmul(m1, m2):
"""
Matrix multiplication of two matrices.
Args:
m1: First matrix (sequence of sequences)
m2: Second matrix (sequence of sequences)
Returns:
Iterator of tuples representing result matrix rows
"""
def reshape(matrix, shape):
"""
Reshape matrix to new dimensions.
Args:
matrix: Input matrix or flat iterable
shape: New shape (int for 1D, iterable for multi-D)
Returns:
Iterator with reshaped data
"""Advanced number theory functions.
def factor(n):
"""
Generate prime factors of integer n.
Args:
n: Integer to factor
Returns:
Iterator of prime factors
"""
def is_prime(n):
"""
Test if n is prime using Miller-Rabin test.
Args:
n: Integer to test
Returns:
True if n is prime, False otherwise
"""
def sieve(n):
"""
Generate prime numbers up to n using Sieve of Eratosthenes.
Args:
n: Upper limit for prime generation
Returns:
Iterator of prime numbers ≤ n
"""
def totient(n):
"""
Compute Euler's totient function φ(n).
Args:
n: Positive integer
Returns:
Number of integers ≤ n that are coprime to n
"""
def multinomial(*counts):
"""
Compute multinomial coefficient.
Args:
*counts: Sequence of non-negative integers
Returns:
Multinomial coefficient
"""
def nth_prime(n, *, approximate=False):
"""
Return the nth prime number (0-indexed).
Args:
n: Index of prime to return (0-based)
approximate: If True, return approximate result for faster computation
Returns:
The nth prime number
"""Usage Examples:
from more_itertools import factor, is_prime, sieve, nth_prime
# Prime factorization
factors = list(factor(60))
# Result: [2, 2, 3, 5]
# Prime testing
print(is_prime(17)) # True
print(is_prime(18)) # False
# Generate primes up to 20
primes = list(sieve(20))
# Result: [2, 3, 5, 7, 11, 13, 17, 19]
# Get nth prime
first_prime = nth_prime(0) # Result: 2
tenth_prime = nth_prime(10) # Result: 31
# Fast approximation for large indices
approx_prime = nth_prime(1000000, approximate=True)Functions for statistical calculations.
def running_median(iterable, *, maxlen=None):
"""
Compute running median of values.
Args:
iterable: Input sequence of numbers
maxlen: Maximum window size for median calculation
Returns:
Iterator of running median values
"""Usage Examples:
from more_itertools import running_median
# Running median
medians = list(running_median([1, 2, 3, 4, 5]))
# Result: [1, 1.5, 2, 2.5, 3]Install with Tessl CLI
npx tessl i tessl/pypi-more-itertools