def tocsc(self, copy=False): """ Convert this matrix to Compressed Sparse Column format Returns ------- CSCMatrix """ from pyomo.contrib.pynumero.sparse.csc import CSCMatrix if self.nnz == 0: return CSCMatrix(self.shape, dtype=self.dtype) else: M, N = self.shape idx_dtype = get_index_dtype((self.col, self.row), maxval=max(self.nnz, M)) row = self.row.astype(idx_dtype, copy=False) col = self.col.astype(idx_dtype, copy=False) indptr = np.empty(N + 1, dtype=idx_dtype) indices = np.empty_like(row, dtype=idx_dtype) data = np.empty_like(self.data, dtype=upcast(self.dtype)) # TODO: check why scipy does this and not coo_tocsc coo_tocsr(N, M, self.nnz, col, row, self.data, indptr, indices, data) x = CSCMatrix((data, indices, indptr), shape=self.shape) if not self.has_canonical_format: x.sum_duplicates() return x
def test_with_data(self): row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([45., 55., 75., 95.]) m = CSCMatrix((data, (row, col)), shape=(4, 4)) data = m.data m2 = self.basic_m._with_data(data) self.assertTrue(np.allclose(m.todense(), m2.todense())) m2 = self.basic_m._with_data(data, copy=False) self.assertTrue(np.allclose(m.todense(), m2.todense()))
def setUp(self): row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4., 5., 7., 9.]) m = CSCMatrix((data, (row, col)), shape=(4, 4)) m.name = 'basic_matrix' self.basic_m = m row = np.array([0, 3, 1, 2, 3]) col = np.array([0, 0, 1, 2, 3]) data = np.array([2., 1., 3., 4., 5.]) m = CSCSymMatrix((data, (row, col)), shape=(4, 4)) m.name = 'basic_sym_matrix' self.basic_sym_m = m
def tocsc(self, copy=False): idx_dtype = get_index_dtype((self.indptr, self.indices), maxval=max(self.nnz, self.shape[0])) indptr = np.empty(self.shape[1] + 1, dtype=idx_dtype) indices = np.empty(self.nnz, dtype=idx_dtype) data = np.empty(self.nnz, dtype=upcast(self.dtype)) csr_tocsc(self.shape[0], self.shape[1], self.indptr.astype(idx_dtype), self.indices.astype(idx_dtype), self.data, indptr, indices, data) from pyomo.contrib.pynumero.sparse.csc import CSCMatrix A = CSCMatrix((data, indices, indptr), shape=self.shape) A.has_sorted_indices = True return A
def test_is_symmetric_numerically(self): test_m = np.array([[2., 0., 0., 1.], [0., 3., 0., 0.], [0., 0., 4., 0.], [1., 0., 0., 5.]]) m = CSCMatrix(test_m) self.assertTrue(_is_symmetric_numerically(m)) self.assertFalse(_is_symmetric_numerically(self.basic_m))
def test_mul_sparse_matrix(self): #from pyomo.contrib.pynumero.sparse.block_matrix import BlockMatrix # test unsymmetric times unsymmetric m = self.basic_m dense_m = m.toarray() res = m * m dense_res = np.matmul(dense_m, dense_m) self.assertFalse(res.is_symmetric) self.assertTrue(np.allclose(res.toarray(), dense_res)) # test symmetric result m = self.basic_m dense_m = m.toarray() res = m.transpose() * m dense_res = np.matmul(dense_m.transpose(), dense_m) self.assertTrue(res.is_symmetric) self.assertTrue(np.allclose(res.toarray(), dense_res)) # test unsymmetric with rectangular m = self.basic_m dense_m2 = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]]) m2 = CSCMatrix(dense_m2) res = m * m2 dense_res = np.matmul(m.toarray(), dense_m2) self.assertFalse(res.is_symmetric) self.assertTrue(np.allclose(res.toarray(), dense_res)) # test unsymmetric with rectangular scipycsr m = self.basic_m dense_m2 = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]]) m2 = csc_matrix(dense_m2) with self.assertRaises(Exception) as context: res = m * m2 # test product with symmetric matrix m = self.basic_m dense_m = m.todense() m2 = self.basic_sym_m dense_m2 = m2.todense() res = m * m2 res_dense = np.matmul(dense_m, dense_m2) self.assertTrue(np.allclose(res.todense(), res_dense)) """
def test_convert_matrix_to_symmetric(self): test_m = np.array([[2., 0., 0., 1.], [0., 3., 0., 0.], [0., 0., 4., 0.], [1., 0., 0., 5.]]) m = CSCMatrix(test_m) sm = _convert_matrix_to_symmetric(m) self.assertTrue(sm.is_symmetric) mm = sm.toarray() self.assertTrue(np.allclose(mm, test_m, atol=1e-6))
def transpose(self, axes=None, copy=False): if axes is not None: raise ValueError(("Sparse matrices do not support " "an 'axes' parameter because swapping " "dimensions is the only logical permutation.")) M, N = self.shape from pyomo.contrib.pynumero.sparse.csc import CSCMatrix return CSCMatrix((self.data, self.indices, self.indptr), shape=(N, M), copy=copy)
def getcol(self, j): from pyomo.contrib.pynumero.sparse.csc import CSCMatrix return CSCMatrix(self.toscipy().getcol(j))