Polynomial objects¶
-
class
sageopt.symbolic.polynomials.
Polynomial
(alpha_maybe_c, c=None)¶ This class provides a symbolic representation for polynomials which are sparse in the monomial basis. These functions look like the following.
\[x \mapsto \sum_{i=1}^m c_i \prod_{j=1}^n {x_j}^{\alpha_{ij}}\]The constructor for this class can be called in two different ways. The arguments to the constructor have names which reflect the different possibilities.
- Parameters
alpha_maybe_c (dict or NumPy ndarray) –
If
alpha_maybe_c
is a dict, then it must be a dictionary from tuples-of-numbers to scalars. The keys will be converted to rows of a matrix which we callalpha
, and the values will be assembled into a vector which we callc
.If
alpha_maybe_c
is a NumPy ndarray, then the argumentc
must also be an ndarray, andc.size
must equalalpha_maybe_c.shape[0]
.c (None or NumPy ndarray) – This value is only used when
alpha_maybe_c
is a NumPy ndarray. If that is the case, then this Polynomial will represent the functionlambda x: c @ np.prod(np.power(alpha_maybe_c, x))
.
Examples
There are two ways to call the Polynomial constructor.
The first way is to specify a dictionary from tuples to scalars. Each key-value pair of the dictionary represents a monomial appearing in this polynomial. The key tuples must all be of some common length
n
. If(a, coeff)
is a key-value pair in this dictionary, then the Polynomial includes an additive termlambda x: coeff * np.prod(np.power(a, x))
.alpha_and_c = {(1,): 2} f = Polynomial(alpha_and_c) print(f(1)) # equal to 2. print(f(-3)) # equal to -6.
The second way is to specify two arguments. In this case the first argument
alpha
is a NumPy array of exponent vectors, wherealpha[i, j]
is the power of variablej
in monomiali
. The second argument is a numpy arrayc
, wherec[i]
is the coefficient on the i-th monomial defined byalpha
.alpha = np.array([[1, 0], [0, 1], [1, 1]]) c = np.array([1, 2, 3]) f = Polynomial(alpha, c) x = np.array([-4, 7]) val = f(x) # val = 1 * (-4) + 2 * (7) + 3 * (-4 * 7) print(val) # -74
-
n
¶ The dimension of the space over which this Polynomial is defined. The number of columns in
alpha
, and the length of tuples appearing in the dictionaryalpha_c
.- Type
int
-
m
¶ The number of terms needed to express this Polynomial in the standard monomial basis. The number of rows in
alpha
. The length of the dictionaryalpha_c
.- Type
int
-
alpha
¶ Has shape
(m, n)
. Entryalpha[i,j]
is the power of an implicit variablex[j]
appearing in the i-th monomial for this Polynomial. The i-th monomial, in turn, has coefficientc[i]
.- Type
NumPy ndarray
-
c
¶ Has shape
(m,)
. The scalarc[i]
is this Polynomial’s coefficient on the basis functionlambda x: np.prod(np.power(alpha[i, :], x))
. It is possible to havec.dtype == object
, to allow for coniclifts Variables.- Type
NumPy ndarray
-
alpha_c
¶ The keys of
alpha_c
are tuples of lengthn
, containing real numeric types (e.g int, float). These tuples correspond to rows inalpha
.- Type
defaultdict
Notes
The Polynomial class subclasses the Signomial class. This is done because most algebraic operations between polynomials and signomials are identical. However it is important to remember that polynomials and signomials evaluate in very different ways.
-
as_signomial
()¶ - Returns
f – For every elementwise positive vector
x
, we haveself(x) == f(np.log(x))
.- Return type
-
constant_location
()¶ Return the index
i
so thatalpha[i, :]
is the zero vector.
-
even_locations
()¶ Return the largest ndarray
evens
, so thatnp.all(alpha[evens,:] % 2 == 0)
.
-
property
grad
¶ A numpy ndarray of shape
(n,)
whose entries are Polynomials. For a numpy ndarrayx
,grad[i](x)
is the partial derivative of this Polynomial with respect to coordinatei
, evaluated atx
. This array is constructed only when necessary, and is cached upon construction.
-
property
hess
¶ A numpy ndarray of shape
(n, n)
, whose entries are Polynomials. For a numpy ndarrayx
,hess[i,j](x)
is the (i,j)-th partial derivative of this Polynomial, evaluated atx
. This array is constructed only when necessary, and is cached upon construction.
-
partial
(i)¶ Compute the symbolic partial derivative of this polynomial, at coordinate
i
.- Parameters
i (int) –
i
must be an integer from 0 toself.n - 1
.- Returns
p – The function obtained by differentiating this polynomial with respect to its i-th argument.
- Return type
-
query_coeff
(a)¶ Returns the coefficient of the monomial
lambda x: np.prod(np.power(a, x))
for this Polynomial.
-
remove_terms_with_zero_as_coefficient
()¶ Update
alpha
,c
, andalpha_c
to remove nonconstant terms wherec[i] == 0
.
-
property
sig_rep
¶ Return the signomial representative of the current Polynomial, as well as a list of constraints needed to enforce the relationship between the current Polynomial and the signomial representative.
- Returns
sr (Signomial) – If this Signomial is globally nonnegative, then the current Polynomial is also globally nonnegative.
sr_cons (list of coniclifts Constraints) – If the current Polynomial has nonconstant coefficients (i.e. some entries of
c
are coniclifts Variables), thensr
will also have nonconstant coefficients. In order to enforce the relationship betweensr
and the current Polynomial, we may require constraints betweenc
andsr.c
. Any such constraints are in this list.
-
standard_multiplier
()¶ Returns a Polynomial which is globally nonnegative by construction, for use as a modulator in SAGE hierarchies. The particular polynomial has exponents
alpha = alpha[even_locations(), :]
, and a coefficient vector of all ones.
-
polynomials.
standard_poly_monomials
()¶ - Parameters
n (int) – The dimension of the space over which the constituent Polynomials will be defined.
- Returns
x – An array of length
n
.x[i]
is the Polynomial that evaluates tox[i](z) = z[i]
for all numpy ndarraysz
of lengthn
.- Return type
NumPy ndarray
Example
This function is useful for constructing Polynomials in an algebraic form.
x = standard_poly_monomials(3) f = (x[0] ** 2) * x[2] - 4 * x[1] * x[2] * x[0]