You can natively multiply two MLinExpr objects together following normal matrix multiplication rules. For example, consider the following constraint:
$$\begin{align} y &= (Ax + b)^\top (Ax + b). \end{align}$$
This can be formulated naturally as follows:
import gurobipy as gp
import numpy as np
m = gp.Model()
x = m.addMVar(3)
y = m.addMVar(1)
A = np.random.rand(4,3)
b = np.random.rand(4)
m.addConstr(y == (A @ x + b) @ (A @ x + b))
Further information
- How do I pointwise multiply a numpy vector with a (1,) MVar with the matrix-friendly Python API?
- How do I pointwise multiply two MVars with the matrix-friendly Python API?
- How do I multiply an array and a 2-D MVar object using the matrix-friendly Python API?
- How do I pointwise multiply an array and an MVar with the matrix-friendly Python API?