8.6.17. MLinExpr

class MLinExpr

Represents a multidimensional array consisting of LinExprs. Generally, it is generated by arithmetic operations among constant, Var, MVar, MLinExpr or other types. For example:

# Multi-dimensional array variable plus a constant
mvar + 1
# product of variable and numpy array
var * numpy.ones((1, 1))

Properties

ndim

Number of MLinExpr dimensions

shape

Shape of the MLinExpr

size

Number of expressions contained in the MLinExpr

Methods

clear()

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

copy()

Return a copy of the MLinExpr

getValue()

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

item()

Get the unique expression contained in the current MLinExpr

sum()

Sum all included expressions and return the summed result

zeros()

Return a MLinExpr of the specified shape, which contains empty expressions

clear()

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

example:

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

Return a copy of the MLinExpr.

example:

another = mLinExpr.copy()
getValue()

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

example:

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

Get the unique expression contained in the current MLinExpr.

example:

mat = m.addMVar((2, 2))
mLinExpr = mat + 1
first = mLinExpr[0, 0]
print(type(first))
print(type(first.item()))

Note

An exception is thrown if the current MLinExpr 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))
mLinExpr = mat +1
print(mLinExpr.sum().getConstant() == 4)
zeros(shape)

Return a MLinExpr of the specified shape, which contains empty expressions.

Parameters

shape – The shape to be specified.

example:

mLinExpr = MLinExpr.zeros((2, 2))
mLinExpr += x
print(mLinExpr[0, 0].item().size() == 1)