Skip to content

Commit 6ea1333

Browse files
Transurgeonclaude
andauthored
Propagate initial values to reduced variables for diag attribute (#154)
When CvxAttr2Constr creates reduced variables for dimension-reducing attributes (e.g. diag=True), the initial value was not propagated to the reduced variable, causing NLP solvers to fail. This mirrors the existing value propagation already done for parameters. Also handle sparse diagonal matrices in lower_value() by using .diagonal() instead of np.diag() which doesn't accept sparse input. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent de847aa commit 6ea1333

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

cvxpy/reductions/cvx_attr2constr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def lower_value(variable, value=None) -> np.ndarray:
120120
if attributes_present([variable], SYMMETRIC_ATTRIBUTES):
121121
return value[np.triu_indices(variable.shape[0])]
122122
elif variable.attributes['diag']:
123-
return np.diag(value)
123+
return value.diagonal() if sp.issparse(value) else np.diag(value)
124124
elif variable.attributes['sparsity']:
125125
if full_size:
126126
return np.asarray(value)[variable.sparse_idx]
@@ -217,6 +217,8 @@ def apply(self, problem):
217217

218218
reduced_var = Variable(n, var_id=var.id, **new_attr)
219219
reduced_var.set_leaf_of_provenance(var)
220+
if var.value is not None:
221+
reduced_var.value = lower_value(var)
220222
id2new_var[var.id] = reduced_var
221223
obj = build_dim_reduced_expression(var, reduced_var)
222224
elif new_var:

0 commit comments

Comments
 (0)