I'm using the CVXPY code here. I want to run it on Python 2.7 instead of Python 3. The operator @ seems to work on Python 3. To make it work for python 2.7, I revised the code as
import cvxpy as cp
import numpy as np
n = 3
p = 3
np.random.seed(1)
C = np.random.randn(n, n)
A = []
b = []
for i in range(p):
A.append(np.random.randn(n, n))
b.append(np.random.randn())
X = cp.Variable((n,n), symmetric=True)
# The operator >> denotes matrix inequality.
constraints = [X >> 0]
prob = cp.Problem(cp.Minimize(np.matmul(C,X)), constraints)
prob.solve()
where I used numpy.matmul instead of @. However, it gives me this error "ValueError: matmul: Input operand 1 does not have enough dimensions"
My question is how to run this code here successfully in python 2.7 (instead of Python 3).
You need to use cvxpy operators on cvxpy variables, in other words you can't do np.matmul
with a cvxpy variable. You can just use the *
operator. cvxpy will treat this as matrix multiplication. Try this,
C = np.random.randn(2, n)
C * X
and you'll get:
Expression(AFFINE, UNKNOWN, (2, 3))