(A, method='habituated', B=None, nu=3, thetacr=0.7,
        thetacs='auto', maxiter=20, verbose=False)
return splitting
Use Compatible Relaxation to compute a C/F splitting.

    Parameters
    ----------
    A : csr_matrix
        sparse matrix (n x n) usually matrix A of Ax=b
    method : {'habituated','concurrent'}
        Method used during relaxation:
            - concurrent: GS relaxation on F-points, leaving e_c = 0
            - habituated: full relaxation, setting e_c = 0
    B : array like
        Target algebraically smooth vector used in CR. If multiple
        vectors passed in, only first one is used. If B=None, the
        constant vector is used.
    nu : int
        Number of smoothing iterations to apply each CR sweep.
    thetacr : float
        Desired convergence factor of relaxations, 0 < thetacr < 1.
    thetacs : list, float, 'auto'
        Threshold value, 0 < thetacs < 1, to consider nodes from
        candidate set for coarse grid. If e[i] > thetacs for relaxed
        error vector, e, node i is considered for the coarse grid.
        Can be passed in as float to be used for every iteration,
        list of floats to be used on progressive iterations, or as
        string 'auto,' wherein each iteration thetacs = 1 - rho, for
        convergence factor rho from most recent smoothing.
    maxiter : int
        Maximum number of CR iterations (updating of C/F splitting)
        to do.
    verbose : bool
        If true, print iteration number, convergence factor and
        coarsening factor after each iteration.

    Returns
    -------
    splitting : array
        C/F list of 1's (coarse pt) and 0's (fine pt) (n x 1)

    References
    ----------
    [1] Brannick, James J., and Robert D. Falgout. "Compatible
    relaxation and coarsening in algebraic multigrid." SIAM Journal
    on Scientific Computing 32.3 (2010): 1393-1416.

    Examples
    --------
    >>> from pyamg.gallery import poisson
    >>> from pyamg.classical.cr import CR
    >>> A = poisson((20,20),format='csr')
    >>> splitting = CR(A)
Use Compatible Relaxation to compute a C/F splitting.
Use Compatible Relaxation to compute a C / F splitting .
def CR(A, method='habituated', B=None, nu=3, thetacr=0.7,
        thetacs='auto', maxiter=20, verbose=False):
    """Use Compatible Relaxation to compute a C/F splitting.

    Parameters
    ----------
    A : csr_matrix
        sparse matrix (n x n) usually matrix A of Ax=b
    method : {'habituated','concurrent'}
        Method used during relaxation:
            - concurrent: GS relaxation on F-points, leaving e_c = 0
            - habituated: full relaxation, setting e_c = 0
    B : array like
        Target algebraically smooth vector used in CR. If multiple
        vectors passed in, only first one is used. If B=None, the
        constant vector is used.
    nu : int
        Number of smoothing iterations to apply each CR sweep.
    thetacr : float
        Desired convergence factor of relaxations, 0 < thetacr < 1.
    thetacs : list, float, 'auto'
        Threshold value, 0 < thetacs < 1, to consider nodes from
        candidate set for coarse grid. If e[i] > thetacs for relaxed
        error vector, e, node i is considered for the coarse grid.
        Can be passed in as float to be used for every iteration,
        list of floats to be used on progressive iterations, or as
        string 'auto,' wherein each iteration thetacs = 1 - rho, for
        convergence factor rho from most recent smoothing.
    maxiter : int
        Maximum number of CR iterations (updating of C/F splitting)
        to do.
    verbose : bool
        If true, print iteration number, convergence factor and
        coarsening factor after each iteration.

    Returns
    -------
    splitting : array
        C/F list of 1's (coarse pt) and 0's (fine pt) (n x 1)

    References
    ----------
    [1] Brannick, James J., and Robert D. Falgout. "Compatible
    relaxation and coarsening in algebraic multigrid." SIAM Journal
    on Scientific Computing 32.3 (2010): 1393-1416.

    Examples
    --------
    >>> from pyamg.gallery import poisson
    >>> from pyamg.classical.cr import CR
    >>> A = poisson((20,20),format='csr')
    >>> splitting = CR(A)

    """
    n = A.shape[0]    # problem size

    if thetacs == 'auto':
        pass
    else:
        if isinstance(thetacs, list):
            thetacs.reverse()
        elif isinstance(thetacs, float):
            thetacs = list(thetacs)

        if (np.max(thetacs) >= 1) or (np.min(thetacs) <= 0):
            raise ValueError('Must have 0 < thetacs < 1')

    if (thetacr >= 1) or (thetacr <= 0):
        raise ValueError('Must have 0 < thetacr < 1')

    if not isspmatrix_csr(A):
        raise TypeError('expecting csr sparse matrix A')

    if A.dtype == complex:
        raise NotImplementedError('complex A not implemented')

    # Set initial vector. If none provided, set default
    # initial vector of ones
    if B is None:
        B = np.ones((n, 1))

    if B.ndim == 1:
        B = B.reshape((len(B), 1))

    target = B[:, 0]

    # 3.1a - Initialize all nodes as F points
    splitting = np.zeros((n,), dtype='intc')
    indices = np.zeros((n+1,), dtype='intc')
    indices[0] = n
    indices[1:] = np.arange(0, n, dtype='intc')
    Findex = indices[1:]
    Cindex = np.empty((0,), dtype='intc')
    gamma = np.zeros((n,))

    # 3.1b - Run initial smoothing sweep
    rho, e = _CRsweep(A, B, Findex, Cindex, nu, thetacr, method=method)

    # 3.1c - Loop until desired convergence or maximum iterations reached
    for it in range(0, maxiter):

        # Set thetacs value
        if thetacs == 'auto':
            tcs = 1-rho
        else:
            tcs = thetacs[-1]
            if len(thetacs) > 1:
                thetacs.pop()

        # 3.1d - 3.1f, see amg_core.ruge_stuben
        fn = amg_core.cr_helper
        fn(A.indptr,
           A.indices,
           target,
           e,
           indices,
           splitting,
           gamma,
           tcs)

        # Separate F indices and C indices
        num_F = indices[0]
        Findex = indices[1:(num_F+1)]
        Cindex = indices[(num_F+1):]

        # 3.1g - Call CR smoothing iteration
        rho, e = _CRsweep(A, B, Findex, Cindex, nu, thetacr, method=method)

        # Print details on current iteration
        if verbose:
            print(f'CR Iteration {it} CF = {rho}'
                  f', Coarsening factor = {(n-indices[0])/n}')

        # If convergence factor satisfactory, break loop
        if rho < thetacr:
            break

    return splitting
