docplex.mp.quad module

class docplex.mp.quad.QuadExpr[source]

Bases: docplex.mp.basic._SubscriptionMixin, docplex.mp.basic.Expr

This class models quadratic expressions. This class is not intended to be instantiated. Quadratic expressions are built either by using operators or by using docplex.mp.model.Model.quad_expr().

clone()[source]

Makes a copy of the quadratic expression and returns it.

Returns:A quadratic expression.
constant

This property is used to get or set the constant part of a quadratic expression

contains_var(dvar)[source]

Checks whether a variable is present in the expression.

Param:dvar (docplex.mp.dvar.Var): A decision variable.
Returns:True if the variable is present in the expression, else False.
Return type:Boolean
get_quadratic_coefficient(var1, var2=None)[source]

Returns the coefficient of a quadratic term in the expression.

Returns the coefficient of the quadratic term var1*var2 in the expression, if any. If the product is not present in the expression, returns 0.

Parameters:
  • var1 – The first variable of the product (an instance of class Var)
  • var2 – the second variable of the product. If passed None, returns the coefficient of the square of var1 in the expression.

Example

Assuming x and y are decision variables and q is the expression 2*x**2 + 3*x*y + 5*y**2, then

q.get_quadratic_coefficient(x) returns 2

q.get_quadratic_coefficient(x, y) returns 3

q.get_quadratic_coefficient(y) returns 5

Returns:The coefficient of one quadratic product term in the expression.
has_quadratic_term()[source]

Returns true if there is at least one quadratic term in the expression.

is_quad_expr()[source]

Returns True if the expression is quadratic

is_separable()[source]

Checks if all quadratic terms are separable.

Returns:True if all quadratic terms are separable.
iter_quad_triplets()[source]

Iterates over quadratic terms.

This iterator returns triplets of the form v1,v2,k, where v1 and v2 are decision variables and k is a number.

Returns:An iterator object.
iter_terms()[source]

Iterates over the linear terms in the quadratic expression.

Equivalent to self.linear_part.iter_terms()

Returns:An iterator over the (variable, coefficient) pairs in the linear part of the expression.

Example

Calling this method on (x^2 +2x+1) will return one pair (x, 2).

linear_part

This property returns the linear part of a quadratic expression.

For example, the linear part of x^2 +2x+1 is (2x+1)

Returns:an instance of docplex.mp.LinearExpr
number_of_quadratic_terms

This property returns the number of quadratic terms.

Counts both the square and product terms.

Examples:

q1 = x**2
q1.number_of_quadratic_terms
>>> 1
q2 = (x+y+1)**2
q2.number_of_quadratic_terms
>>> 3