DensePolynomial
A straightforward dense n-dimensional polynomial data structure with multiplications and shifts
__init__(self, coeffs, prefactor=None, shift=None, stack_dim=0):
LLM Docstring
Build a dense n-dimensional polynomial from a coefficient tensor, with a deferred scalar prefactor and variable shift.
The coefficient tensor is indexed by per-variable powers; a leading stack_dim
axes hold a batch/stack of polynomials. The prefactor and shift are applied
lazily the first time the coefficients are materialized.
coeffs:np.ndarray | SparseArraythe coefficient tensor (dense or
SparseArray)prefactor:float | Nonean overall scalar multiplier (applied lazily)
shift:np.ndarray | Nonea per-variable shift to apply (applied lazily)
stack_dim:intthe number of leading stack/batch axes
__repr__(self):
LLM Docstring
Return a representation showing the coefficient shape and scaling.
:returns:strthe representation
@classmethod
from_tensors(cls, tensors, prefactor=None, shift=None, rescale=True):
LLM Docstring
Build a DensePolynomial from a list of derivative/coefficient tensors (one per
order), condensing them into a single coefficient tensor.
tensors:listthe per-order coefficient tensors
prefactor:float | Nonean overall scalar multiplier
shift:np.ndarray | Nonea per-variable shift
rescale:booldivide each tensor entry by its permutation count
:returns:DensePolynomialthe polynomial
@property
shape(self):
LLM Docstring
The shape of the coefficient tensor (including the stack axes).
:returns:tuplethe coefficient shape
@property
scaling(self):
LLM Docstring
The overall scalar prefactor (1 when unset). Setting it stores a new deferred prefactor.
:returns:floatthe scaling factor
@property
coeffs(self) -> 'np.ndarray|SparseArray':
LLM Docstring
The materialized coefficient tensor, applying (and then clearing) any deferred shift and prefactor on first access. Setting it replaces the raw coefficients.
:returns:np.ndarray | SparseArraythe coefficient tensor
@property
coordinate_dim(self):
LLM Docstring
The number of polynomial variables (the coefficient rank minus the stack axes).
:returns:intthe coordinate dimension
__mul__(self, other) -> 'DensePolynomial':
LLM Docstring
Multiply this polynomial by another (convolving their coefficient tensors) or by a scalar.
other:Anythe multiplier polynomial or scalar
:returns:DensePolynomialthe product polynomial (or
0/selffor the scalar special cases)
__add__(self, other) -> 'DensePolynomial':
LLM Docstring
Add another polynomial (aligning and padding their coefficient tensors) or a scalar (added to the constant term).
other:Anythe addend polynomial or scalar
:returns:DensePolynomialthe sum polynomial
shift(self, shift) -> 'DensePolynomial':
LLM Docstring
Return the polynomial with an added deferred variable shift (p(x + shift)).
shift:Anythe per-variable shift (scalar or vector)
:returns:DensePolynomialthe shifted polynomial
@classmethod
compute_shifted_coeffs(cls, poly_coeffs, shift, stack_dim=0):
LLM Docstring
Compute the coefficient tensor of a polynomial after a variable shift
(p(x + shift)), via a factorial-weighted convolution of the coefficients with
the shift powers (a Taylor re-expansion).
poly_coeffs:np.ndarraythe original coefficient tensor
shift:np.ndarray | floatthe per-variable shift
stack_dim:intthe number of leading stack axes
:returns:np.ndarraythe shifted coefficient tensor
@classmethod
fill_tensors(self, tensors, idx, value, stack_dim, pcache, permute, rescale):
LLM Docstring
Scatter a single coefficient value into the per-order derivative tensors, filling every index permutation (optionally rescaling by the permutation count) so the resulting tensors are symmetric.
tensors:listthe per-order tensors being filled (modified in place)
idx:tuplethe coefficient’s power index (with any stack prefix)
value:Anythe coefficient value
stack_dim:intthe number of leading stack axes
pcache:dicta cache of index permutations
permute:boolfill all index permutations
rescale:booldivide the value across its permutations
@classmethod
extract_tensors(cls, coeffs, stack_dim=None, permute=True, rescale=True, cutoff=1e-15):
LLM Docstring
Decompose a coefficient tensor into a list of per-order (symmetric) derivative tensors, scattering each nonzero coefficient across its index permutations.
coeffs:np.ndarray | SparseArraythe coefficient tensor (dense or sparse)
stack_dim:int | Nonethe number of leading stack axes
permute:boolfill all index permutations
rescale:booldivide each value across its permutations
cutoff:floatthe magnitude below which dense entries are treated as zero
:returns:listthe per-order tensors (index 0 is the constant term)
@classmethod
condense_tensors(cls, tensors, rescale=True, allow_sparse=True):
LLM Docstring
Collapse a list of per-order derivative tensors back into a single (dense or sparse) coefficient tensor, choosing a sparse representation when the density is low.
tensors:listthe per-order tensors
rescale:boolmultiply each entry by its permutation count
allow_sparse:boolallow returning a
SparseArraywhen sparse enough:returns:tuple(coefficient_tensor, stack_dim)
@property
coefficient_tensors(self):
LLM Docstring
The per-order (permutation-rescaled) derivative tensors of the polynomial, computed lazily.
:returns:listthe per-order coefficient tensors
@property
unscaled_coefficient_tensors(self):
LLM Docstring
The per-order derivative tensors without permutation rescaling, computed lazily.
:returns:listthe per-order unscaled coefficient tensors
transform(self, lin_transf):
Applies (for now) a linear transformation to the polynomial
outer(self, other):
LLM Docstring
Form the outer-product polynomial of this one with another coefficient tensor (no stack dimensions supported).
other:Anythe other polynomial/coefficients
:returns:DensePolynomialthe outer-product polynomial
deriv(self, coord):
LLM Docstring
Differentiate the polynomial with respect to one coordinate.
coord:intthe coordinate index
:returns:DensePolynomial | intthe derivative polynomial (or
0if constant in that coordinate)
grad(self):
LLM Docstring
Return the gradient polynomial, whose leading stack axis indexes the derivative with respect to each coordinate.
:returns:DensePolynomialthe gradient polynomial
clip(self, threshold=1e-15):
LLM Docstring
Drop coefficients below a magnitude threshold, returning a trimmed polynomial
(or 0 if everything is clipped).
threshold:floatthe magnitude cutoff
:returns:DensePolynomial | intthe clipped polynomial (or
0)
make_sparse_backed(self, threshold=1e-15):
LLM Docstring
Return an equivalent polynomial whose coefficients are stored as a SparseArray
(after clipping small entries).
threshold:floatthe clipping magnitude cutoff
:returns:DensePolynomialthe sparse-backed polynomial