Gurobi linear matrix expression object. A linear matrix expression results from an arithmetic operation with an MVar object. A common example is a matrix-vector product, where the matrix is a NumPy ndarray or a SciPy sparse matrix and the vector is a Gurobi MVar object. 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),
multiplication by a constant (e.g.
expr = 2 * A @ x), and
point-wise multiplication with an ndarray or a sparse matrix. An
MLinExpr object containing empty expressions can be created
MLinExpr object has a
shape representing its
size that counts the total number of elements,
ndim that gives the number of dimensions. These
properties lean on their counterparts in NumPy's ndarray class.
When working with
MLinExpr objects, you need to make sure that
the operands' shapes are compatible. For matrix multiplication, we
follow the rules of Python's matrix multiplication operator: both
operands need to have at least one dimension, and their inner
dimensions must agree. For more information we refer you to Python's
documentation. Other binary operations such as addition and
multiplication are straightforward to understand if both operands have
the same shape: the operation is applied point wise on the matching
indices. For operands that have different shapes, the arithmetic
follows NumPy's broadcasting rules. We refer you to the NumPy
documentation for more information.
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.