### If you are using Gurobi 10 or later

It is possible to pointwise multiply an array and an MVar object:

$$\begin{align} \begin{bmatrix} a_1 \\ a_2 \\ a_3 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}= \begin{bmatrix} a_1 x_1 \\ a_2 x_2 \\ a_3 x_3 \end{bmatrix}. \end{align}$$

This works natively, using numpy conventions:

import gurobipy as gp

import numpy as np

m = gp.Model()

x = m.addMVar(3)

a = np.array([1.0, 2.0, 3.0])

expr = a * x

### If you are using Gurobi 9.x

In Gurobi versions 9.x, pointwise multiplication required a workaround. Specifically, convert the array to a (sparse) diagonal matrix, then multiply the sparse matrix with the MVar object:

import gurobipy as gp

import numpy as np

import scipy.sparse as sp

m = gp.Model()

x = m.addMVar(3)

a = np.array([1.0, 2.0, 3.0])

A = sp.diags(a)

expr = A @ x

**Note:** Gurobi 9.0 introduced the first version of the Python matrix API. The developers are continually working to improve the usability of the matrix API.

### Further information

- How do I pointwise multiply two MVars with the matrix-friendly Python API?
- How do I multiply two MLinExpr objects together 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 a numpy vector with a (1,) MVar with the matrix-friendly Python API?

## Comments

0 comments

Article is closed for comments.