(A, tol=0.01, maxiter=15, restart=5,
                                symmetric=None, initial_guess=None,
                                return_vector=False)
return A.rho
Approximate the spectral radius of a matrix.

    Parameters
    ----------
    A : {dense or sparse matrix}
        E.g. csr_matrix, csc_matrix, ndarray, etc.
    tol : {scalar}
        Relative tolerance of approximation, i.e., the error divided
        by the approximate spectral radius is compared to tol.
    maxiter : {integer}
        Maximum number of iterations to perform
    restart : {integer}
        Number of restarted Arnoldi processes.  For example, a value of 0 will
        run Arnoldi once, for maxiter iterations, and a value of 1 will restart
        Arnoldi once, using the maximal eigenvector from the first Arnoldi
        process as the initial guess.
    symmetric : {boolean}
        True  - if A is symmetric Lanczos iteration is used (more efficient)
        False - if A is non-symmetric Arnoldi iteration is used (less efficient)
    initial_guess : {array|None}
        If n x 1 array, then use as initial guess for Arnoldi/Lanczos.
        If None, then use a random initial guess.
    return_vector : {boolean}
        True - return an approximate dominant eigenvector and the spectral radius.
        False - Do not return the approximate dominant eigenvector

    Returns
    -------
    An approximation to the spectral radius of A, and
    if return_vector=True, then also return the approximate dominant
    eigenvector

    Notes
    -----
    The spectral radius is approximated by looking at the Ritz eigenvalues.
    Arnoldi iteration (or Lanczos) is used to project the matrix A onto a
    Krylov subspace: H = Q* A Q.  The eigenvalues of H (i.e. the Ritz
    eigenvalues) should represent the eigenvalues of A in the sense that the
    minimum and maximum values are usually well matched (for the symmetric case
    it is true since the eigenvalues are real).

    References
    ----------
    .. [1] Z. Bai, J. Demmel, J. Dongarra, A. Ruhe, and H. van der Vorst,
       editors.  "Templates for the Solution of Algebraic Eigenvalue Problems:
       A Practical Guide", SIAM, Philadelphia, 2000.

    Examples
    --------
    >>> from pyamg.util.linalg import approximate_spectral_radius
    >>> import numpy as np
    >>> from scipy.linalg import eigvals, norm
    >>> A = np.array([[1.,0.],[0.,1.]])
    >>> sr = approximate_spectral_radius(A,maxiter=3)
    >>> print(f'{sr:2.6}')
    1.0
    >>> print(max([norm(x) for x in eigvals(A)]))
    1.0
Approximate the spectral radius of a matrix.
Approximate the spectral radius of a matrix .
def approximate_spectral_radius(A, tol=0.01, maxiter=15, restart=5,
                                symmetric=None, initial_guess=None,
                                return_vector=False):
    """Approximate the spectral radius of a matrix.

    Parameters
    ----------
    A : {dense or sparse matrix}
        E.g. csr_matrix, csc_matrix, ndarray, etc.
    tol : {scalar}
        Relative tolerance of approximation, i.e., the error divided
        by the approximate spectral radius is compared to tol.
    maxiter : {integer}
        Maximum number of iterations to perform
    restart : {integer}
        Number of restarted Arnoldi processes.  For example, a value of 0 will
        run Arnoldi once, for maxiter iterations, and a value of 1 will restart
        Arnoldi once, using the maximal eigenvector from the first Arnoldi
        process as the initial guess.
    symmetric : {boolean}
        True  - if A is symmetric Lanczos iteration is used (more efficient)
        False - if A is non-symmetric Arnoldi iteration is used (less efficient)
    initial_guess : {array|None}
        If n x 1 array, then use as initial guess for Arnoldi/Lanczos.
        If None, then use a random initial guess.
    return_vector : {boolean}
        True - return an approximate dominant eigenvector and the spectral radius.
        False - Do not return the approximate dominant eigenvector

    Returns
    -------
    An approximation to the spectral radius of A, and
    if return_vector=True, then also return the approximate dominant
    eigenvector

    Notes
    -----
    The spectral radius is approximated by looking at the Ritz eigenvalues.
    Arnoldi iteration (or Lanczos) is used to project the matrix A onto a
    Krylov subspace: H = Q* A Q.  The eigenvalues of H (i.e. the Ritz
    eigenvalues) should represent the eigenvalues of A in the sense that the
    minimum and maximum values are usually well matched (for the symmetric case
    it is true since the eigenvalues are real).

    References
    ----------
    .. [1] Z. Bai, J. Demmel, J. Dongarra, A. Ruhe, and H. van der Vorst,
       editors.  "Templates for the Solution of Algebraic Eigenvalue Problems:
       A Practical Guide", SIAM, Philadelphia, 2000.

    Examples
    --------
    >>> from pyamg.util.linalg import approximate_spectral_radius
    >>> import numpy as np
    >>> from scipy.linalg import eigvals, norm
    >>> A = np.array([[1.,0.],[0.,1.]])
    >>> sr = approximate_spectral_radius(A,maxiter=3)
    >>> print(f'{sr:2.6}')
    1.0
    >>> print(max([norm(x) for x in eigvals(A)]))
    1.0

    """
    if not hasattr(A, 'rho') or return_vector:
        # somehow more restart causes a nonsymmetric case to fail...look at
        # this what about A.dtype=int?  convert somehow?

        # The use of the restart vector v0 requires that the full Krylov
        # subspace V be stored.  So, set symmetric to False.
        symmetric = False

        if maxiter < 1:
            raise ValueError('expected maxiter > 0')
        if restart < 0:
            raise ValueError('expected restart >= 0')
        if A.dtype == int:
            raise ValueError('expected A to be float (complex or real)')
        if A.shape[0] != A.shape[1]:
            raise ValueError('expected square A')

        if initial_guess is None:
            v0 = np.random.rand(A.shape[1], 1)
            if A.dtype == complex:
                v0 = v0 + 1.0j * np.random.rand(A.shape[1], 1)
        else:
            if initial_guess.shape[0] != A.shape[0]:
                raise ValueError('initial_guess and A must have same shape')
            if (len(initial_guess.shape) > 1) and (initial_guess.shape[1] > 1):
                raise ValueError('initial_guess must be an (n,1) or\
                                  (n,) vector')
            v0 = initial_guess.reshape(-1, 1)
            v0 = np.array(v0, dtype=A.dtype)

        for j in range(restart+1):
            [evect, ev, H, V, breakdown_flag] =\
                _approximate_eigenvalues(A, maxiter, symmetric, initial_guess=v0)
            # Calculate error in dominant eigenvector
            nvecs = ev.shape[0]
            max_index = np.abs(ev).argmax()
            error = H[nvecs, nvecs-1] * evect[-1, max_index]

            # error is a fast way of calculating the following line
            # error2 = ( A - ev[max_index]*sp.mat(
            #           sp.eye(A.shape[0],A.shape[1])) )*\
            #           ( sp.mat(sp.hstack(V[:-1]))*\
            #           evect[:,max_index].reshape(-1,1) )
            # print(str(error) + "    " + str(sp.linalg.norm(e2)))

            v0 = np.dot(np.hstack(V[:-1]), evect[:, max_index].reshape(-1, 1))

            if np.abs(error)/np.abs(ev[max_index]) < tol:
                # halt if below relative tolerance
                break

            if breakdown_flag:
                warn(f'Breakdown occured in step {j}')
                break
        # end j-loop

        rho = np.abs(ev[max_index])
        if sparse.isspmatrix(A):
            A.rho = rho

        if return_vector:
            return (rho, v0)

        return rho

    return A.rho
