
    _-Ph2                     r    d Z ddlZddlmZ ddlZddlmZ ddlZddl	m
Z
 ddlmZ ddlmZ d	 Z	 	 	 ddZdS )zDFlexible Generalized Minimum Residual Method (fGMRES) Krylov solver.    N)warn)get_lapack_funcs   )norm)make_system)amg_corec                 @    | dk    rdS | t          j        |           z  S )zReturn complex sign of x.              ?)npabs)xs    T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_fgmres.py_mysignr      s"    CxxsRVAYY;    h㈵>c
           
         |	1|t          d          |	}d}
t          j        |
t          d           t	          | |||          \  } }}}}| j        d         }t          j        dd	           t          d
g|g          \  }|r!|r|}nd}||k    rt          d           |}|}n.d}|t          |d          }n||k    rt          d           |}|}|dk    rAt          j
        | t          j        dg|j                  z            } |||z            dfS || |z  z
  }t          |          }||g|dd<   t          |          }|dk    rd}|||z  k     r ||          dfS d}t          |          D ]>}|}t          |d                   |z  }|dxx         |z  cc<   |t          |          z  }t          j        d|z  f|j                  }t          j        ||f|j                  }t          j        ||f|j                  }t          j        ||f|j                  }||dddf<   t          j        |f|j                  }| |d<   t          |          D ]0}dt          j        ||                   z  |z  }||xx         dz  cc<   t%          j        |t          j
        |          ||dz
  dd           ||z  }||dd|f<   | |z  }t%          j        |t          j
        |          |d|dz   d           ||dz
  k    r||dz
  k     r||dz   ddf         }||dz   d         } t          |           }!|!dk    rct          | d                   |!z  }!||dz
  k     r/| ||dz   d<   ||dz   xx         |!z  cc<   |t          |          z  }|! ||dz   <   d||dz   d<   |dk    rt%          j        ||||           ||dz
  k    r||dz            dk    r |||         ||dz                      \  }"}#}t          j        |"|#gt          j        |#           |"gg|j                  }$t          j
        |$                                          ||dz  |dz   dz  <   t          j        |$|||dz                      |||dz   <   t          j        |$dddf         |||dz                      ||<   d||dz   <   |d|         |dd|f<   ||dz
  k     rt          j        ||dz                      }|||z  k     r n||                    |           |ot2          j                            |d|dz   d|dz   f         |d|dz                      }%t          j        |ddd|dz   f         |%          }& |||&z              |dz  }2t2          j                            |d|dz   d|dz   f         |d|dz                      }%t          j        |ddd|dz   f         |%          }&||&z   }|| |z  z
  }| ||           t          |          }||                    |           |dk    }'|'                                rJt          j        t          j        |&|'         ||'         z                      }(|(dk     r ||          dfc S |||z  k     r ||          dfc S @ ||          |fS )a"  Flexible Generalized Minimum Residual Method (fGMRES).

    fGMRES iteratively refines the initial solution guess to the
    system Ax = b.  fGMRES is flexible in the sense that the right
    preconditioner (M) can vary from iteration to iteration.

    Parameters
    ----------
    A : array, matrix, sparse matrix, LinearOperator
        n x n, linear system to solve
    b : array, matrix
        right hand side, shape is (n,) or (n,1)
    x0 : array, matrix
        initial guess, default is a vector of zeros
    tol : float
        Tolerance for stopping criteria, let r=r_k
        ||r|| < tol ||b||
        if ||b||=0, then set ||b||=1 for these tests.
    restart : None, int
        - if int, restart is max number of inner iterations
          and maxiter is the max number of outer iterations
        - if None, do not restart GMRES, and max number of inner iterations
          is maxiter
    maxiter : None, int
        - if restart is None, maxiter is the max number of inner iterations
          and GMRES does not restart
        - if restart is int, maxiter is the max number of outer iterations,
          and restart is the max number of inner iterations
        - defaults to min(n,40) if restart=None
    M : array, matrix, sparse matrix, LinearOperator
        n x n, inverted preconditioner, i.e. solve M A x = M b.
        M need not be stationary for fgmres
    callback : function
        User-supplied function is called after each iteration as
        callback(xk), where xk is the current solution vector
    residuals : list
        residual history in the 2-norm, including the initial residual
    reorth : boolean
        If True, then a check is made whether to re-orthogonalize the Krylov
        space each GMRES iteration
    restrt : None, int
        Deprecated.  See restart.

    Returns
    -------
    xk, info
    xk : an updated guess after k iterations to the solution of Ax = b
    info : halting status

            ==  =======================================
            0   successful exit
            >0  convergence to tolerance not achieved,
                return iteration count instead.
            <0  numerical breakdown, or illegal input
            ==  =======================================

    Notes
    -----
    The LinearOperator class is in scipy.sparse.linalg.
    Use this class if you prefer to define A or M as a mat-vec routine
    as opposed to explicitly constructing the matrix.

    fGMRES allows for non-stationary preconditioners, as opposed to GMRES

    For robustness, Householder reflections are used to orthonormalize
    the Krylov Space
    Givens Rotations are used to provide the residual norm each iteration
    Flexibility implies that the right preconditioner, M, can
    vary from iteration to iteration

    Examples
    --------
    >>> from pyamg.krylov import fgmres
    >>> from pyamg.util.linalg import norm
    >>> import numpy as np
    >>> from pyamg.gallery import poisson
    >>> A = poisson((10,10))
    >>> b = np.ones((A.shape[0],))
    >>> (x,flag) = fgmres(A,b, maxiter=2, tol=1e-8)
    >>> print(f'{norm(b - A*x):.6}')
    6.54282

    References
    ----------
    .. [1] Yousef Saad, "Iterative Methods for Sparse Linear Systems,
       Second Edition", SIAM, pp. 151-172, pp. 272-275, 2003
       http://www-users.cs.umn.edu/~saad/books.html

    Nz*Only use restart, not restrt (deprecated).zaThe keyword argument "restrt" is deprecated and will be removed in 2024.   Use "restart" instead.   )
