From 7605eb248ad624a7ff5d337acfd66ed9bc484379 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Mon, 8 Jul 2024 15:17:47 -0600 Subject: [PATCH] Fix bugs in creating CSC matricies --- pyomo/repn/plugins/standard_form.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pyomo/repn/plugins/standard_form.py b/pyomo/repn/plugins/standard_form.py index 2052edecffd..10eae5ff099 100644 --- a/pyomo/repn/plugins/standard_form.py +++ b/pyomo/repn/plugins/standard_form.py @@ -561,13 +561,19 @@ def write(self, model): def _create_csc(self, data, index, index_ptr, nnz, nCol): if not nnz: - return scipy.sparse.csc_array( + # The empty CSC has no (or few) rows and a large number of + # columns and no nonzeros: it is faster / easier to create + # the empty CSR on the python side and convert it to CSC on + # the C (numpy) side, as opposed to ceating the large [0] * + # (nCol + 1) array on the Python side and transfer it to C + # (numpy) + return scipy.sparse.csr_array( (data, index, index_ptr), [len(index_ptr) - 1, nCol] - ) + ).tocsc() data = np.fromiter(itertools.chain.from_iterable(data), np.float64, nnz) # data = list(itertools.chain(*data)) - con_index = np.fromiter(itertools.chain.from_iterable(index), np.int32, nnz) + index = np.fromiter(itertools.chain.from_iterable(index), np.int32, nnz) # index = list(itertools.chain(*index)) index_ptr = np.array(index_ptr, dtype=np.int32) A = scipy.sparse.csr_array((data, index, index_ptr), [len(index_ptr) - 1, nCol])