API reference
Documentation under construction
Polynomial
¶
A 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 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: 5*x_1**2*x_2*x_3**4*x_5 + 3*x_1*x_2 + 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 + 4*x_4^4*x_5^3 + 5*x_1^2*x_2*x_3^4*x_5
__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
|
|
__neg__
¶
__neg__() -> Polynomial
The negation of the polynomial.
All coefficients are multiplied by -1
. The exponents remain unchanged.
RETURNS | DESCRIPTION |
---|---|
Polynomial
|
A new polynomial with negated coefficients. |
__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: Algebraic) -> 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. |
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. |
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: