当前位置: 首页>>代码示例>>Python>>正文


Python MatrixUtil.assert_nonnegative方法代码示例

本文整理汇总了Python中MatrixUtil.assert_nonnegative方法的典型用法代码示例。如果您正苦于以下问题:Python MatrixUtil.assert_nonnegative方法的具体用法?Python MatrixUtil.assert_nonnegative怎么用?Python MatrixUtil.assert_nonnegative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MatrixUtil的用法示例。


在下文中一共展示了MatrixUtil.assert_nonnegative方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: fiedler_cut_valuator

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def fiedler_cut_valuator(A):
    """
    @param A: a symmetric weighted adjacency matrix
    @return: a valuation of the indices of the adjacency matrix
    """
    MatrixUtil.assert_symmetric(A)
    MatrixUtil.assert_nonnegative(A)
    MatrixUtil.assert_hollow(A)
    nverts = A.shape[0]
    L = np.diag(np.sum(A, axis=1)) - A
    w, v = scipy.linalg.eigh(L)
    return v[:, 1]
开发者ID:argriffing,项目名称:xgcode,代码行数:14,代码来源:20130113a.py

示例2: check_generic_cut

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def check_generic_cut(valuator, extendor, A):
    """
    The input matrix is expected to have a certain block structure.
    In particular, the leaf vertices are expected to
    precede the points of articulation.
    Because the tree is expected to be an unrooted binary tree,
    the relative number of leaves and points of articulation
    is determined by the size of the adjacency matrix.
    @param A: adjacency matrix of an unrooted edge-weighted binary tree
    @return: True if a counterexample is found
    """
    MatrixUtil.assert_symmetric(A)
    MatrixUtil.assert_nonnegative(A)
    MatrixUtil.assert_hollow(A)
    nverts = A.shape[0]
    if nverts < 4:
        raise Exception('expected at least four vertices')
    if nverts % 2 != 0:
        raise Exception('expected an even number of vertices')
    ntips = nverts / 2 + 1
    narts = nverts / 2 - 1
    # get the schur complement laplacian and its associated adjacency matrix
    L = np.diag(np.sum(A, axis=1)) - A
    L_tips = L[:ntips, :ntips] - ndot(
            L[:ntips, -narts:],
            scipy.linalg.inv(L[-narts:, -narts:]),
            L[-narts:, :ntips],
            )
    A_tips = np.diag(np.diag(L_tips)) - L_tips
    tip_valuations = valuator(A_tips)
    #tip_valuations -= np.mean(tip_valuations)
    #tip_valuations /= np.linalg.norm(tip_valuations)
    art_valuations = extendor(A, tip_valuations)
    #ntip_pos = sum(1 for v in tip_valuations if v > 0)
    #ntip_neg = sum(1 for v in tip_valuations if v < 0)
    #nart_pos = sum(1 for v in art_valuations if v > 0)
    #nart_neg = sum(1 for v in art_valuations if v < 0)
    #print ((ntip_pos, ntip_neg), (nart_pos, nart_neg))
    valuations = np.concatenate((tip_valuations, art_valuations))
    ncrossings = 0
    for i in range(nverts):
        for j in range(i+1, nverts):
            if valuations[i] * valuations[j] * A[i, j] < 0:
                ncrossings += 1
    if ncrossings != 1:
        # found a counterexample!
        print ncrossings
        print A
        return True
开发者ID:argriffing,项目名称:xgcode,代码行数:51,代码来源:20130113a.py

示例3: min_cut_valuator

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def min_cut_valuator(A):
    """
    @param A: a symmetric weighted adjacency matrix
    @return: a valuation of the indices of the adjacency matrix
    """
    MatrixUtil.assert_symmetric(A)
    MatrixUtil.assert_nonnegative(A)
    MatrixUtil.assert_hollow(A)
    nverts = A.shape[0]
    v_pos = StoerWagner.stoer_wagner_min_cut(A)
    v_neg = set(range(nverts)) - v_pos
    valuations = np.zeros(nverts, dtype=float)
    for v in v_pos:
        valuations[v] = 1
    for v in v_neg:
        valuations[v] = -1
    return valuations
开发者ID:argriffing,项目名称:xgcode,代码行数:19,代码来源:20130113a.py

