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 | SparseArray

    the coefficient tensor (dense or SparseArray)

  • prefactor: float | None

    an overall scalar multiplier (applied lazily)

  • shift: np.ndarray | None

    a per-variable shift to apply (applied lazily)

  • stack_dim: int

    the number of leading stack/batch axes

 

__repr__(self): 

LLM Docstring

Return a representation showing the coefficient shape and scaling.

  • :returns: str

    the 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: list

    the per-order coefficient tensors

  • prefactor: float | None

    an overall scalar multiplier

  • shift: np.ndarray | None

    a per-variable shift

  • rescale: bool

    divide each tensor entry by its permutation count

  • :returns: DensePolynomial

    the polynomial

 

@property
shape(self): 

LLM Docstring

The shape of the coefficient tensor (including the stack axes).

  • :returns: tuple

    the coefficient shape

 

@property
scaling(self): 

LLM Docstring

The overall scalar prefactor (1 when unset). Setting it stores a new deferred prefactor.

  • :returns: float

    the 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 | SparseArray

    the coefficient tensor

 

@property
coordinate_dim(self): 

LLM Docstring

The number of polynomial variables (the coefficient rank minus the stack axes).

  • :returns: int

    the coordinate dimension

 

__mul__(self, other) -> 'DensePolynomial': 

LLM Docstring

Multiply this polynomial by another (convolving their coefficient tensors) or by a scalar.

  • other: Any

    the multiplier polynomial or scalar

  • :returns: DensePolynomial

    the product polynomial (or 0/self for 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: Any

    the addend polynomial or scalar

  • :returns: DensePolynomial

    the sum polynomial

 

shift(self, shift) -> 'DensePolynomial': 

LLM Docstring

Return the polynomial with an added deferred variable shift (p(x + shift)).

  • shift: Any

    the per-variable shift (scalar or vector)

  • :returns: DensePolynomial

    the 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.ndarray

    the original coefficient tensor

  • shift: np.ndarray | float

    the per-variable shift

  • stack_dim: int

    the number of leading stack axes

  • :returns: np.ndarray

    the 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: list

    the per-order tensors being filled (modified in place)

  • idx: tuple

    the coefficient’s power index (with any stack prefix)

  • value: Any

    the coefficient value

  • stack_dim: int

    the number of leading stack axes

  • pcache: dict

    a cache of index permutations

  • permute: bool

    fill all index permutations

  • rescale: bool

    divide 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 | SparseArray

    the coefficient tensor (dense or sparse)

  • stack_dim: int | None

    the number of leading stack axes

  • permute: bool

    fill all index permutations

  • rescale: bool

    divide each value across its permutations

  • cutoff: float

    the magnitude below which dense entries are treated as zero

  • :returns: list

    the 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: list

    the per-order tensors

  • rescale: bool

    multiply each entry by its permutation count

  • allow_sparse: bool

    allow returning a SparseArray when 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: list

    the per-order coefficient tensors

 

@property
unscaled_coefficient_tensors(self): 

LLM Docstring

The per-order derivative tensors without permutation rescaling, computed lazily.

  • :returns: list

    the 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: Any

    the other polynomial/coefficients

  • :returns: DensePolynomial

    the outer-product polynomial

 

deriv(self, coord): 

LLM Docstring

Differentiate the polynomial with respect to one coordinate.

  • coord: int

    the coordinate index

  • :returns: DensePolynomial | int

    the derivative polynomial (or 0 if 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: DensePolynomial

    the 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: float

    the magnitude cutoff

  • :returns: DensePolynomial | int

    the 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: float

    the clipping magnitude cutoff

  • :returns: DensePolynomial

    the sparse-backed polynomial


Feedback

Examples

Templates

Documentation