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 a MVar, for example, MVar.fromlist() .
Properties
Obtain the transpose of MVar
Number of MVar dimensions
Shape of MVar
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
Static Methods
Wrap a set of variables as a MVar
Wrap a variable as a MVar
Methods
Return a copy of the current MVar
Obtain the diagonal elements of the matrix MVar
Obtain the attribute values associated with MVar
Obtain the unique variable contained in the current MVar
Return a MVar with the same data but a new shape
Set the attribute values associated with MVar
Return a linear expression that sums all variables in MVar
Return an array of all variables in the current MVar
The transpose of a matrix variable (MVar)
- fromlist(li)¶
Wrap a set of variables as a MVar
- Parameters
li – List of variables
example:
m = Model() mat = MVar.fromlist([x0, x1, x2, x3]).reshape(2, 2)
- fromvar(var)¶
Wrap a variable as a 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 a 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())