API reference
Public¶
Info
Primary interface intended for end users.
Polynomial
¶
Bases: BasePolynomial
A scalar multivariate polynomial class.
Represents a multivariate polynomial in the form:
P(X) = ∑ c_i * x_1^e_i1 * x_2^e_i2 * ... * x_n^e_in
where c_i are the scalar coefficients and e_ji are the exponents of each
monomial.
| PARAMETER | DESCRIPTION |
|---|---|
exponents
|
A nested sequence or a NumPy 2D-array with shape (n_monomials, n_vars), where each row contains the exponents of one monomial. The order of variables is assumed to be increasing, i.e., [x_1, x_2, ..., x_n].
TYPE:
|
coefficients
|
A sequence or a NumPy 1D-array with shape (n_monomials,). Containing the corresponding scalar multipliers of each monomial.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_vars |
Number of variables in the polynomial.
TYPE:
|
degree |
Total degree of the polynomial.
TYPE:
|
exponents |
A NumPy 2D-array representing the exponents of the polynomial.
TYPE:
|
coefficients |
A NumPy 1D-array with the corresponding coefficients.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
The current implementation allows coefficients to be complex numbers, but complex polynomials are not yet officially supported and may produce unexpected behavior.
Although attributes are publicly accessible, modifying them directly may lead to bugs and unexpected behavior.
Examples:
Create the polynomial: 3*x_1*x_2 + 5*x_1^2*x_2*x_3^4*x_5 + 4*x_4^4*x_5^3
>>> exponents = [[1, 1, 0, 0, 0],
... [0, 0, 0, 4, 3],
... [2, 1, 4, 0, 1]]
>>> coefficients = [3, 4, 5]
>>> Polynomial(exponents, coefficients)
3*x_1*x_2 + 5*x_1^2*x_2*x_3^4*x_5 + 4*x_4^4*x_5^3
__add__
¶
__add__(other: object) -> Polynomial
Addition with another polynomial or scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The value to be added. A scalar can be an int, float, or NumPy scalars.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial representing the sum. |
__call__
¶
Evaluate the polynomial at a given point
| PARAMETER | DESCRIPTION |
|---|---|
point
|
A point with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float64
|
The result of evaluating the polynomial at |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Examples:
For univariate polynomials:
>>> poly = Polynomial.univariate([1, 2, 3])
>>> poly([0])
np.float64(1.0)
>>> poly([2])
np.float64(17.0)
For multivariate polynomials:
__lshift__
¶
__lshift__(other: int) -> Polynomial
Removes empty variables of the Polynomial.
A shorthand for Polynomial.shift(k) with k < 0 using the left shift
operator (<<). For more details, see the
Polynomial.shift() method.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The shift count. Must be a non-negative integer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial with shifted variables. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
__mul__
¶
__mul__(other: object) -> Polynomial
Multiplication with another polynomial or scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The value to be multiplied. A scalar can be an int, float, or NumPy scalars.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial representing the multiplication. |
__rshift__
¶
__rshift__(other: int) -> Polynomial
Adds extra variables to the Polynomial.
A shorthand for Polynomial.shift(k) with k > 0 using the right shift
operator (>>). For more details, see the
Polynomial.shift() method.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The shift count. Must be a non-negative integer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial with shifted variables. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
__sub__
¶
__sub__(other: ScalarAlgebraic) -> Polynomial
Subtraction with another polynomial or scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The value to be subtracted. A scalar can be an int, float, or NumPy scalars.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial representing the difference. |
__truediv__
¶
__truediv__(other: Scalar) -> Polynomial
Division with a scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The value to divide the polynomial by.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial representing the division. |
| RAISES | DESCRIPTION |
|---|---|
ZeroDivisionError
|
|
FloatingPointError
|
|
Notes
Currently, division can only be performed between polynomials and scalars.
partial
¶
partial(var_index: int) -> Polynomial
Partial derivative of a polynomial
Computes the partial derivative of the polynomial with respect to the variable
indexed by var_index.
| PARAMETER | DESCRIPTION |
|---|---|
var_index
|
The variable index to perform the partial derivative (zero-based).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
The resulting polynomial after differentiation. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Examples:
prune
¶
prune() -> Polynomial
Prune the empty monomials of a polynomial.
Removes all monomials whose associated coefficients are exactly zero.
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A pruned polynomial, containing only monomials with non-zero coefficients. |
Notes
If all coefficients are zero, a [zeros][polyany.Polynomial.zeros] polynomial
with the same number of variables is returned.
Examples:
This polynomial has four terms, but only the first and last have a non-zero coefficient.
The result keeps only the non-empty monomials, discarding all others.
quadratic_form
classmethod
¶
quadratic_form(matrix: ArrayLike) -> Polynomial
Creates a quadratic form from its associated symmetric matrix
| PARAMETER | DESCRIPTION |
|---|---|
matrix
|
A nested sequence or a NumPy 2D array of shape (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A second-degree homogeneous multivariate polynomial, i.e, a quadratic form. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
| WARNS | DESCRIPTION |
|---|---|
UserWarning
|
|
Notes
If matrix is not symmetric, its symmetric part is used instead,
computed as symmetric_part = (matrix + matrix.T) / 2.
Examples:
shift
¶
shift(k: int = 1) -> Polynomial
Shifts the polynomial variables.
This method returns a new polynomial with its variables shifted. A positive shift adds extra variables (increasing all variable indices). A negative shift removes variables, but only if they are empty.
| PARAMETER | DESCRIPTION |
|---|---|
k
|
The shift count. If positive, adds
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new polynomial with shifted variables. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
If k = 0 a copy of the polynomial is returned.
The Python shift operators can be used as a syntactic sugar for this method.
poly >> 3 is equivalent to poly.shift(3), and poly << 2 is equivalent to
poly.shift(-2).
This method is reversible as long as both directions are valid.
-
The statement
poly.shift(k).shift(-k)will return a polynomial equal to the original objectpoly. -
Likewise, if
poly.shift(-k)is possible, then applyingshift(k)after it will also return a copy ofpoly.
Examples:
Adding extra variables (shift right), increases the variable indices.
>>> poly = Polynomial.univariate([1, 2, 3])
>>> poly
1 + 2*x_1 + 3*x_1^2
>>> poly.shift(2)
1 + 2*x_3 + 3*x_3^2
>>> poly >> 2 # equivalent syntax
1 + 2*x_3 + 3*x_3^2
Removing empty variables (shift left), decreases the variable indices.
univariate
classmethod
¶
univariate(coefficients: ArrayLike) -> Polynomial
Creates a univariate polynomial from a coefficients vector
This classmethod is a convenient shortcut to construct a univariate polynomial from a coefficients vector.
| PARAMETER | DESCRIPTION |
|---|---|
coefficients
|
The coefficients of the univariate polynomial, associated with increasing
powers of the variable
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A univariate polynomial. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Examples:
zeros
classmethod
¶
zeros(n_vars: int) -> Polynomial
Create a zero polynomial.
Returns a polynomial with a single monomial (the constant 0)
in n_vars variables.
| PARAMETER | DESCRIPTION |
|---|---|
n_vars
|
Number of variables in the polynomial.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A zero polynomial. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
Primarily intended for internal use in specific cases.
Examples:
MatrixPolynomial
¶
Bases: BasePolynomial
A matrix multivariate polynomial class.
Represents a multivariate polynomial in the form:
P(X) = ∑ C_i * x_1^e_i1 * x_2^e_i2 * ... * x_n^e_in
where C_i are the matrix coefficients and e_ji are the exponents of each
monomial.
| PARAMETER | DESCRIPTION |
|---|---|
exponents
|
A nested sequence or a NumPy 2D-array with shape (n_monomials, n_vars), where each row contains the exponents of one monomial. The order of variables is assumed to be increasing, i.e., [x_1, x_2, ..., x_n].
TYPE:
|
coefficients
|
A sequence or a NumPy 3D-array with shape (n_monomials, n_rows, n_cols). Containing the corresponding matrix multipliers of each monomial.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_vars |
Number of variables in the polynomial.
TYPE:
|
degree |
Total degree of the polynomial.
TYPE:
|
shape |
Common shape of the matrices (n_rows, n_cols).
TYPE:
|
exponents |
A NumPy 2D-array representing the exponents of the polynomial.
TYPE:
|
coefficients |
A NumPy 3D-array with the corresponding matrix coefficients.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
The current implementation allows matrices to have complex entries, but complex polynomials are not yet officially supported and may produce unexpected behavior.
Although 1x1 matrices are allowed, if you intend to create a polynomial with
scalar coefficients, check the Polynomial class
for more efficient operations and manipulations.
Although attributes are publicly accessible, modifying them directly may lead to bugs and unexpected behavior.
Examples:
Create the matrix polynomial:
>>> exponents = [
... [1, 0],
... [0, 1],
... ]
>>> C_1 = np.eye(2)
>>> C_2 = np.arange(4).reshape(2, 2)
>>> coefficients = [C_1, C_2]
>>> MatrixPolynomial(exponents, coefficients)
[[1. 0.] [[0. 1.]
[0. 1.]]*x_1 + [2. 3.]]*x_2
T
property
¶
Transposition of a matrix polynomial
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A new matrix polynomial with matrix coefficients transposed. |
Notes
This method transposes only the coefficients, the exponents remain unchanged.
Examples:
__add__
¶
__add__(other: MatrixAlgebraic) -> MatrixPolynomial
Addition with another matrix polynomial, matrix or scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The operand in the addition. A scalar can be an int, float, or NumPy scalars. A matrix can be a NumPy 2D-array, nested lists or nested tuples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A new matrix polynomial representing the sum. |
__matmul__
¶
__matmul__(other: MatrixAlgebraic) -> MatrixPolynomial
Matrix product with another matrix or matrix polynomial
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The operand in the multiplication. A matrix can be a NumPy 2D-array, nested lists or nested tuples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A new matrix polynomial representing the matrix product. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
Matrix multiplication is generally non-commutative. Which means that:
operand_1 @ operand_2 != operand_2 @ operand_1.
Matrix multiplication with scalars is not supported, use
* instead.
__mul__
¶
__mul__(
other: MatrixAlgebraic | Scalar,
) -> MatrixPolynomial
Element-wise product with a scalar, matrix or matrix polynomial
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The operand in the multiplication. A matrix can be a NumPy 2D-array, nested lists or nested tuples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A new matrix polynomial representing the element-wise product. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
The element-wise multiplication, also known as the Hadamard product,
is commutative. Which means that, unlike
matrix multiplication,
operand_1 * operand_2 == operand_2 * operand_1.
__sub__
¶
__sub__(other: MatrixAlgebraic) -> MatrixPolynomial
Subtraction with another matrix polynomial, matrix or scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The operand in the subtraction. A scalar can be an int, float, or NumPy scalars. A matrix can be a NumPy 2D-array, nested lists or nested tuples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A new matrix polynomial representing the difference. |
__truediv__
¶
__truediv__(other: Scalar) -> MatrixPolynomial
Element-wise division with a scalar
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The value to divide element-wise the matrix polynomial.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Polynomial
|
A new matrix polynomial representing the element-wise division. |
| RAISES | DESCRIPTION |
|---|---|
ZeroDivisionError
|
|
FloatingPointError
|
|
Notes
Currently, element-wise division can only be performed between matrix polynomials and scalars.
from_scalar
classmethod
¶
from_scalar(
scalar_polynomial: Polynomial,
shape: tuple[int, int],
method: Literal["eye", "ones"] = "ones",
) -> MatrixPolynomial
Convert a scalar polynomial into a matrix polynomial.
| PARAMETER | DESCRIPTION |
|---|---|
scalar_polynomial
|
The scalar polynomial to be converted.
TYPE:
|
shape
|
Shape of the resultant matrix polynomial. |
method
|
Method used in the conversion. If "eye" the scalar coefficients are expanded using an identity (eye) matrix. If "ones" the scalar coefficients are expanded using an ones matrix. Defaults to "ones".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
The converted matrix polynomial. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Notes
The exponents, n_vars and degree remain unchanged in the conversion process.
Examples:
>>> from polyany import MatrixPolynomial, Polynomial
>>> poly = Polynomial.univariate([1, 2, 3])
>>> poly
1 + 2*x_1 + 3*x_1^2
>>> MatrixPolynomial.from_scalar(poly, (2, 2), "ones")
[[1. 1.] [[2. 2.] [[3. 3.]
[1. 1.]] + [2. 2.]]*x_1 + [3. 3.]]*x_1^2
>>> MatrixPolynomial.from_scalar(poly, (2, 2), "eye")
[[1. 0.] [[2. 0.] [[3. 0.]
[0. 1.]] + [0. 2.]]*x_1 + [0. 3.]]*x_1^2
zeros
classmethod
¶
zeros(
n_vars: int, shape: tuple[int, int]
) -> MatrixPolynomial
Create a zeros matrix polynomial.
Returns a polynomial with a single monomial
(the zeros matrix with shape shape) in n_vars variables.
| PARAMETER | DESCRIPTION |
|---|---|
n_vars
|
Number of variables in the matrix polynomial
TYPE:
|
shape
|
Shape of the zeros matrix |
| RETURNS | DESCRIPTION |
|---|---|
MatrixPolynomial
|
A zeros matrix polynomial. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
Primarily intended for internal use in specific cases.
Examples:
functions
¶
block
¶
block(
polynomials: Sequence[Sequence[MatrixPolynomial]],
) -> MatrixPolynomial
Create a matrix polynomial block
A polynomial block is the structure formed by concatenating polynomials both vertically and horizontally.
| PARAMETER | DESCRIPTION |
|---|---|
polynomials
|
A nested sequence (lists or tuples) of matrix polynomials to assemble.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
block_polynomial
|
A new matrix polynomial with assembled coefficients.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
Notes
Similar to NumPy behavior, the inner sequence is concatenated horizontally and then the resulting polynomials are concatenated vertically.
Examples:
>>> mpoly1 = MatrixPolynomial([[1], [2]], [np.eye(2), np.tri(2)])
>>> mpoly2 = MatrixPolynomial([[1]], [np.ones((2,2))])
>>> mpoly3 = MatrixPolynomial([[1]], [3*np.ones((3,2))])
>>> mpoly4 = MatrixPolynomial([[2]], [np.arange(6).reshape(3, 2)])
>>> block([[mpoly1, mpoly2], [mpoly3, mpoly4]])
[[1. 0. 1. 1.] [[1. 0. 0. 0.]
[0. 1. 1. 1.] [1. 1. 0. 0.]
[3. 3. 0. 0.] [0. 0. 0. 1.]
[3. 3. 0. 0.] [0. 0. 2. 3.]
[3. 3. 0. 0.]]*x_1 + [0. 0. 4. 5.]]*x_1^2
concatenate
¶
concatenate(
polynomials: Sequence[MatrixPolynomial], axis: int = 0
) -> MatrixPolynomial
Concatenate a sequence of matrix polynomials
The coefficient matrices of the polynomials are concatenated (vertically or horizontally) with respect to each monomial.
| PARAMETER | DESCRIPTION |
|---|---|
polynomials
|
A sequence (list or tuple) of matrix polynomials to concatenate.
TYPE:
|
axis
|
The axis along which the polynomials will be concatenated. Use 0 for vertical concatenation or 1 for horizontal concatenation. Default is 0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
concatenated_polynomial
|
A new matrix polynomial with concatenated coefficients.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
If a monomial exists in one polynomial but not in the others, a zeros matrix of appropriate shape is utilized.
Examples:
>>> mpoly1 = MatrixPolynomial([[1], [2]], [np.eye(2), np.tri(2)])
>>> mpoly2 = MatrixPolynomial([[1]], [np.ones((2,2))])
>>> concatenate([mpoly1, mpoly2]) # defaults to vertical concatenation
[[1. 0.] [[1. 0.]
[0. 1.] [1. 1.]
[1. 1.] [0. 0.]
[1. 1.]]*x_1 + [0. 0.]]*x_1^2
>>> concatenate([mpoly1, mpoly2], axis=1) # horizontal concatenation
[[1. 0. 1. 1.] [[1. 0. 0. 0.]
[0. 1. 1. 1.]]*x_1 + [1. 1. 0. 0.]]*x_1^2
Internal¶
Warning
Abstract class and types used in the package's core. Not intended for direct use by end users.
BasePolynomial
¶
Bases: ABC
A multivariate polynomial abstract base class.
Abstract implementation of the core structure of polynomials.
| PARAMETER | DESCRIPTION |
|---|---|
exponents
|
A nested sequence or a NumPy 2D-array with shape (n_monomials, n_vars), where each row contains the exponents of one monomial. The order of variables is assumed to be increasing, i.e., [x_1, x_2, ..., x_n].
TYPE:
|
coefficients
|
A sequence or NumPy array of coefficients corresponding to each monomial. The exact shape is defined by the concrete subclasses.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_vars |
Number of variables in the polynomial.
TYPE:
|
degree |
Total degree of the polynomial.
TYPE:
|
exponents |
A NumPy 2D-array representing the exponents of the polynomial.
TYPE:
|
coefficients |
A NumPy array of coefficients with shape defined by the concrete subclass.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
Although attributes are publicly accessible, modifying them directly may lead to bugs and unexpected behavior.
__neg__
¶
The negation of the polynomial.
All coefficients are multiplied by -1. The exponents remain unchanged.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
A new polynomial with negated coefficients. |
squeeze
¶
Remove the extra variables from a polynomial.
The coefficients remain unchanged.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
A new polynomial without extra variables. |
types
¶
MatrixAlgebraic
module-attribute
¶
MatrixAlgebraic: TypeAlias = ArrayLike | MatrixPolynomial
An algebraic element that can be a matrix or a matrix Polynomial.
Scalar
module-attribute
¶
A numeric scalar that can be a builtin numeric type or a NumPy scalar.
ScalarAlgebraic
module-attribute
¶
ScalarAlgebraic: TypeAlias = Scalar | Polynomial
An algebraic element that can be a scalar or a scalar Polynomial.