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 call alpha, and the values will be assembled into a vector which we call c.

    If alpha_maybe_c is a NumPy ndarray, then the argument c must also be an ndarray, and c.size must equal alpha_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 function lambda 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 term lambda 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, where alpha[i, j] is the power of variable j in monomial i. The second argument is a numpy array c, where c[i] is the coefficient on the i-th monomial defined by alpha.

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 dictionary alpha_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 dictionary alpha_c.

Type

int

alpha

Has shape (m, n). Entry alpha[i,j] is the power of an implicit variable x[j] appearing in the i-th monomial for this Polynomial. The i-th monomial, in turn, has coefficient c[i].

Type

NumPy ndarray

c

Has shape (m,). The scalar c[i] is this Polynomial’s coefficient on the basis function lambda x: np.prod(np.power(alpha[i, :], x)). It is possible to have c.dtype == object, to allow for coniclifts Variables.

Type

NumPy ndarray

alpha_c

The keys of alpha_c are tuples of length n, containing real numeric types (e.g int, float). These tuples correspond to rows in alpha.

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 have self(x) == f(np.log(x)).

Return type

Signomial

constant_location()

Return the index i so that alpha[i, :] is the zero vector.

even_locations()

Return the largest ndarray evens, so that np.all(alpha[evens,:] % 2 == 0).

property grad

A numpy ndarray of shape (n,) whose entries are Polynomials. For a numpy ndarray x, grad[i](x) is the partial derivative of this Polynomial with respect to coordinate i, evaluated at x. 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 ndarray x, hess[i,j](x) is the (i,j)-th partial derivative of this Polynomial, evaluated at x. 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 to self.n - 1.

Returns

p – The function obtained by differentiating this polynomial with respect to its i-th argument.

Return type

Polynomial

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, and alpha_c to remove nonconstant terms where c[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), then sr will also have nonconstant coefficients. In order to enforce the relationship between sr and the current Polynomial, we may require constraints between c and sr.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 to x[i](z) = z[i] for all numpy ndarrays z of length n.

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]