示例4: stoer_wagner_min_cut

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def stoer_wagner_min_cut(weight_matrix):
    """
    The input matrix is assumed to be a numpy ndarray with float dtype.
    @param weight_matrix: non-negative symmetric weighted adjacency matrix
    @return: the set of indices belonging to one of the two clusters
    """
    w = weight_matrix.copy()
    n = w.shape[0]
    MatrixUtil.assert_symmetric(w)
    MatrixUtil.assert_nonnegative(w)
    MatrixUtil.assert_hollow(w)
    # no cut has been observed so far
    min_cut = None
    min_cut_weight = None
    remaining_indices = set(range(n))
    index_sets = [set([i]) for i in range(n)]
    # reduce the number of remaining indices by one each iteration
    while len(remaining_indices) > 1:
        # initialize the borg
        assimilated_indices = [0]
        unassimilated_indices = remaining_indices - set(assimilated_indices)
        weight_index_pairs = [(w[i, 0], i) for i in unassimilated_indices]
        while len(assimilated_indices) < len(remaining_indices):
            max_weight, best_index = max(weight_index_pairs)
            assimilated_indices.append(best_index)
            unassimilated_indices.remove(best_index)
            next_weight_index_pairs = []
            for weight, index in weight_index_pairs:
                if index is not best_index:
                    next_weight = weight + w[index, best_index]
                    next_weight_index_pairs.append((next_weight, index))
            weight_index_pairs = next_weight_index_pairs
        # the cut between the last two assimilated indices is a possible min cut
        cut = set(index_sets[assimilated_indices[-1]])
        s, t = list(sorted(assimilated_indices[-2:]))
        cut_weight = max_weight
        if min_cut is None or cut_weight < min_cut_weight:
            min_cut, min_cut_weight = cut, cut_weight
        # combine the last two assimilated indices
        for i in remaining_indices:
            w[i, s] += w[i, t]
            w[s, i] += w[t, i]
        index_sets[s].update(index_sets[t])
        remaining_indices.remove(t)
    return min_cut
开发者ID:argriffing,项目名称:xgcode,代码行数:47,代码来源:StoerWagner.py

示例5: harmonic_extension

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def harmonic_extension(A, tip_valuations):
    """
    This extension uses the 'Laplacian matrix' and the 'accompanying matrix'.
    @param A: weighted undirected adjacency matrix with tips first
    @param valuations: valuations associated with the first part of A
    @return: extension of valuations to the remaining vertices
    """
    MatrixUtil.assert_symmetric(A)
    MatrixUtil.assert_nonnegative(A)
    MatrixUtil.assert_hollow(A)
    nverts = A.shape[0]
    ntips = tip_valuations.shape[0]
    narts = nverts - ntips
    L = np.diag(np.sum(A, axis=1)) - A
    acc = -np.dot(L[:ntips, -narts:], scipy.linalg.inv(L[-narts:, -narts:]))
    if acc.shape != (ntips, narts):
        raise Exception('unexpected shape of accompanying matrix')
    MatrixUtil.assert_positive(acc)
    return np.dot(tip_valuations, acc)
开发者ID:argriffing,项目名称:xgcode,代码行数:21,代码来源:20130113a.py

示例6: combinatorial_extension

# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_nonnegative [as 别名]
def combinatorial_extension(A, tip_valuations):
    """
    This extension uses {-1, +1} valuations to minimize zero-crossings.
    It currently uses brute force.
    A more sophisticated approach would minimize not the number of
    zero-crossings, but would instead minimize the number of
    connected components induced by the valuation sign cut.
    @param A: weighted undirected adjacency matrix with tips first
    @param valuations: valuations associated with the first part of A
    @return: extension of valuations to the remaining vertices
    """
    MatrixUtil.assert_symmetric(A)
    MatrixUtil.assert_nonnegative(A)
    MatrixUtil.assert_hollow(A)
    nverts = A.shape[0]
    ntips = tip_valuations.shape[0]
    narts = nverts - ntips
    # get some arbitrarily directed edges from the adjacency matrix
    edges = []
    for i in range(nverts):
        for j in range(i+1, nverts):
            if A[i, j] > 0:
                edges.append((i, j))
    # use brute force to get the best sign valuation of internal vertices
    best_art_vals = None
    best_ncrossings = None
    for art_vals in itertools.product((-1, 1), repeat=narts):
        vfull = np.concatenate((tip_valuations, art_vals))
        ncrossings = 0
        for i, j in edges:
            if A[i, j] * vfull[i] * vfull[j] < 0:
                ncrossings += 1
        if best_art_vals is None or ncrossings < best_ncrossings:
            best_art_vals = art_vals
            best_ncrossings = ncrossings
    return np.array(best_art_vals, dtype=float)
开发者ID:argriffing,项目名称:xgcode,代码行数:38,代码来源:20130113a.py


注:本文中的MatrixUtil.assert_nonnegative方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。