(mg, filter_entries=True)
return leveleigs
Examine a multilevel hierarchy's spectrum.

    Parameters
    ----------
    mg { pyamg multilevel hierarchy }
        e.g. generated with smoothed_aggregation_solver(...) or
        ruge_stuben_solver(...)

    Returns
    -------
    leveleigs : list
        List of eigenvalues per level

    Notes
    -----
    This can be useful for troubleshooting and when examining how your
    problem's nature changes from level to level

    A table is printed to standard out detailing the spectrum of each level in mg

    Examples
    --------
    >>> from pyamg import smoothed_aggregation_solver
    >>> from pyamg.gallery import poisson
    >>> from pyamg.util.utils import hierarchy_spectrum
    >>> A = poisson( (1,), format='csr' )
    >>> ml = smoothed_aggregation_solver(A)
    >>> leveleigs = hierarchy_spectrum(ml)
    <BLANKLINE>
     Level | min(re(eig)) | max(re(eig)) | num re(eig) < 0 | num re(eig) > 0 | cond_2(A)
    -------------------------------------------------------------------------------------
       0   |    2.000     |    2.000     |        0        |        1        |  1.00e+00
    <BLANKLINE>
    <BLANKLINE>
     Level | min(im(eig)) | max(im(eig)) | num im(eig) < 0 | num im(eig) > 0 | cond_2(A)
    -------------------------------------------------------------------------------------
       0   |    0.000     |    0.000     |        0        |        0        |  1.00e+00
    <BLANKLINE>
Examine a multilevel hierarchy's spectrum.
Examine a multilevel hierarchy s spectrum .
def hierarchy_spectrum(mg, filter_entries=True):
    """Examine a multilevel hierarchy's spectrum.

    Parameters
    ----------
    mg { pyamg multilevel hierarchy }
        e.g. generated with smoothed_aggregation_solver(...) or
        ruge_stuben_solver(...)

    Returns
    -------
    leveleigs : list
        List of eigenvalues per level

    Notes
    -----
    This can be useful for troubleshooting and when examining how your
    problem's nature changes from level to level

    A table is printed to standard out detailing the spectrum of each level in mg

    Examples
    --------
    >>> from pyamg import smoothed_aggregation_solver
    >>> from pyamg.gallery import poisson
    >>> from pyamg.util.utils import hierarchy_spectrum
    >>> A = poisson( (1,), format='csr' )
    >>> ml = smoothed_aggregation_solver(A)
    >>> leveleigs = hierarchy_spectrum(ml)
    <BLANKLINE>
     Level | min(re(eig)) | max(re(eig)) | num re(eig) < 0 | num re(eig) > 0 | cond_2(A)
    -------------------------------------------------------------------------------------
       0   |    2.000     |    2.000     |        0        |        1        |  1.00e+00
    <BLANKLINE>
    <BLANKLINE>
     Level | min(im(eig)) | max(im(eig)) | num im(eig) < 0 | num im(eig) > 0 | cond_2(A)
    -------------------------------------------------------------------------------------
       0   |    0.000     |    0.000     |        0        |        0        |  1.00e+00
    <BLANKLINE>

    """
    real_table = [['Level', 'min(re(eig))', 'max(re(eig))', 'num re(eig) < 0',
                   'num re(eig) > 0', 'cond_2(A)']]
    imag_table = [['Level', 'min(im(eig))', 'max(im(eig))', 'num im(eig) < 0',
                   'num im(eig) > 0', 'cond_2(A)']]

    leveleigs = []
    for i, level in enumerate(mg.levels):
        A = level.A.tocsr()

        if filter_entries is True:
            # Filter out any zero rows and columns of A
            A.eliminate_zeros()
            nnz_per_row = A.indptr[0:-1] - A.indptr[1:]
            nonzero_rows = (nnz_per_row != 0).nonzero()[0]
            A = A.tocsc()
            nnz_per_col = A.indptr[0:-1] - A.indptr[1:]
            nonzero_cols = (nnz_per_col != 0).nonzero()[0]
            nonzero_rowcols = np.union1d(nonzero_rows, nonzero_cols)
            A = A.toarray()
            A = A[nonzero_rowcols, :][:, nonzero_rowcols]
        else:
            A = A.toarray()

        e = eigvals(A)
        c = linalg.cond(A)
        lambda_min = min(np.real(e))
        lambda_max = max(np.real(e))
        num_neg = max(e[np.real(e) < 0.0].shape)
        num_pos = max(e[np.real(e) > 0.0].shape)
        real_table.append([str(i), f'{lambda_min:1.3f}', f'{lambda_max:1.3f}',
                           str(num_neg), str(num_pos), f'{c:1.2e}'])

        lambda_min = min(np.imag(e))
        lambda_max = max(np.imag(e))
        num_neg = max(e[np.imag(e) < 0.0].shape)
        num_pos = max(e[np.imag(e) > 0.0].shape)
        imag_table.append([str(i), f'{lambda_min:1.3f}', f'{lambda_max:1.3f}',
                           str(num_neg), str(num_pos), f'{c:1.2e}'])

        leveleigs.append(e)

    print(print_table(real_table))
    print(print_table(imag_table))

    return leveleigs
def hierarchy_spectrum ( mg , filter_entries = True ) : real_table = [ [ 'Level' , 'min(re(eig))' , 'max(re(eig))' , 'num re(eig) < 0' , 'num re(eig) > 0' , 'cond_2(A)' ] ] imag_table = [ [ 'Level' , 'min(im(eig))' , 'max(im(eig))' , 'num im(eig) < 0' , 'num im(eig) > 0' , 'cond_2(A)' ] ] leveleigs = [ ] for i , level in enumerate ( mg . levels ) : A = level . A . tocsr ( ) if filter_entries is True : # Filter out any zero rows and columns of A A . eliminate_zeros ( ) nnz_per_row = A . indptr [ 0 : - 1 ] - A . indptr [ 1 : ] nonzero_rows = ( nnz_per_row != 0 ) . nonzero ( ) [ 0 ] A = A . tocsc ( ) nnz_per_col = A . indptr [ 0 : - 1 ] - A . indptr [ 1 : ] nonzero_cols = ( nnz_per_col != 0 ) . nonzero ( ) [ 0 ] nonzero_rowcols = np . union1d ( nonzero_rows , nonzero_cols ) A = A . toarray ( ) A = A [ nonzero_rowcols , : ] [ : , nonzero_rowcols ] else : A = A . toarray ( ) e = eigvals ( A ) c = linalg . cond ( A ) lambda_min = min ( np . real ( e ) ) lambda_max = max ( np . real ( e ) ) num_neg = max ( e [ np . real ( e ) < 0.0 ] . shape ) num_pos = max ( e [ np . real ( e ) > 0.0 ] . shape ) real_table . append ( [ str ( i ) , f'{lambda_min:1.3f}' , f'{lambda_max:1.3f}' , str ( num_neg ) , str ( num_pos ) , f'{c:1.2e}' ] ) lambda_min = min ( np . imag ( e ) ) lambda_max = max ( np . imag ( e ) ) num_neg = max ( e [ np . imag ( e ) < 0.0 ] . shape ) num_pos = max ( e [ np . imag ( e ) > 0.0 ] . shape ) imag_table . append ( [ str ( i ) , f'{lambda_min:1.3f}' , f'{lambda_max:1.3f}' , str ( num_neg ) , str ( num_pos ) , f'{c:1.2e}' ] ) leveleigs . append ( e ) print ( print_table ( real_table ) ) print ( print_table ( imag_table ) ) return leveleigs