laurent#
Utilities for manipulating (Laurent) polynomials with SymPy: - reciprocal transforms f(x) -> f(x^{-1}) - normalization for ordinary and Laurent polynomials - symmetric normalization under t ↔ 1/t - canonicalization under permutations of variables
Functions
Canonicalize an expression up to permutations of variables (and optionally sign). |
|
|
Extract variables of the form <prefix><digits> from a SymPy expression. |
|
Normalize a (Laurent) polynomial up to monomials and ±1. |
|
Normalize a multivariate (non-Laurent) polynomial by removing minimal exponents. |
|
Normalize a Laurent polynomial symmetrically in a single variable. |
|
Return the reciprocal transform of a polynomial in a given variable. |
- reciprocal(expr, var)#
Return the reciprocal transform of a polynomial in a given variable.
Applies the substitution var → var**(-1) and expands.
- Parameters:
expr (Expr) – SymPy expression.
var (Symbol | str) – The variable to invert (Symbol or its name).
- Returns:
Expr – The transformed expression.
- Return type:
Expr
- normalize_polynomial(poly)#
Normalize a multivariate (non-Laurent) polynomial by removing minimal exponents.
For each generator, divides the polynomial by the minimal exponent of that generator across all monomials, so exponents start at 0.
- Parameters:
poly (Poly) – SymPy Poly with nonnegative exponents.
- Returns:
Poly – A normalized polynomial with minimal support.
- Return type:
Poly
- normalize_laurent_polynomial(expr, variables=None, normalize_sign=True)#
Normalize a (Laurent) polynomial up to monomials and ±1.
- Steps:
Shift exponents of selected variables so that all are nonnegative.
Optionally flip overall sign so the leading term has positive coefficient.
- Parameters:
expr (Expr) – SymPy expression (may have negative exponents).
variables (Iterable[Symbol] | None) – Variables to consider. If None, use all free symbols.
normalize_sign (bool) – If True, ensure leading term’s coefficient is positive.
- Returns:
Expr – A canonicalized expression.
- Return type:
Expr
- normalize_symmetric(expr, variable)#
Normalize a Laurent polynomial symmetrically in a single variable.
The result is centered and made symmetric under variable ↔ 1/variable, then the overall sign is chosen so the leading term is positive.
- Parameters:
expr (Expr) – SymPy expression.
variable (Symbol) – The variable to symmetrize.
- Returns:
Expr – Symmetrically normalized expression.
- Return type:
Expr
- extract_variables(expr, prefix=None)#
Extract variables of the form <prefix><digits> from a SymPy expression.
Examples
prefix=None → returns all symbols like t1, x2, a10, grouped by (prefix, index)
prefix=”t” → returns only t1, t2, …
- Parameters:
expr (Expr) – SymPy expression.
prefix (str | None) – Optional prefix filter.
- Returns:
list[Symbol] – Symbols sorted by (prefix, numeric index).
- Return type:
list[Symbol]
- canonicalize_under_variable_permutation(expr, variables=None, allow_sign_change=False)#
Canonicalize an expression up to permutations of variables (and optionally sign).
For all permutations of the selected variables, compute the permuted polynomial and choose the lexicographically minimal representation of (exponent-vector → coefficient) pairs (optionally also considering the negated polynomial).
- Parameters:
expr (Expr) – SymPy expression.
variables (Sequence[Symbol] | None) – Sequence of variables to permute. If None, attempts to detect with
extract_variables()
and then sorts them.allow_sign_change (bool) – If True, also consider -f under permutations and pick the best.
- Returns:
Expr – Canonical representative under variable permutations (and ±1 if enabled).
- Return type:
Expr