Try our new documentation site.


Gurobi quadratic matrix expression object. Quadratic matrix expressions are used to build quadratic objective functions and quadratic constraints. They are temporary objects that typically have short lifespans.

You generally build quadratic matrix expressions using overloaded operators. For example, if x is an MVar object and A is a 2-D matrix (dense or sparse), then x @ A @ x and x @ x are both MQuadExpr objects. Most arithmetic operations are support on MQuadExpr objects, including addition and subtraction (e.g., expr = x @ A @ x - y @ B @ y), and multiplication by a constant (e.g. expr = 2 * x @ A @ y).

The full list of overloaded operators on MQuadExpr objects is as follows: +, +=, -, -=, *, *=, and /. In Python parlance, we've defined the following QuadExpr functions: __add__, __radd__, __iadd__, __sub__, __rsub__, __isub__, __neg__, __mul__, __rmul__, and __imul__.

We've also overloaded the comparison operators (==, <=, and >=), to make it easier to build constraints from quadratic expressions.

Note that the Python matrix multiplication operator (@) was introduced in Python version 3.5; it isn't available from Python 2.7.

Note that a quadratic matrix expression always produces a scalar result (a result with shape (1,)). You can add linear terms into a quadratic matrix expression, but for the dimensions to be compatible they must also have shape (1,).


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.