
    _-Phv4                     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 )z'GMRES Housholder-based implementations.    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    _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_gmres_householder.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
  }||z  }t          |          }||g|dd<   t          |          }|dk    rd}nt          ||z            }|||z  k     r ||          dfS d}t          |          D ]}|}t          |d                   |z  }|d         |z   |d<   |t          |          z  |dd<   t          j        d|z  f|j                  }t          j        ||f|j                  }t          j        |dz   |f|j                  }||dddf<   t          j        |f|j                  }| |d<   t          |          D ]d}dt          j        ||                   z  |z  }||         dz   ||<   t%          j        |t          j
        |          ||dz
  dd           | |z  }||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    rht          | d                   |!z  }!||dz
  k     r4| ||dz   d<   ||dz   xx         |!z  cc<   |t          |          z  |dd<   |! ||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  }||dz
  k     rt          j        ||dz                      }|||z  k     r n||                    |           |t2          j                            |d|dz   d|dz   f         |d|dz                      }%t          j        |j        |j                  }&t%          j        |&t          j
        |          t          j
        |%          ||dd            |||&z              ft2          j                            |d|dz   d|dz   f         |d|dz                      }%t          j        |j        |j                  }&t%          j        |&t          j
        |          t          j
        |%          ||dd           ||&z   |dd<   || |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  Generalized Minimum Residual Method (GMRES) based on Housholder.

    GMRES iteratively refines the initial solution guess to the
    system Ax = b. Householder reflections are used for orthogonalization.
    Left preconditioning, leading to preconditioned residuals.

    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
        ||M r|| < tol ||M b||
        if ||b||=0, then set ||M 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.
    callback : function
        User-supplied function is called after each iteration as
        callback(xk), where xk is the current solution vector
    residuals : list
        preconditioned residual history in the 2-norm,
        including the initial preconditioned 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.

    For robustness, modified Gram-Schmidt is used to orthogonalize the
    Krylov Space Givens Rotations are used to provide the residual norm
    each iteration

    The residual is the *preconditioned* residual.

    Examples
    --------
    >>> from pyamg.krylov import gmres
    >>> 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) = gmres(A, b, maxiter=2, tol=1e-8, orthog='householder')
    >>> 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._gmres_householder)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householder_hornerschemeanymax))Abx0tolrestartmaxiterMcallback	residualsrestrtmsgr   postprocessnr   	max_outer	max_innerentryrnormrnormbnormMbniter_outerwbetaQHWginnervvslicealphacsQblockyupdateindiceschanges)                                            r   gmres_householderrZ      se   t IJJJ>c-!<<<< *!QA66Aq!Q	
A H-NOOOO y1#..GU   	IIIQ;;9:::G			?!RjjGGq[[9:::G	 	AvvRXse17;;;;<<AeG$$a(( 	
AE	A 	
AAGGEw	!!! GGE||a!e sV|A""
 E 	"" k' k'
 qt}}u$td{!477{!!! Ha)m%QW555Hi+17;;;Hik1%QW555!QQQ$ HaT)))u!9%% c	) c	)E r|AeH---1Ax#~AeH '28A;;57BKKK AA AA '28A;;1eAgqIII !||IaK((%'111*A5788VA::#F1I..6E	!,,&,%'((%'


e+


 477{!!! #(AeAgJ"%AeAghhKqyy%aAu555 !||U1W:?? %ah%'
 ; ;IQ1X1va0@!/D&E,-G5 5 5F68hv6F6F6K6K6M6MAuqyU1WM23 (*vfaeAg6F'G'GAeE!Gm$  "vfQTlAeE!Gm4DEEAeH!$AeAgJ AiK.AaaahKQJE y{""qqz**3<''E($$U+++'	!U1W+q%'{*B(CQq%PQ'{^TTAXagQW===F5fbhqkk28TU;;67BH H HHQZ((( IOOAaqk1eAg;671eAg;HH !'111
 	)&"(1++rx{{*+UB	< 	< 	< 6z!!!AI EQ HQKKK 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   rZ    r   r   <module>rc      s    - -            ) ) ) ) ) )                         *.,0DHR# R# R# R# R# R#r   