Gurobi linear matrix expression object. A linear matrix expression consists of a matrix-vector product, where the matrix is a NumPy dense matrix or a SciPy sparse matrix and the vector is a Gurobi MVar object, plus an optional constant vector of compatible dimensions (i.e., ). Linear matrix expressions are used to build linear objectives and constraints. They are temporary objects that typically have short lifespans.
You generally build linear matrix expressions using overloaded
operators, typically by multiplying a 2-D matrix (dense or sparse) by
a 1-D MVar object using the Python
matrix multiply (
@) operator (e.g.,
expr = A @ x). You
can also promote an
MVar object to an
arithmetic expressions (e.g.,
expr = x + 1). Most arithmetic
operations are supported on
MLinExpr objects, including
addition and subtraction (e.g.,
expr = A @ x - B @ y), and
multiplication by a constant (e.g.
expr = 2 * A @ x).
MLinExpr object has a
shape, defined similarly to
that of other NumPy ndarray objects. Due to the way it is defined,
the shape will always be 1-dimensional, with the length reflecting the
size of the corresponding matrix-vector result. To give an example,
A @ x where
A has shape
x has shape
(3,) gives a result with shape
When working with
MLinExpr objects, you of course need to make
sure that the shapes are compatible. If you want to form
A @ x, then
A must be a 2-D array,
x must be a
1-D array, and the size of the second dimension of
A must be
equal to the size of
x. Similarly, adding an object into an
MLinExpr object (including another
MLinExpr) requires an
object of the same shape. The one exception is a constant, which is
automatically promoted to have a compatible shape.
The full list of overloaded operators on
MLinExpr objects is as follows:
In Python parlance, we've defined the following
We've also overloaded the comparison operators (==, <=, and >=), to make it easier to build constraints from linear matrix expressions.
Note that the Python matrix multiplication operator (
introduced in Python version 3.5; it isn't available from Python 2.7.