Skip to content

cvxpy/dense-identity-quad-form

Rule dense-identity-quad-form
Pack CVXPY
Severity info
Fix none, edit by hand
Library versions every version

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

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

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

Browse the other rules of the CVXPY pack.