Try our new documentation site (beta).


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., <span>$</span>Ax + b<span>$</span>). 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 MLinExpr using 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).

An 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, forming A @ x where A has shape (10,3) and x has shape (3,) gives a result with shape (10,).

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: +, +=, -, -=, *, *=, and @. In Python parlance, we've defined the following MLinExpr functions: __add__, __radd__, __iadd__, __sub__, __rsub__, __isub__, __neg__, __mul__, __rmul__, __imul__, __matmul__, and __rmatmul__.

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 (@) was introduced in Python version 3.5; it isn't available from Python 2.7.


Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.