stacklevelr   alwayszpyamg.krylov._fgmres)modulelartgz&Setting restart to maximum allowed, n.(   z&Setting maxiter to maximum allowed, n.r   )dtyper
      g       r   g-q=)
ValueErrorwarningsr   DeprecationWarningr   shapefilterwarningsr   minr   ravelarrayr   r   ranger   zeros	conjugater   apply_householdersapply_givenscopydotr   appendsplinalgsolveanymax))Abx0tolrestartmaxiterMcallback	residualsrestrtmsgr   postprocessnr   	max_outer	max_innerentryrnormrnormbniter_outerwbetaQHWZginnervvslicealphacsQblockyupdateindiceschanges)                                            r   fgmresrY      s   x IJJJ>c-!<<<< *!QA66Aq!Q	
A H-CDDDD y1#..GU   	IIIQ;;9:::G			?!RjjGGq[[9:::G	 	AvvRXse17;;;;<<AeG$$a(( 	
AE	AGGEw	!!! GGE||sU{A""
 E 	"" a' a'
 qt}}u$	!	T!WW Ha)m%QW555Hi+17;;;Hi^17333 Ha^17333!QQQ$ HaT)))u!9%% a	 a	E r|AeH---1AeHHHOHHH '28A;;57BKKK AA
 AaaahK AA '28A;;1eAgqIII !||IaK((%'111*A5788VA::#F1I..6E	!,,&,%'((%'


e+


T!WW #(AeAgJ"%AeAghhKqyy%aAu555 !||U1W:?? %ah%'
 ; ;IQ1X1va0@!/D&E,-G5 5 5F24(62B2B2G2G2I2IAuQw57A+./ (*vfaeAg6F'G'GAeE!Gm$  "vfQTlAeE!Gm4DEEAeH!$AeAgJ AiK.AaaahK y{""qqz**3;&&E($$U+++'	!U1W+q%'{*B(CQq%PQ'{^TTAVAaaa57lOQ77FHQZ(((QJEE IOOAaqk1eAg;671eAg;HH !!!QuQwY,++JAI HQKKKQ U### q&;;== 	,VBF6'?QwZ#?@@AAF~~#A++++ 3;KNNA&&&& 
 KNNE""r   )Nr   NNNNNN)__doc__r   r   numpyr   scipy.linalgr   scipyr-   util.linalgr   utilr    r   r   rY    r   r   <module>rb      s    J J            ) ) ) ) ) )                         #!%9=D# D# D# D# D# D#r   