Skip to content

cvxpy/dense-identity-quad-form

Pack Severity Fix Versions
CVXPY info none, edit by hand All versions

CVXPY's quad_form(x, np.eye(n)) allocates a dense square identity matrix to express the sum of the squares of a real vector.

x = cp.Variable(3)
loss = cp.quad_form(x, np.eye(3))
loss = cp.sum_squares(x)

The rule checks a default real CVXPY Variable with a literal vector length and an equally sized NumPy eye or identity call with one argument. It resolves qualified and renamed imports of all three symbols. The variable can be constructed in the call or bound in an unambiguous earlier assignment in the same block.

Use cp.sum_squares(x) to express the same scalar objective without allocating the matrix. Keep quad_form for a quadratic form with a nontrivial matrix.

CVXPY performance guide to quadratic objectives

CVXPY quad_form with a dense identity allocates a square matrix for a sum of squares. Use cvxpy.sum_squares(x).

See all rules in the CVXPY pack.