8.6.20. MQuadExpr

class MQuadExpr

Represents a multidimensional array consisting of QuadExprs. Generally, It is generated by arithmetic operations among constant, Var, MVar, LinExpr, QuadExpr and other types. For example:

# multidimensional array linear expression multiplied by a variable
mLinExpr * x
# matrix multiplication of two multidimensional array variables
mvar1 @ mvar2

Properties

ndim

Number of MQuadExpr dimensions

shape

Shape of the MQuadExpr

size

Number of expressions contained in the QuadExpr

ndim

Number of MQuadExpr dimensions

shape

Shape of the MQuadExpr

size

Number of expressions contained in the QuadExpr

Methods

zeros()

Return a MQuadExpr of the specified shape containing empty expressions

clear()

Perform the clear operation on all contained quadratic expressions, that is, all contained quadratic expressions become empty expressions

copy()

Return a copy of the MQuadExpr

getValue()

After the problem is solved, the values of all quadratic expressions are returned

item()

Get the unique expression contained in the current MQuadExpr

sum()

Sum all included expressions and return the summed result

static zeros(shape)

Return a MQuadExpr of the specified shape containing empty expressions

Parameters

shape – The shape to be specified.

example:

mQuadExpr = MQuadExpr.zeros((2, 2))
mQuadExpr += x * x
print(mQuadExpr[0, 0].item().size() == 1)
clear()

Perform the clear operation on all contained quadratic expressions, that is, all contained quadratic expressions become empty expressions.

example:

mQuadExpr.clear()
print(mQuadExpr[0, 0].item().size() == 0)
copy()

Return a copy of the MQuadExpr.

example:

another = mQuadExpr.copy()
getValue()

After the problem is solved, the values of all quadratic expressions are returned.

example:

m.optimize()
print(mQuadExpr.getValue()[0, 0])
item()

Get the unique expression contained in the current MQuadExpr.

example:

mat = m.addMVar((2, 2))
mQuadExpr = mat * mat[0, 0].item()
first = mQuadExpr[0, 0]
print(type(first))
print(type(first.item()))

Note

An exception is thrown if the current MQuadExpr contains more than one expression.

sum(axis=None)

Sum all included expressions and return the summed result.

Parameters

axis=None – Sum along the axis

example:

mat = m.addMVar((2, 2))
mQuadExpr = mat * mat[0, 0].item()
print(mQuadExpr.sum().size == 1)