Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building diagonal block matrix with some blocks as variable CVXPY

I want to generate a (diagonal) block matrix (preferably sparse) in CVXPY.

Some blocks can be eye(m) or anything but I have a block which is:

from cvxopt import *
import cvxpy as cvx
import numpy as np
import scipy
W = cvx.Variable(m,1)
W_diag = cvx.diag(W)

Then I tried to form the block diagonal matrix with W_diag as a block, for instance, by:

T = scipy.sparse.block_diag((scipy.sparse.eye(m1).todense(), cvx.diag(W))

and I got the following error:

TypeError: no supported conversion for types: (dtype('float64'), dtype('O'))

What can I do? Other ways? I want to use matrix T in a constraint for CVXPY later on.

like image 958
hoot Avatar asked Sep 14 '25 03:09

hoot


1 Answers

You can't use CVXPY objects in SciPy and NumPy functions. You need to create the block diagonal matrix using CVXPY. This code would work for your example:

import cvxpy as cvx
import numpy as np
W = cvx.Variable(m)
B = np.ones(m)
T = cvx.diag(cvx.vstack(B, W))

There's no block_diag function in CVXPY right now, but I can add one if it would still be helpful for you.

like image 70
steven Avatar answered Sep 16 '25 19:09

steven