(C, num_clusters=None)
return AggOp, seeds
Aggregate nodes using Balanced Lloyd Clustering.

    Parameters
    ----------
    C : csr_matrix
        strength of connection matrix with positive weights
    num_clusters : int
        Number of seeds or clusters expected (default: C.shape[0] / 10)

    Returns
    -------
    AggOp : csr_matrix
        aggregation operator which determines the sparsity pattern
        of the tentative prolongator
    seeds : array
        array of Cpts, i.e., Cpts[i] = root node of aggregate i

    See Also
    --------
    amg_core.standard_aggregation

    Examples
    --------
    >>> import pyamg
    >>> from pyamg.aggregation.aggregate import balanced_lloyd_aggregation
    >>> data = pyamg.gallery.load_example('unit_square')
    >>> G = data['A']
    >>> xy = data['vertices'][:,:2]
    >>> G.data[:] = np.ones(len(G.data))

    >>> np.random.seed(787888)
    >>> AggOp, seeds = balanced_lloyd_aggregation(G)
Aggregate nodes using Balanced Lloyd Clustering.
Aggregate nodes using Balanced Lloyd Clustering .
def balanced_lloyd_aggregation(C, num_clusters=None):
    """Aggregate nodes using Balanced Lloyd Clustering.

    Parameters
    ----------
    C : csr_matrix
        strength of connection matrix with positive weights
    num_clusters : int
        Number of seeds or clusters expected (default: C.shape[0] / 10)

    Returns
    -------
    AggOp : csr_matrix
        aggregation operator which determines the sparsity pattern
        of the tentative prolongator
    seeds : array
        array of Cpts, i.e., Cpts[i] = root node of aggregate i

    See Also
    --------
    amg_core.standard_aggregation

    Examples
    --------
    >>> import pyamg
    >>> from pyamg.aggregation.aggregate import balanced_lloyd_aggregation
    >>> data = pyamg.gallery.load_example('unit_square')
    >>> G = data['A']
    >>> xy = data['vertices'][:,:2]
    >>> G.data[:] = np.ones(len(G.data))

    >>> np.random.seed(787888)
    >>> AggOp, seeds = balanced_lloyd_aggregation(G)

    """
    if num_clusters is None:
        num_clusters = int(C.shape[0] / 10)

    if num_clusters < 1 or num_clusters > C.shape[0]:
        raise ValueError('num_clusters must be between 1 and n')

    if not (sparse.isspmatrix_csr(C) or sparse.isspmatrix_csc(C)):
        raise TypeError('expected csr_matrix or csc_matrix')

    if C.data.min() <= 0:
        raise ValueError('positive edge weights required')

    if C.dtype == complex:
        data = np.real(C.data)
    else:
        data = C.data

    G = C.__class__((data, C.indices, C.indptr), shape=C.shape)
    num_nodes = G.shape[0]

    seeds = np.random.permutation(num_nodes)[:num_clusters]
    seeds = seeds.astype(np.int32)
    mv = np.finfo(G.dtype).max
    d = mv * np.ones(num_nodes, dtype=G.dtype)
    d[seeds] = 0

    cm = -1 * np.ones(num_nodes, dtype=np.int32)
    cm[seeds] = seeds

    amg_core.lloyd_cluster_exact(num_nodes,
                                 G.indptr, G.indices, G.data,
                                 num_clusters,
                                 d, cm, seeds)

    col = cm
    row = np.arange(len(cm))
    data = np.ones(len(row), dtype=np.int32)
    AggOp = sparse.coo_matrix((data, (row, col)),
                              shape=(G.shape[0], num_clusters)).tocsr()
    return AggOp, seeds
def balanced_lloyd_aggregation ( C , num_clusters = None ) : if num_clusters is None : num_clusters = int ( C . shape [ 0 ] / 10 ) if num_clusters < 1 or num_clusters > C . shape [ 0 ] : raise ValueError ( 'num_clusters must be between 1 and n' ) if not ( sparse . isspmatrix_csr ( C ) or sparse . isspmatrix_csc ( C ) ) : raise TypeError ( 'expected csr_matrix or csc_matrix' ) if C . data . min ( ) <= 0 : raise ValueError ( 'positive edge weights required' ) if C . dtype == complex : data = np . real ( C . data ) else : data = C . data G = C . __class__ ( ( data , C . indices , C . indptr ) , shape = C . shape ) num_nodes = G . shape [ 0 ] seeds = np . random . permutation ( num_nodes ) [ : num_clusters ] seeds = seeds . astype ( np . int32 ) mv = np . finfo ( G . dtype ) . max d = mv * np . ones ( num_nodes , dtype = G . dtype ) d [ seeds ] = 0 cm = - 1 * np . ones ( num_nodes , dtype = np . int32 ) cm [ seeds ] = seeds amg_core . lloyd_cluster_exact ( num_nodes , G . indptr , G . indices , G . data , num_clusters , d , cm , seeds ) col = cm row = np . arange ( len ( cm ) ) data = np . ones ( len ( row ) , dtype = np . int32 ) AggOp = sparse . coo_matrix ( ( data , ( row , col ) ) , shape = ( G . shape [ 0 ] , num_clusters ) ) . tocsr ( ) return AggOp , seeds