Ejemplo n.º 1
0
def check_generator(Q, symmetrize=False):
    """
    Checks whether Q is a generator.
    """
    is_gen = True
    if not np.allclose(Q.sum(1), 0):
        print("GENERATOR: matrix rows do not sum to 0.")
        is_gen = False
    else:
        print("GENERATOR: matrix rows sum to 0.")
    if np.any(Q[~np.eye(Q.shape[0], dtype=bool)] < 0.0):
        print("GENERATOR: some matrix off-diagonals are negative.")
        is_gen = False
    if np.any(np.diag(Q) > 0.0):
        print("GENERATOR: some matrix diagonals are non-negative.")
        is_gen = False
    if is_symmetric(Q):
        print("GENERATOR: generator is symmetric.")
    else:
        print("GENERATOR: generator is not symmetric.")
        if symmetrize:
            Q = symmetrize_generator(Q)
            assert is_symmetric(Q)
            print("GENERATOR: generator symmetrized.")
    if is_gen:
        print("GENERATOR: Q is a generator with shape", Q.shape, ".")
    else:
        raise ValueError("GENERATOR: Q is not a generator.")

    return Q
Ejemplo n.º 2
0
def check_weightmat(W):
    """
    Checks whether W is a weight matrix.
    """
    if not np.all(np.diag(W) == 0):
        raise ValueError("WEIGHT MATRIX: nonzero on the diagonal.")
    if not is_symmetric(W):
        raise ValueError("WEIGHT MATRIX: not symmetric.")
Ejemplo n.º 3
0
    def __init__(self, A, crout=True):
        self.crout = crout
        if self.crout:
            self.R = np.array(A)
        else:
            self.R = upper_diag(np.array(A), diag=True)

        # check that A is symmetric
        error_msg = 'A must be symmetric!'
        assert is_symmetric(A), error_msg
Ejemplo n.º 4
0
def cycle_count(A, L0):
    """
    Counts all simple cycles of length up to L0 included on a
    graph whose adjacency matrix is A.

    Parameters
    ----------
    A : numpy.ndarray
        Adjacency matrix
    L0: int
        Length of cycles to count

    Returns
    -------
    tuple
        Two lists are returned. In each, index "i" is the number of primes of length i>=1 up to L0
        The first list is the count of N_positive - N_negative. The second one is the count of
        N_positive + N_negative. Using these two lists, one can compute N_positive and N_negative.
    """
    Primes = ([0] * L0, [0] * L0)
    np.fill_diagonal(A, 0)

    if is_symmetric(A):
        Anw = A != 0
        directed = False
    else:
        Anw = (A != 0) | (A.transpose(1, 0) != 0)
        directed = True

    Size = len(A)
    if L0 > Size:
        L0 = Size

    AllowedVert = [True] * Size
    for i in range(len(A)):
        AllowedVert[i] = False
        Neighbourhood = [False] * Size
        Neighbourhood[i] = True
        Neighbourhood = Neighbourhood + Anw[i, :]
        Primes = recursive_subgraphs(A, Anw, L0, [i], AllowedVert[:], Primes,
                                     Neighbourhood[:], directed)

    return Primes