I am trying to solve an optimization problem using CVXPY where I need to form a block matrix as a constraint. The block matrix is given by:
$$M = \begin{bmatrix} Q & \mathbf{b} \ \mathbf{b}^T & 1 \end{bmatrix}$$
where Q
is a CVXPY variable representing a symmetric matrix, h
is a NumPy array, and 1
is a scalar. I am trying to form this block matrix using the cp.bmat
function in CVXPY as follows:
block_matrix = cp.bmat([[Q, h.reshape(-1, 1)], [h, 1]])
However, I am getting the following error:
All the input dimensions except for axis 0 must match exactly.
I have already reshaped h
to a column vector using reshape(-1, 1)
because CVXPY expects 2D arrays for matrix operations. I am not sure why I am getting this error and how to fix it. Any help would be greatly appreciated.
Here is the complete relevant code for reference:
import cvxpy as cp
import numpy as np
# Define the size of the matrix Q
n = 21
# Define the variables
Q = cp.Variable((n, n), symmetric=True)
# Define the h vector
h = np.array([0.01 * k**2 for k in range(-10, 11)])
# Form the block matrix
block_matrix = cp.bmat([[Q, h.reshape(-1, 1)], [h, 1]])
In case it wasn't obvious enough
# Form the block matrix
block_matrix = cp.bmat([[Q, h.reshape(-1, 1)], [h.reshape(1, -1), np.ones((1,1))]])