Example #1
0
    def setup_class(self):
        # Scalar matrix.
        self.true_scalar = SparseBoolMat(sparse.coo_matrix(([True],([0],[0])),shape=(1,1)))
        self.false_scalar = SparseBoolMat(sparse.coo_matrix(([],([],[])),shape=(1,1)))

        self.n = 4
        # Vectors.
        self.arr = np.array(self.n*[True])
        self.mixed_arr = np.array(self.n/2*[True, False])
        self.true_vec = SparseBoolMat(sparse.coo_matrix(self.arr))
        self.false_vec = SparseBoolMat(sparse.coo_matrix(~self.arr))

        # Dense matrices.
        self.mat = np.vstack(self.n*[self.arr])
        self.mixed = np.vstack(self.n*[self.mixed_arr])
        self.true_mat = SparseBoolMat(sparse.coo_matrix(self.mat))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~self.mat))
        self.mixed_mat = SparseBoolMat(sparse.coo_matrix(self.mixed))

        # Diagonal matrices.
        self.spdiag = sparse.eye(self.n, self.n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(self.n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V,(I,J)), shape=(self.n, self.n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(self.n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(self.n))
        V = np.array(2*self.n*[True])
        self.x = sparse.coo_matrix((V,(I,J)), shape=(self.n, self.n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty and full matrices.
        mat = np.empty((self.n, self.n), dtype='bool_')
        mat.fill(np.bool_(True))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~mat))
        self.true_mat = SparseBoolMat(sparse.coo_matrix(mat))
Example #2
0
 def test_sparse_add(self):
     assert_equals(self.pos_spmat + self.neg_spmat, self.unknown_spmat)
     assert_equals(self.neg_spmat + Sign.ZERO, self.neg_spmat)
     assert_equals(self.pos_spmat + self.pos_spmat, self.pos_spmat)
     assert_equals(self.unknown_mat + self.neg_mat, self.unknown_mat)
     result = Sign(self.true_mat, SparseBoolMat(self.spmat))
     assert_equals(self.pos_spmat + Sign.NEGATIVE, result)
     assert_equals(self.neg_spmat + self.unknown_mat, self.unknown_mat)
     assert_equals(self.pos_spmat + self.pos_mat, self.pos_mat)
Example #3
0
    def setup_class(self):
        n = 4
        # Vectors.
        self.arr = np.array(n*[True])
        self.mixed_arr = np.array(n/2 * [True, False])
        self.true_vec = BoolMat(self.arr)
        self.false_vec = BoolMat(~self.arr)

        # Dense matrices.
        self.mat = np.vstack(n*[self.arr])
        self.mixed = np.vstack(n*[self.mixed_arr])
        self.true_mat = BoolMat(self.mat)
        self.false_mat = BoolMat(~self.mat)
        self.mixed_mat = BoolMat(self.mixed)

        # Diagonal matrices.
        self.diag = np.eye(n).astype('bool')
        self.diag_mat = BoolMat(self.diag)
        self.spdiag = sparse.eye(n,n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V,(I,J)),shape=(n,n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(n))
        V = np.array(2*n*[True])
        self.x = sparse.coo_matrix((V,(I,J)),shape=(n,n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty sparse matrix.
        self.empty = sparse.coo_matrix(([],([],[])),shape=(n,n))
        self.empty_spmat = SparseBoolMat(self.empty)
Example #4
0
    def setup_class(self):
        self.n = 5
        self.arr = np.atleast_2d(self.n * [np.bool_(True)]).T
        # Vectors
        self.neg_vec = Sign(self.arr, ~self.arr)
        self.pos_vec = Sign(~self.arr, self.arr)
        self.unknown_vec = Sign(self.arr, self.arr)
        self.zero_vec = Sign(~self.arr, ~self.arr)

        # Dense Matrices
        self.true_mat = np.hstack(self.n * [self.arr])
        self.neg_mat = Sign(self.true_mat, ~self.true_mat)
        self.pos_mat = Sign(~self.true_mat, self.true_mat)
        self.unknown_mat = Sign(self.true_mat, self.true_mat)
        self.zero_mat = Sign(~self.true_mat, ~self.true_mat)

        # Sparse Matrices
        self.spmat = sparse.eye(self.n, self.n).astype('bool_')
        self.neg_spmat = Sign(SparseBoolMat(self.spmat), np.bool_(False))
        self.pos_spmat = Sign(np.bool_(False), SparseBoolMat(self.spmat))
        self.unknown_spmat = Sign(SparseBoolMat(self.spmat),
                                  SparseBoolMat(self.spmat))
Example #5
0
class TestSparseBoolMat(object):
    """ Unit tests for the utilities/SparseBoolMat class. """
    @classmethod
    def setup_class(self):
        n = 4
        # Vectors.
        self.arr = np.array(n*[True])
        self.mixed_arr = np.array(n/2 * [True, False])
        self.true_vec = BoolMat(self.arr)
        self.false_vec = BoolMat(~self.arr)

        # Dense matrices.
        self.mat = np.vstack(n*[self.arr])
        self.mixed = np.vstack(n*[self.mixed_arr])
        self.true_mat = BoolMat(self.mat)
        self.false_mat = BoolMat(~self.mat)
        self.mixed_mat = BoolMat(self.mixed)

        # Diagonal matrices.
        self.diag = np.eye(n).astype('bool')
        self.diag_mat = BoolMat(self.diag)
        self.spdiag = sparse.eye(n,n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V,(I,J)),shape=(n,n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(n))
        V = np.array(2*n*[True])
        self.x = sparse.coo_matrix((V,(I,J)),shape=(n,n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty sparse matrix.
        self.empty = sparse.coo_matrix(([],([],[])),shape=(n,n))
        self.empty_spmat = SparseBoolMat(self.empty)

    # Test the | operator.
    def test_or(self):
        assert_equals(self.diag_spmat | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.false_mat, self.diag_mat)
        assert_equals(self.diag_spmat | True, True)
        assert_equals(False | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.coo_spmat, self.x_spmat)

    # Test the & operator.
    def test_and(self):
        assert_equals(self.diag_spmat & self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat & self.false_mat, self.false_mat)
        assert_equals(self.diag_spmat & True, self.diag_spmat)
        assert_equals(False & self.diag_spmat, False)
        assert_equals(self.x_spmat & self.coo_spmat, self.coo_spmat)
        assert_equals(self.diag_spmat & self.coo_spmat, self.empty_spmat)

    # Test the * operator.
    def test_mul(self):
        assert_equals(self.x_spmat * self.x_spmat, self.x_spmat)
        assert_equals(self.diag_spmat * self.coo_spmat, self.coo_spmat)
        assert_equals(BoolMat.mul(self.diag_spmat, (4,4), True, (1,1)), self.diag_spmat)
        assert_equals(BoolMat.mul(False, (1,1), self.diag_spmat, (4,4)), False)
        assert_equals(self.x_spmat * self.coo_spmat, self.x_spmat)
        assert_equals(self.x_spmat * self.empty_spmat, self.empty_spmat)

    # Test the any operator.
    def test_any(self):
        assert self.diag_spmat.any()
        assert self.coo_spmat.any()
        assert not self.empty_spmat.any()
Example #6
0
class TestSparseBoolMat(object):
    """ Unit tests for the utilities/SparseBoolMat class. """
    @classmethod
    def setup_class(self):
        # Scalar matrix.
        self.true_scalar = SparseBoolMat(sparse.coo_matrix(([True],([0],[0])),shape=(1,1)))
        self.false_scalar = SparseBoolMat(sparse.coo_matrix(([],([],[])),shape=(1,1)))

        self.n = 4
        # Vectors.
        self.arr = np.array(self.n*[True])
        self.mixed_arr = np.array(self.n/2*[True, False])
        self.true_vec = SparseBoolMat(sparse.coo_matrix(self.arr))
        self.false_vec = SparseBoolMat(sparse.coo_matrix(~self.arr))

        # Dense matrices.
        self.mat = np.vstack(self.n*[self.arr])
        self.mixed = np.vstack(self.n*[self.mixed_arr])
        self.true_mat = SparseBoolMat(sparse.coo_matrix(self.mat))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~self.mat))
        self.mixed_mat = SparseBoolMat(sparse.coo_matrix(self.mixed))

        # Diagonal matrices.
        self.spdiag = sparse.eye(self.n, self.n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(self.n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V,(I,J)), shape=(self.n, self.n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(self.n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(self.n))
        V = np.array(2*self.n*[True])
        self.x = sparse.coo_matrix((V,(I,J)), shape=(self.n, self.n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty and full matrices.
        mat = np.empty((self.n, self.n), dtype='bool_')
        mat.fill(np.bool_(True))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~mat))
        self.true_mat = SparseBoolMat(sparse.coo_matrix(mat))

    def test_or(self):
        """
        Test the | operator.
        """
        assert_equals(self.diag_spmat | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.false_mat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.true_mat, self.true_scalar)
        assert_equals(self.false_mat | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.coo_spmat, self.x_spmat)

    def test_and(self):
        """
        Test the & operator.
        """
        assert_equals(self.diag_spmat & self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat & self.false_mat, self.false_scalar)
        assert_equals(self.diag_spmat & self.true_mat, self.diag_spmat)
        assert_equals(self.false_mat & self.diag_spmat, self.false_scalar)
        assert_equals(self.x_spmat & self.coo_spmat, self.coo_spmat)
        assert_equals(self.diag_spmat & self.coo_spmat, self.false_scalar)

    def test_mul(self):
        """
        Test the * operator.
        """
        assert_equals(self.x_spmat * self.x_spmat, self.x_spmat)
        assert_equals(self.diag_spmat * self.coo_spmat, self.coo_spmat)
        assert_equals(self.diag_spmat * self.true_mat, self.true_scalar)
        assert_equals(self.false_mat * self.diag_spmat, self.false_scalar)
        assert_equals(self.x_spmat * self.coo_spmat, self.x_spmat)
        assert_equals(self.x_spmat * self.false_mat, self.false_scalar)

    def test_any(self):
        """
        Test the any operator.
        """
        assert self.diag_spmat.any()
        assert self.coo_spmat.any()

    def test_transpose(self):
        """
        Test the transpose method.
        """
        assert_equals(self.diag_spmat.T, self.diag_spmat)
        assert_equals(self.false_scalar.T, self.false_scalar)
        size = self.mixed_mat.value.shape
        assert_equals(self.mixed_mat.T.value.shape, (size[1], size[0]))
Example #7
0
    def setup_class(self):
        # Scalar matrix.
        self.true_scalar = SparseBoolMat(
            sparse.coo_matrix(([True], ([0], [0])), shape=(1, 1)))
        self.false_scalar = SparseBoolMat(
            sparse.coo_matrix(([], ([], [])), shape=(1, 1)))

        self.n = 4
        # Vectors.
        self.arr = np.array(self.n * [True])
        self.mixed_arr = np.array(self.n / 2 * [True, False])
        self.true_vec = SparseBoolMat(sparse.coo_matrix(self.arr))
        self.false_vec = SparseBoolMat(sparse.coo_matrix(~self.arr))

        # Dense matrices.
        self.mat = np.vstack(self.n * [self.arr])
        self.mixed = np.vstack(self.n * [self.mixed_arr])
        self.true_mat = SparseBoolMat(sparse.coo_matrix(self.mat))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~self.mat))
        self.mixed_mat = SparseBoolMat(sparse.coo_matrix(self.mixed))

        # Diagonal matrices.
        self.spdiag = sparse.eye(self.n, self.n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(self.n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V, (I, J)), shape=(self.n, self.n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(self.n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(self.n))
        V = np.array(2 * self.n * [True])
        self.x = sparse.coo_matrix((V, (I, J)), shape=(self.n, self.n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty and full matrices.
        mat = np.empty((self.n, self.n), dtype='bool_')
        mat.fill(np.bool_(True))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~mat))
        self.true_mat = SparseBoolMat(sparse.coo_matrix(mat))
Example #8
0
class TestSparseBoolMat(object):
    """ Unit tests for the utilities/SparseBoolMat class. """
    @classmethod
    def setup_class(self):
        # Scalar matrix.
        self.true_scalar = SparseBoolMat(
            sparse.coo_matrix(([True], ([0], [0])), shape=(1, 1)))
        self.false_scalar = SparseBoolMat(
            sparse.coo_matrix(([], ([], [])), shape=(1, 1)))

        self.n = 4
        # Vectors.
        self.arr = np.array(self.n * [True])
        self.mixed_arr = np.array(self.n / 2 * [True, False])
        self.true_vec = SparseBoolMat(sparse.coo_matrix(self.arr))
        self.false_vec = SparseBoolMat(sparse.coo_matrix(~self.arr))

        # Dense matrices.
        self.mat = np.vstack(self.n * [self.arr])
        self.mixed = np.vstack(self.n * [self.mixed_arr])
        self.true_mat = SparseBoolMat(sparse.coo_matrix(self.mat))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~self.mat))
        self.mixed_mat = SparseBoolMat(sparse.coo_matrix(self.mixed))

        # Diagonal matrices.
        self.spdiag = sparse.eye(self.n, self.n).astype('bool')
        self.diag_spmat = SparseBoolMat(self.spdiag)

        # Reverse diag COO sparse matrix.
        vals = range(self.n)
        I = np.array(vals)
        vals.reverse()
        J = np.array(vals)
        V = self.arr
        self.coo = sparse.coo_matrix((V, (I, J)), shape=(self.n, self.n))
        self.coo_spmat = SparseBoolMat(self.coo)

        # X pattern sparse matrix.
        vals = range(self.n)
        I = np.array(vals + vals)
        vals.reverse()
        J = np.array(vals + range(self.n))
        V = np.array(2 * self.n * [True])
        self.x = sparse.coo_matrix((V, (I, J)), shape=(self.n, self.n))
        self.x_spmat = SparseBoolMat(self.x)

        # Empty and full matrices.
        mat = np.empty((self.n, self.n), dtype='bool_')
        mat.fill(np.bool_(True))
        self.false_mat = SparseBoolMat(sparse.coo_matrix(~mat))
        self.true_mat = SparseBoolMat(sparse.coo_matrix(mat))

    def test_or(self):
        """
        Test the | operator.
        """
        assert_equals(self.diag_spmat | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.false_mat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.true_mat, self.true_scalar)
        assert_equals(self.false_mat | self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat | self.coo_spmat, self.x_spmat)

    def test_and(self):
        """
        Test the & operator.
        """
        assert_equals(self.diag_spmat & self.diag_spmat, self.diag_spmat)
        assert_equals(self.diag_spmat & self.false_mat, self.false_scalar)
        assert_equals(self.diag_spmat & self.true_mat, self.diag_spmat)
        assert_equals(self.false_mat & self.diag_spmat, self.false_scalar)
        assert_equals(self.x_spmat & self.coo_spmat, self.coo_spmat)
        assert_equals(self.diag_spmat & self.coo_spmat, self.false_scalar)

    def test_mul(self):
        """
        Test the * operator.
        """
        assert_equals(self.x_spmat * self.x_spmat, self.x_spmat)
        assert_equals(self.diag_spmat * self.coo_spmat, self.coo_spmat)
        assert_equals(self.diag_spmat * self.true_mat, self.true_scalar)
        assert_equals(self.false_mat * self.diag_spmat, self.false_scalar)
        assert_equals(self.x_spmat * self.coo_spmat, self.x_spmat)
        assert_equals(self.x_spmat * self.false_mat, self.false_scalar)

    def test_any(self):
        """
        Test the any operator.
        """
        assert self.diag_spmat.any()
        assert self.coo_spmat.any()

    def test_transpose(self):
        """
        Test the transpose method.
        """
        assert_equals(self.diag_spmat.T, self.diag_spmat)
        assert_equals(self.false_scalar.T, self.false_scalar)
        size = self.mixed_mat.value.shape
        assert_equals(self.mixed_mat.T.value.shape, (size[1], size[0]))