Skip to content

Commit 587f096

Browse files
author
Jonas Breuling
committed
Ensure sparsity nnz stays the same and removed useless returns.
1 parent 02c7bac commit 587f096

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/bindings.cpp.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void PyQOCOSolver::update_vector_data(py::object cnew, py::object bnew, py::obje
274274
hnew_ptr = (QOCOFloat *)buf.ptr;
275275
}
276276

277-
return qoco_update_vector_data(this->_solver, cnew_ptr, bnew_ptr, hnew_ptr);
277+
qoco_update_vector_data(this->_solver, cnew_ptr, bnew_ptr, hnew_ptr);
278278
}
279279

280280
void PyQOCOSolver::update_matrix_data(py::object Pxnew, py::object Axnew, py::object Gxnew)
@@ -310,7 +310,7 @@ void PyQOCOSolver::update_matrix_data(py::object Pxnew, py::object Axnew, py::ob
310310
Gxnew_ptr = (QOCOFloat *)buf.ptr;
311311
}
312312

313-
return qoco_update_matrix_data(this->_solver, Pxnew_ptr, Axnew_ptr, Gxnew_ptr);
313+
qoco_update_matrix_data(this->_solver, Pxnew_ptr, Axnew_ptr, Gxnew_ptr);
314314
}
315315

316316
PYBIND11_MODULE(@QOCO_EXT_MODULE_NAME@, m)

src/qoco/interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,22 @@ def update_matrix_data(self, P=None, A=None, G=None):
144144
if not isinstance(P, np.ndarray):
145145
P = np.array(P)
146146
P = P.astype(np.float64)
147+
if P.shape[0] != self.P.nnz:
148+
raise ValueError(f"P size must be {self.P.nnz}")
147149

148150
if A is not None:
149151
if not isinstance(A, np.ndarray):
150152
A = np.array(A)
151153
A = A.astype(np.float64)
154+
if A.shape[0] != self.A.nnz:
155+
raise ValueError(f"A size must be {self.A.nnz}")
152156

153157
if G is not None:
154158
if not isinstance(G, np.ndarray):
155159
G = np.array(G)
156160
G = G.astype(np.float64)
161+
if G.shape[0] != self.G.nnz:
162+
raise ValueError(f"G size must be {self.G.nnz}")
157163

158164
return self._solver.update_matrix_data(P, A, G)
159165

tests/test_update_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,19 @@ def test_update_vector_data_float_conversion(setup_qoco):
207207
prob.update_vector_data(c=c_int, b=b_int, h=h_int)
208208
res = prob.solve()
209209
assert res.status in ["QOCO_SOLVED", "QOCO_SOLVED_INACCURATE"]
210+
211+
def test_update_matrix_data_invalid_size(setup_qoco):
212+
"""Test that updating with wrong size matrix data raises error."""
213+
prob = setup_qoco
214+
215+
# Test P with wrong size
216+
with pytest.raises(ValueError, match="P size must be"):
217+
prob.update_matrix_data(P=np.array([1, 2, 3]))
218+
219+
# Test A with wrong size
220+
with pytest.raises(ValueError, match="A size must be"):
221+
prob.update_matrix_data(A=np.array([1, 2]))
222+
223+
# Test G with wrong size
224+
with pytest.raises(ValueError, match="G size must be"):
225+
prob.update_matrix_data(G=np.array([1, 2]))

0 commit comments

Comments
 (0)