Search code examples
pythoncvxpy

CVXPY: possible bug in sum_squares when handling nested sums?


As part of a larger project, I end up with convex expressions like ((x_0 + x_1)^2 + x_2^2:

from cvxpy import Variable, sum_squares, sum

target = Variable(3, nonneg=True, name='Target')
sum(target)
sum_squares(target)

combo = [sum([target[0], target[1]]), target[2]]
sum(combo)
sum_squares(combo)

sum(combo) works fine, but sum_squares(combo) throws the following error

ValueError: setting an array element with a sequence.

despite both atoms being scalar functions of with array inputs. Is there a better way to rewrite this?


Solution

  • sum_squares does not take array input. The input needs to be a single CVXPY Expression. Use vstack, hstack, or bmat to combine multiple Expressions into a single vector or matrix Expression.