Plotting a matrix of diagonal blocks with some blocks as a CVXPY variable

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

Some blocks can be eye(m)

or whatever, but I have a block that:

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 a block-diagonal matrix with W_diag

as a block, for example by:

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

      

and I got the following error:

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

What can I do? Other methods? I want to use the matrix T

in CVXPY constraint later.

+3


source to share


1 answer


You cannot use CVXPY objects in SciPy and NumPy functions. You need to create a diagonal block matrix using CVXPY. This code will 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 is no function in CVXPY block_diag

, but I can add it if it is useful to you anyway.

+4


source







All Articles