8.6.16. QuadExpr

class QuadExpr

Represents a quadratic expression. A quadratic expression consists of 0 or more quadratic terms and a linear expression. A quadratic expression can be obtained by arithmetic operations. For example:

# the square of the variable
x ** 2
# variable multiplication
x * y
# linear expression multiply a variable
linExpr * x

Methods

__init__()

Construct a quadratic expression

add()

Add all terms of another expression to the current quadratic expression

addConstant()

Add a value to the constant term of the quadratic expression

addTerms()

Add one or more term(s)

clear()

Clear all included terms and set constant to 0

copy()

Return a copy of a quadratic expression

getCoeff()

Obtain the coefficient of a quadratic term from expression

getConstant()

Obtain the constant term of a quadratic expression

getLinExpr()

Get a linear expression contained in a quadratic expression

getValue()

After solving the problem, obtain the value of the quadratic expression

getVar1()

Get the first variable of a quadratic term in a quadratic expression

getVar2()

Get the second variable of a quadratic term in a quadratic expression

remove()

Delete some terms contained in an expression

size()

Obtain the number of quadratic terms, excluding linear terms and constant terms

__init__(expr=None)

Construct a quadratic expression

Parameters

expr=None – The initial value of a quadratic expression, which can be a constant, a variable, a linear expression, or another quadratic expression.

example:

QuadExpr()
QuadExpr(1)
QuadExpr(x)
QuadExpr(2 * x + y)
QuadExpr(2 * x * x)
add(expr, mult=1.0)

Add all terms of another expression to the current quadratic expression

Parameters
  • expr – Another expression

  • mult=1.0 – Multiplier. Default value: 1.0.

example:

quadExpr.add(linExpr, -1)
quadExpr.add(quadExpr1, -1)
addConstant(c)

Add a value to the constant term of the quadratic expression

Parameters

c – The value to be added. A negative number indicates that value should be subtracted.

example:

quadExpr.addConstant(-quadExpr.getConstant())
addTerms(coeffs, vars, vars2=None)

Add one or more term(s)

Parameters
  • coeffs – The coefficient of the term(s) to be added, which may be a number or an array.

  • vars – The variable of the term(s) to be added, which can be a single variable or an array.

  • vars2=None – If it is not None, it indicates the second variable of the quadratic item, which can be a single variable or an array.

example:

quadExpr.addTerms([1, 2], [x, y])
quadExpr.addTerms(1, x)
quadExpr.addTerms(1, x, y)
clear()

Clear all included terms and set constant to 0

example:

quadExpr = 2 * x * x +3 * y +1
quadExpr.clear()
print(quadExpr.size() == 0)
print(quadExpr.getLinExpr().size() == 0)
print(quadExpr.getConstant() == 0)
copy()

Return a copy of a quadratic expression.

example:

another = quadExpr.copy()
getCoeff(index)

Obtain the coefficient of a quadratic term from expression.

Parameters

index – The index of term

example:

quadExpr = 2 * x + 1 * y + 3 * z * z
print(quadExpr.getCoeff(0) == 2)
getConstant()

Obtain the constant term of a quadratic expression.

example:

quadExpr.addConstant(-quadExpr.getConstant())
getLinExpr()

Get a linear expression contained in a quadratic expression

example:

quadExpr = 2 * x * x + 3 * y + 1
print(quadExpr.getLinExpr().size() == 1)
print(quadExpr.getLinExpr().getConstant() == 1)
getValue()

After solving the problem, obtain the value of the quadratic expression

example:

m.optimize()
quadExpr = 2 * x * x + y
print(quadExpr.getValue())
getVar1(index)

Get the first variable of a quadratic term in a quadratic expression.

Parameters

index – index of quadratic term in expression

example:

quadExpr = 2 * x + 1 * y + 3 * x * y
print(quadExpr.getVar1(0).sameAs(x))
getVar2(index)

Get the second variable of a quadratic term in a quadratic expression.

Parameters

index – index of quadratic term in expression

example:

quadExpr = 2 * x + 1 * y + 3 * x * y
print(quadExpr.getVar2(0).sameAs(y))
remove(item)

Delete some terms contained in an expression

Parameters

item – If item is a number, the quadratic term whose index is item is deleted. If item is a variable, delete all terms that contain this variable, including quadratic and linear terms.

example:

quadExpr = 2 * x * x +3 * y + 4 * x
quadExpr.remove(1)
quadExpr.remove(x)
print(quadExpr.size() == 0)
print(quadExpr.getLinExpr().size() == 0)
size()

Obtain the number of quadratic terms, excluding linear terms and constant terms.

example:

quadExpr = 2 * x * x + 3 * y + 1
print(quadExpr.size() == 1)