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. Arithmetic operations supported on MQuadExpr objects are addition, subtraction (e.g., expr = x @ A @ x - y @ B @ y), and multiplication by a constant (e.g. expr = 2 * x @ A @ y).

An MQuadExpr object has a shape representing its dimensions, a size that counts the total number of elements, and an ndim that gives the number of dimensions. These properties lean on their counterparts in NumPy's ndarray class.

When working with MQuadExpr objects, you need to make sure that the operands' shapes are compatible. The first step in building an MQuadExpr is often to build an MLinExpr (e.g., |expr = x @ A @ x|), so we refer you to the discussion of MLinExpr shape rules first. Rules for other operations on MQuadExpr objects are generally similar to those for MLinExpr objects, and both classes follow NumPy rules, so we refer you to the NumPy documentation for details.

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.