
    _-Ph                         d Z ddlmZ ddlZddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ dd	lmZ d
ddddfdddifdddifddfdZd ZdS )z+Support for pairwise-aggregation-based AMG.    )warnN)
csr_matrixisspmatrix_csrisspmatrix_bsrSparseEfficiencyWarning)MultilevelSolver)change_smoothers)get_blocksize levelize_strength_or_aggregation   )pairwise_aggregationpairwiseg      ?min   )thetanorm	matchingsblock_gauss_seidelsweep	symmetric   
   c                    t          |           sWt          |           sH	 t          |           } t          dt                     n"# t
          $ r}t          d          |d}~ww xY w|                                 } | j        d         | j        d         k    rt          d          t          j        | j                  rt          d          t          |||          \  }}}g }|                    t          j                               | |d         _        t%          |          |k     rt'          |d         j        j        d         t)          |d         j                  z            |k    rgt+          ||           t%          |          |k     rDt'          |d         j        j        d         t)          |d         j                  z            |k    gt          |fi |}	t-          |	||           |	S )	a	  
    Create a multilevel solver using Pairwise Aggregation.

    Parameters
    ----------
    A : {csr_matrix, bsr_matrix}
        Sparse NxN matrix in CSR or BSR format
    aggregate : {tuple, string, list} : default ('pairwise',
            {'theta': 0.25, 'norm':'min', 'matchings': 2})
        Method choice must be 'pairwise'; inner pairwise options including
        matchings, theta, and norm can be modified,
    presmoother : {tuple, string, list} : default ('block_gauss_seidel',
        {'sweep':'symmetric'})
        Defines the presmoother for the multilevel cycling.  The default block
        Gauss-Seidel option defaults to point-wise Gauss-Seidel, if the matrix
        is CSR or is a BSR matrix with blocksize of 1.  See notes below for
        varying this parameter on a per level basis.
    postsmoother : {tuple, string, list}
        Same as presmoother, except defines the postsmoother.
    max_levels : {integer} : default 10
        Maximum number of levels to be used in the multilevel solver.
    max_coarse : {integer} : default 500
        Maximum number of variables permitted on the coarse grid.

    Other Parameters
    ----------------
    coarse_solver : ['splu', 'lu', 'cholesky, 'pinv', 'gauss_seidel', ... ]
        Solver used at the coarsest level of the MG hierarchy.
        Optionally, may be a tuple (fn, args), where fn is a string such as
        ['splu', 'lu', ...] or a callable function, and args is a dictionary of
        arguments to be passed to fn.


    Returns
    -------
    ml : multilevel_solver
        Multigrid hierarchy of matrices and prolongation operators

    See Also
    --------
    multilevel_solver, classical.ruge_stuben_solver,
    aggregation.smoothed_aggregation_solver

    Examples
    --------
    >>> from pyamg import pairwise_solver
    >>> from pyamg.gallery import poisson
    >>> from scipy.sparse.linalg import cg
    >>> import numpy as np
    >>> A = poisson((100, 100), format='csr')       # matrix
    >>> b = np.ones((A.shape[0]))                   # RHS
    >>> ml = pairwise_solver(A)                     # AMG solver
    >>> M = ml.aspreconditioner(cycle='V')          # preconditioner
    >>> x, info = cg(A, b, tol=1e-8, maxiter=30, M=M)   # solve with CG

    References
    ----------
    [0] Notay, Y. (2010). An aggregation-based algebraic multigrid
    method. Electronic transactions on numerical analysis, 37(6),
    123-146.

    zImplicit conversion of A to CSRzSArgument A must have type csr_matrix or bsr_matrix, or be convertible to csr_matrixNr   r   zexpected square matrixz1Pairwise solver not verified for complex matrices)r   r   r   r   r   	Exception	TypeErrorasfptypeshape
ValueErrornpiscomplexobjdatar   appendr   LevelAlenintr
   _extend_hierarchyr	   )
r%   	aggregatepresmootherpostsmoother
max_levels
max_coarsekwargselevelsmls
             Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/aggregation/pairwise.pypairwise_solverr3      s   N 1 F!2!2 F	F1A24KLLLL 	F 	F 	F > ? ?DEF	F 	


AwqzQWQZ1222	qv NLMMM
 	)J
KK &J
I F
MM"(**+++F2JL
f++

"
"r
"1%mF2JL&A&AABBZOO&),,, f++

"
"r
"1%mF2JL&A&AABBZOO 
&	+	+F	+	+BRl333Is   $A 
A$AA$c                    d }| d         j         } ||t          |           dz
                     \  }}t          |fi |ddid         }|j                                        }t          |          r|                                }|| d         _        || d         _        | 	                    t          j                               ||z  |z  }|| d         _         dS )zExtend the multigrid hierarchy.c                 T    t          | t                    r| d         | d         fS | i fS )Nr   r   )
isinstancetuple)vs    r2   
unpack_argz%_extend_hierarchy.<locals>.unpack_argz   s/    a 	Q41:"u    r   r   	compute_PTr   N)r%   r&   r   T	conjugater   tocsrPRr#   r   r$   )r0   r)   r9   r%   _r.   r?   r@   s           r2   r(   r(   x   s      
 	r
A 
9S[[]344IAvQ99&99D999!<A	Aa GGIIF2JLF2JL
MM"(**+++	A	AF2JLLLr:   )__doc__warningsr   numpyr    scipy.sparser   r   r   r   pyamg.multilevelr   pyamg.relaxation.smoothingr	   pyamg.util.utilsr
   r   r)   r   r3   r(    r:   r2   <module>rJ      s)   1 1                      . - - - - - 7 7 7 7 7 7 L L L L L L L L + + + + + +  *T',1,> ,> ?!5");!7!9"6#*K"8":!bf f f fR    r:   