def approximate_spectral_radius ( A , tol = 0.01 , maxiter = 15 , restart = 5 , symmetric = None , initial_guess = None , return_vector = False ) : if not hasattr ( A , 'rho' ) or return_vector : # somehow more restart causes a nonsymmetric case to fail...look at # this what about A.dtype=int?  convert somehow? # The use of the restart vector v0 requires that the full Krylov # subspace V be stored.  So, set symmetric to False. symmetric = False if maxiter < 1 : raise ValueError ( 'expected maxiter > 0' ) if restart < 0 : raise ValueError ( 'expected restart >= 0' ) if A . dtype == int : raise ValueError ( 'expected A to be float (complex or real)' ) if A . shape [ 0 ] != A . shape [ 1 ] : raise ValueError ( 'expected square A' ) if initial_guess is None : v0 = np . random . rand ( A . shape [ 1 ] , 1 ) if A . dtype == complex : v0 = v0 + 1.0j * np . random . rand ( A . shape [ 1 ] , 1 ) else : if initial_guess . shape [ 0 ] != A . shape [ 0 ] : raise ValueError ( 'initial_guess and A must have same shape' ) if ( len ( initial_guess . shape ) > 1 ) and ( initial_guess . shape [ 1 ] > 1 ) : raise ValueError ( 'initial_guess must be an (n,1) or\
                                  (n,) vector' ) v0 = initial_guess . reshape ( - 1 , 1 ) v0 = np . array ( v0 , dtype = A . dtype ) for j in range ( restart + 1 ) : [ evect , ev , H , V , breakdown_flag ] = _approximate_eigenvalues ( A , maxiter , symmetric , initial_guess = v0 ) # Calculate error in dominant eigenvector nvecs = ev . shape [ 0 ] max_index = np . abs ( ev ) . argmax ( ) error = H [ nvecs , nvecs - 1 ] * evect [ - 1 , max_index ] # error is a fast way of calculating the following line # error2 = ( A - ev[max_index]*sp.mat( #           sp.eye(A.shape[0],A.shape[1])) )*\ #           ( sp.mat(sp.hstack(V[:-1]))*\ #           evect[:,max_index].reshape(-1,1) ) # print(str(error) + "    " + str(sp.linalg.norm(e2))) v0 = np . dot ( np . hstack ( V [ : - 1 ] ) , evect [ : , max_index ] . reshape ( - 1 , 1 ) ) if np . abs ( error ) / np . abs ( ev [ max_index ] ) < tol : # halt if below relative tolerance break if breakdown_flag : warn ( f'Breakdown occured in step {j}' ) break # end j-loop rho = np . abs ( ev [ max_index ] ) if sparse . isspmatrix ( A ) : A . rho = rho if return_vector : return ( rho , v0 ) return rho return A . rho