Search code examples
pythoncvxpy

How to take the 2-norm of a list of variables in CVXPY?


I want to take the 2-norm of a list of variables. How can I convert a sequence to a CVXPY compatible "list" of variables? Is there a way to handle this? Thanks in advance. For example,

    test_a=cvxpy.Variable(1)
    test_b=cvxpy.Variable(1)
    mylist= [test_a,test_b]     
    mynorm=cvxpy.norm(mylist,2) #error thrown for this line


packages/cvxpy/interface/matrix_utilities.py", line 63, in size
return (len(constant[0]), len(constant))

Solution

  • CVXPY functions cannot be applied to lists. You need to use vstack, i.e.,

    mynorm=cvxpy.norm(cvxpy.vstack(*mylist),2)