def CR ( A , method = 'habituated' , B = None , nu = 3 , thetacr = 0.7 , thetacs = 'auto' , maxiter = 20 , verbose = False ) : n = A . shape [ 0 ] # problem size if thetacs == 'auto' : pass else : if isinstance ( thetacs , list ) : thetacs . reverse ( ) elif isinstance ( thetacs , float ) : thetacs = list ( thetacs ) if ( np . max ( thetacs ) >= 1 ) or ( np . min ( thetacs ) <= 0 ) : raise ValueError ( 'Must have 0 < thetacs < 1' ) if ( thetacr >= 1 ) or ( thetacr <= 0 ) : raise ValueError ( 'Must have 0 < thetacr < 1' ) if not isspmatrix_csr ( A ) : raise TypeError ( 'expecting csr sparse matrix A' ) if A . dtype == complex : raise NotImplementedError ( 'complex A not implemented' ) # Set initial vector. If none provided, set default # initial vector of ones if B is None : B = np . ones ( ( n , 1 ) ) if B . ndim == 1 : B = B . reshape ( ( len ( B ) , 1 ) ) target = B [ : , 0 ] # 3.1a - Initialize all nodes as F points splitting = np . zeros ( ( n , ) , dtype = 'intc' ) indices = np . zeros ( ( n + 1 , ) , dtype = 'intc' ) indices [ 0 ] = n indices [ 1 : ] = np . arange ( 0 , n , dtype = 'intc' ) Findex = indices [ 1 : ] Cindex = np . empty ( ( 0 , ) , dtype = 'intc' ) gamma = np . zeros ( ( n , ) ) # 3.1b - Run initial smoothing sweep rho , e = _CRsweep ( A , B , Findex , Cindex , nu , thetacr , method = method ) # 3.1c - Loop until desired convergence or maximum iterations reached for it in range ( 0 , maxiter ) : # Set thetacs value if thetacs == 'auto' : tcs = 1 - rho else : tcs = thetacs [ - 1 ] if len ( thetacs ) > 1 : thetacs . pop ( ) # 3.1d - 3.1f, see amg_core.ruge_stuben fn = amg_core . cr_helper fn ( A . indptr , A . indices , target , e , indices , splitting , gamma , tcs ) # Separate F indices and C indices num_F = indices [ 0 ] Findex = indices [ 1 : ( num_F + 1 ) ] Cindex = indices [ ( num_F + 1 ) : ] # 3.1g - Call CR smoothing iteration rho , e = _CRsweep ( A , B , Findex , Cindex , nu , thetacr , method = method ) # Print details on current iteration if verbose : print ( f'CR Iteration {it} CF = {rho}' f', Coarsening factor = {(n-indices[0])/n}' ) # If convergence factor satisfactory, break loop if rho < thetacr : break return splitting