def test_is_symmetric_dense(self): m = self.block00.toarray() self.assertTrue(is_symmetric_dense(m)) self.assertTrue(is_symmetric_dense(2)) with self.assertRaises(Exception) as context: self.assertTrue(is_symmetric_dense(self.block00))
def test_is_symmetric_dense(self): m = self.block00.toarray() self.assertTrue(is_symmetric_dense(m)) self.assertTrue(is_symmetric_dense(2)) with self.assertRaises(Exception) as context: self.assertTrue(is_symmetric_dense(self.block00))
def test_is_symmetric_dense(self): test_m = np.array([[2., 0., 0., 1.], [0., 3., 0., 0.], [0., 0., 4., 0.], [1., 0., 0., 5.]]) flag = is_symmetric_dense(test_m) self.assertTrue(flag) usm = self.basic_m.toarray() flag = is_symmetric_dense(usm) self.assertFalse(flag)
def __init__(self, arg1, shape=None, dtype=None, copy=False, **kwargs): # check if dense matrix is symmetric if isinstance(arg1, np.ndarray): if not is_symmetric_dense(arg1): raise RuntimeError("ndarray is not symmetric") # keep only lower triangular arg1 = np.tril(arg1) # symmetric matrices don't expand symmetry expand_symmetry = kwargs.pop('expand_symmetry', False) error_msg = "Symmetric matrices only store lower triangular" assert not expand_symmetry, error_msg super(CSCSymMatrix, self).__init__(arg1, shape=shape, dtype=dtype, copy=copy, expand_symmetry=expand_symmetry, **kwargs) # add check to verify square matrix if self.shape[0] != self.shape[1]: raise RuntimeError('A rectangular matrix is not symmetric') # check nnz is less than the full lower triangular if self.nnz > self.shape[0] * (self.shape[0] + 1) / 2: raise RuntimeError( 'CSCSymMatrix only store lower triangular entries. Too many nnz' ) # TODO: check only lower triangular entries # makes sparse matrix symmetric self._symmetric = True