title: Convert a semidefinite program from CVX to CVXPY
body: <p>I want to convert the following SDP — which just verifies the feasibility of the constraints — from CVX (MATLAB) to CVXPY (Python): </p>
  
  <pre><code>Ah = [1.0058, -0.0058; 1, 0];
  Bh = [-1; 0];
  Ch = [1.0058, -0.0058; -0.9829, 0.0056];
  Dh = [-1; 1];
  
  M = [0, 1;1, 0];
  ni = size(M,1)/2;
  n = size(Ah,1);
  rho = 0.5;
  
  cvx_begin sdp quiet
      variable P(n,n) semidefinite
      variable lambda(ni) nonnegative
      Mblk = M*kron(diag(lambda),eye(2));
      lambda(ni) == 1  % break homogeneity (many ways to do this...)
      [Ah Bh]'*P*[Ah Bh] - rho^2*blkdiag(P,0) + [Ch Dh]'*Mblk*[Ch Dh] &lt;= 0
  cvx_end
  
  
  switch cvx_status
      case 'Solved'
          feas = 1;
      otherwise
          feas = 0;
  end
  </code></pre>
  
  <p>Below is my Python code,</p>
  
  <pre><code>import cvxpy as cvx
  import numpy as np
  import scipy as sp
  
  
  Ah = np.array([[1.0058, -0.0058], [1, 0]])
  Bh = np.array([[-1], [0]])
  Ch = np.array([[1.0058, -0.0058], [-0.9829, 0.0056]])
  Dh = np.array([[-1], [1]])
  
  M = np.array([[0, 1], [1, 0]])
  ni, n = M.shape[0] / 2, Ah.shape[0]
  rho = 0.5
  
  P = cvx.Semidef(n)
  lamda = cvx.Variable()
  
  Mblk = np.dot(M, np.kron(cvx.diag(lamda), np.eye(2)))
  ABh = np.concatenate((Ah, Bh), axis=1)
  CDh = np.concatenate((Ch, Dh), axis=1)
  constraints = [lamda[-1] == 1,
                 np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) +
                 np.dot(CDh.T, np.dot(Mblk, CDh)) &lt;&lt; 0]
  
  prob = cvx.Problem(cvx.Minimize(1), constraints)
  feas = prob.status is cvx.OPTIMAL
  </code></pre>
  
  <p>There are several errors when I run the program.
  1. When I print Mblk, it shows</p>
  
  <blockquote>
    <p>Traceback (most recent call last):</p>
    
    <p>File
    "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py",
    line 2820, in run_code</p>
    
    <p>Out[1]:     exec code_obj in self.user_global_ns, self.user_ns</p>
    
    <p>File "", line 1, in </p>
    
    <p>Mblk</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py",
    line 247, in <strong>call</strong></p>
    
    <p>format_dict, md_dict = self.compute_format_data(result)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py",
    line 157, in compute_format_data</p>
    
    <p>return self.shell.display_formatter.format(result)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/core/formatters.py",
    line 152, in format</p>
    
    <p>data = formatter(obj)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/core/formatters.py",
    line 481, in <strong>call</strong></p>
    
    <p>printer.pretty(obj)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line
    362, in pretty</p>
    
    <p>return _default_pprint(obj, self, cycle)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line
    482, in _default_pprint</p>
    
    <p>p.text(repr(obj))</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line
    1553, in array_repr</p>
    
    <p>', ', "array(")</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py", line
    454, in array2string</p>
    
    <p>separator, prefix, formatter=formatter)</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py", line
    256, in _array2string</p>
    
    <p>'int' : IntegerFormat(data),</p>
    
    <p>File "/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py", line
    641, in <strong>init</strong></p>
    
    <p>max_str_len = max(len(str(maximum.reduce(data))),</p>
    
    <p>File
    "/usr/local/lib/python2.7/dist-packages/cvxpy/constraints/leq_constraint.py",
    line 67, in <strong>nonzero</strong></p>
    
    <p>Raise Exception("Cannot evaluate the truth value of a constraint.")</p>
    
    <p>Exception: Cannot evaluate the truth value of a constraint.</p>
  </blockquote>
  
  <p>When I step to this line,</p>
  
  <pre><code>  constraints = [lamda[-1] == 1,
                     np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) +
                     np.dot(CDh.T, np.dot(Mblk, CDh)) &lt;&lt; 0]
  </code></pre>
  
  <p>it shows</p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File</p>
    
    <p>".../sdp.py", line 22, in </p>
  
  <pre><code>np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) + 
  </code></pre>
    
    <p>ValueError: setting an array element with a sequence.</p>
  </blockquote>
  
  <p>How to fix these problems?</p>