8.7.6. MVar

class MVar

A multidimensional array of variables. Generally, it is returned by Model.addMVar() . You can also wrap an existing array to obtain an MVar, for example, MVar.fromlist() .

Properties

T

Obtain the transpose of MVar

ndim

Number of MVar dimensions

shape

Shape of MVar

size

Number of variables contained in MVar

T

Obtain the transpose of MVar

ndim

Number of MVar dimensions

shape

Shape of MVar

size

Number of variables contained in MVar

Methods

fromlist()

Wrap a set of variables as an MVar

fromvar()

Wrap a variable as an MVar

copy()

Return a copy of the current MVar

diagonal()

Obtain the diagonal elements of the matrix MVar

getAttr()

Obtain the attribute values associated with MVar

item()

Obtain the unique variable contained in the current MVar

reshape()

Return an MVar with the same data but a new shape

setAttr()

Set the attribute values associated with MVar

sum()

Return a linear expression that sums all variables in MVar

tolist()

Return an array of all variables in the current MVar

transpose()

The transpose of a matrix variable (MVar)

static fromlist(li)

Wrap a set of variables as an MVar

Parameters

li – List of variables

example:

m = Model()
mat = MVar.fromlist([x0, x1, x2, x3]).reshape(2, 2)
static fromvar(var)

Wrap a variable as an MVar

Parameters

var – Variables to be packaged

example:

mat11 = MVar.fromvar(x)
copy()

Return a copy of the current MVar.

example:

mat1 = mat.copy()
diagonal()

Obtain the diagonal elements of the matrix MVar.

example:

print(mat.diagonal())
getAttr(attrname)

Obtain the attribute values associated with MVar.

Parameters

attrname – The name of the attribute.

example:

m = Model()
mat = m.addMVar((2, 2))
print(mat.lb)
print(mat.getAttr(MDO.Attr.LB))

Note

Attribute can also be read and written directly through object attributes; in this case, the attribute name is case-insensitive.

item()

Obtain the unique variable contained in the current MVar.

example:

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

Note

If the current MVar contains more than one variable, an exception is thrown.

reshape(*shape)

Return an MVar with the same data but a new shape.

Parameters

*shape

New shape

example:

m = Model()
mat = m.addMVar((2, 2))
x = mat.reshape(1, 4)  # default to fold along rows
x_c = mat.reshape(-1, order='C')  # fold along rows
x_f = mat.reshape(-1, order='F')  # fold along columns
setAttr(attrname, attrvalues)

Set the attribute values associated with MVar.

Parameters
  • attrname – The name of the attribute to be set.

  • attrvalues – The new value of the attribute to be set. Can be scalar or array.

example:

m = Model()
mat = m.addMVar((2, 2))
mat.lb = [1.0, 2.0, 3.0, 4.0]
mat.setAttr(MDO.Attr.LB, 5.0)

Note

Attributes can also be read and written directly through object attributes; in this case, the attribute name is case-insensitive.

sum(axis=None)

Return a linear expression that sums all variables in MVar

Parameters

axis=None – Sum along the specified axis

example:

linExpr = mat.sum()
tolist()

Return an array of all variables in the current MVar

example:

mat = m.addMVar((2,))
x = mat.tolist()
print(x[0] ** 2 + 2 * x[0] * x[1] + x[1] ** 2)
transpose()

The transpose of a matrix variable (MVar)

example:

print(mat.transpose())