Search code examples
pythonnumpynumpy-ndarraycvxopt

How to perform operations on cvxopt-matrices a la numpy?


I am working with cvxopt matrices in order to use them in picos library. In general I want to take a matrix, evaluate it on a certain vector, subtract something, then take the biggest absolute value of its entries

import picos as pic
import cvxopt as cvx
import numpy as np

(...)

P = pic.Problem()
theta = P.add_variable('theta', size=k, vtype='continuous', lower=-10, upper=10)
theta

P.add_constraint(max(abs(M*theta - b)) <= 5)
P.minimize(theta)

(Here b is some vector treated as cvxopt matrix.) However, the error I get is the following:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-8884e5cb14dc> in <module>
      3 theta
      4 
----> 5 P.add_constraint(max(abs(M*theta - b.T)) < 45)
      6 P.minimize(theta)
      7 

TypeError: 'Norm' object is not iterable

I was wondering if there is an alternative way of making these computations that would be acceptable to cvxopt?


Solution

  • (Never really used this lib apart from smaller experiments years ago)

    This looks like the culprit is the classic case of hidden-magic in those highly-complex automatic-transformation modelling system tools.

    • picos overloads abs in abs(M*theta - b)
      • see: doc
      • this results in type Norm (a picos-based type)
    • picos probably does not overload max in max(abs(M*theta - b.T))
      • python's max-operator (not something customized from picos!) will be used which is based on linear-search on some iterable
      • the iterable here would be the Norm object; but it's not iterable as the error shows

    See also: list of overloaded operators

    It seems to me, this feature max is missing. You can linearize it manually, but well... that's annoying.

    If you don't need something special of picos, cvxpy is very similar and also supports abs and max (and is based on scipy's sparse-matrices + numpy-arrays; thank god!).