From ad244beba38572d36e090cedbeb5e68af5abd834 Mon Sep 17 00:00:00 2001 From: Mayk Thewessen Date: Fri, 13 Mar 2026 22:22:07 +0100 Subject: [PATCH] perf: use single-step sparse matrix slicing in MatrixAccessor Replace double-slicing pattern A[clabels][:, vlabels] with single-step A[np.ix_(clabels, vlabels)] in MatrixAccessor.A and MatrixAccessor.Q. The double-slice creates an intermediate sparse matrix (selecting rows first, then columns), which allocates temporary storage proportional to the full matrix. np.ix_() performs both row and column selection in a single operation, avoiding the intermediate allocation. Co-Authored-By: Claude Opus 4.6 --- linopy/matrices.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linopy/matrices.py b/linopy/matrices.py index e1489e76..160a5d47 100644 --- a/linopy/matrices.py +++ b/linopy/matrices.py @@ -144,7 +144,7 @@ def A(self) -> csc_matrix | None: if not len(m.constraints): return None A: csc_matrix = m.constraints.to_matrix(filter_missings=False) - return A[self.clabels][:, self.vlabels] + return A[np.ix_(self.clabels, self.vlabels)] @property def sense(self) -> ndarray: @@ -178,4 +178,4 @@ def Q(self) -> csc_matrix | None: expr = m.objective.expression if not isinstance(expr, expressions.QuadraticExpression): return None - return expr.to_matrix()[self.vlabels][:, self.vlabels] + return expr.to_matrix()[np.ix_(self.vlabels, self.vlabels)]