
    xf~w                        d Z g d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d	lmZ dd
lmZ dddddZdZdZd Zd Zd(dZ eddd       e       d)d              Z eddd       e       d)d              Z eddd        e       d)d!              Z ed"d#d$       e       d)d%              Z e       	 	 d*d&       Z e       	 	 d+d'       Zy),z,Iterative methods for solving linear systems)bicgbicgstabcgcgsgmresqmr    N)dedent   )
_iterative)LinearOperator)make_system)_aligned_zeros)non_reentrantsdcz)fr   FDzC
Parameters
----------
A : {sparse matrix, ndarray, LinearOperator}aX  b : ndarray
    Right hand side of the linear system. Has shape (N,) or (N,1).

Returns
-------
x : ndarray
    The converged solution.
info : integer
    Provides convergence information:
        0  : successful exit
        >0 : convergence to tolerance not achieved, number of iterations
        <0 : illegal input or breakdown

Other Parameters
----------------
x0 : ndarray
    Starting guess for the solution.
tol, atol : float, optional
    Tolerances for convergence, ``norm(residual) <= max(tol*norm(b), atol)``.
    The default for ``atol`` is ``'legacy'``, which emulates
    a different legacy behavior.

    .. warning::

       The default value for `atol` will be changed in a future release.
       For future compatibility, specify `atol` explicitly.
maxiter : integer
    Maximum number of iterations.  Iteration will stop after maxiter
    steps even if the specified tolerance has not been achieved.
M : {sparse matrix, ndarray, LinearOperator}
    Preconditioner for A.  The preconditioner should approximate the
    inverse of A.  Effective preconditioning dramatically improves the
    rate of convergence, which implies that fewer iterations are needed
    to reach a given error tolerance.
callback : function
    User-supplied function to call after each iteration.  It is called
    as callback(xk), where xk is the current solution vector.
c                 Z    t         j                  j                  |       }||k  r|dfS |dfS )z;
    Successful termination condition for the solvers.
    r
   r   nplinalgnorm)residualatolresids      G/usr/lib/python3/dist-packages/scipy/sparse/linalg/_isolve/iterative.py	_stoptestr    C   s/     IINN8$E}axax    c                    |.t        j                  dj                  |      t        d       d}t	        |       } |dk(  r" |       }|| k  ry|dk(  r| S | t	        |      z  S t        t	        |      | t	        |      z        S )a  
    Parse arguments for absolute tolerance in termination condition.

    Parameters
    ----------
    tol, atol : object
        The arguments passed into the solver routine by user.
    bnrm2 : float
        2-norm of the rhs vector.
    get_residual : callable
        Callable ``get_residual()`` that returns the initial value of
        the residual.
    routine_name : str
        Name of the routine.
    a	  scipy.sparse.linalg.{name} called without specifying `atol`. The default value will be changed in a future release. For compatibility, specify a value for `atol` explicitly, e.g., ``{name}(..., atol=0)``, or to retain the old behavior ``{name}(..., atol='legacy')``)name   category
stacklevellegacyexitr   )warningswarnformatDeprecationWarningfloatmax)tolr   bnrm2get_residualroutine_namer   s         r   	_get_atolr4   N   s    " | 7 8>v<v7P1a	A 
*CxC<A:Ju%%5;eEl 233r!   c                       fd}|S )Nc           	          dj                  t        dj                  dd      z   t        t	              f      | _        | S )N
z    z
    )joincommon_doc1replacecommon_doc2r	   __doc__)fnAinfofooterheaders    r   combinezset_docstring.<locals>.combinex   s?    YY &tX)F F +VF^ = >
 	r!    )r@   r>   r?   atol_defaultrA   s   ```  r   set_docstringrD   w   s    
 Nr!   z7Use BIConjugate Gradient iteration to solve ``Ax = b``.zThe real or complex N-by-N matrix of the linear system.
Alternatively, ``A`` can be a linear operator which can
produce ``Ax`` and ``A^T x`` using, e.g.,
``scipy.sparse.linalg.LinearOperator``.a1                 Examples
               --------
               >>> import numpy as np
               >>> from scipy.sparse import csc_matrix
               >>> from scipy.sparse.linalg import bicg
               >>> A = csc_matrix([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
               >>> b = np.array([2, 4, -1], dtype=float)
               >>> x, exitCode = bicg(A, b)
               >>> print(exitCode)            # 0 indicates successful convergence
               0
               >>> np.allclose(A.dot(x), b)
               True

               )r?   c                    t        | ||      \  } }}t              }	||	dz  }| j                  | j                  c}
|j                  |j                  }}t        j
                  j                     }t        t        |dz         }fd}t        ||t        j                  j                        |d      }|dk(  r
 |      dfS |}d}d}t        d	|	z  j
                  
      }d}d}d}|}	 |} ||||||||	      \	  }}}}}}}}|||kD  r |       t        |dz
  |dz
  |	z         }t        |dz
  |dz
  |	z         }|dk(  r| |       n|dk(  r'||xx   |z  cc<   ||xx   | ||         z  z  cc<   n|dk(  r'||xx   |z  cc<   ||xx   | |
||         z  z  cc<   nn|dk(  r |||         ||<   nZ|dk(  r |||         ||<   nF|dk(  r$||xx   |z  cc<   ||xx   |       z  z  cc<   n|d	k(  r|rd}d}t        ||   |      \  }}d}/|dkD  r||k(  r||k  s|} |      |fS )N
   
bicgrevcomc                  T    t         j                  j                          z
        S Nr   bmatvecxs   r   r2   zbicg.<locals>.get_residual       yy~~fQi!m,,r!   r   r)   r   r
      dtypeT      r$      F)r   lenrL   rmatvec
_type_convrR   chargetattrr   r4   r   r   r   r   slicer    )ArK   x0r0   maxiterMcallbackr   postprocessnrW   psolverpsolveltrrevcomr2   r   ndx1ndx2workijobinfoftflagiter_olditersclr1sclr2slice1slice2rL   rM   s    `                           @@r   r   r      s   . &aB2Aa!KAAB$hh		OFGhh		GF
QWW\\
"CZ|!34F-S$		q 1<HDv~1~q  EDD!A#AGG,DDDFE
!QeUD$dC 	>5%tT5%EGOQKtAvtAvax(tAvtAvax(BJ#aiLE!LLE&f"666LaiLE!LLE'$v,"777Lai!$v,/DLai"4<0DLaiLE!LLE&)O+Lai#DL$7KE4= @ axEW$etmq>4r!   zBUse BIConjugate Gradient STABilized iteration to solve ``Ax = b``.zThe real or complex N-by-N matrix of the linear system.
Alternatively, ``A`` can be a linear operator which can
produce ``Ax`` using, e.g.,
``scipy.sparse.linalg.LinearOperator``.a                 Examples
               --------
               >>> import numpy as np
               >>> from scipy.sparse import csc_matrix
               >>> from scipy.sparse.linalg import bicgstab
               >>> R = np.array([[4, 2, 0, 1],
               ...               [3, 0, 0, 2],
               ...               [0, 1, 1, 1],
               ...               [0, 2, 1, 0]])
               >>> A = csc_matrix(R)
               >>> b = np.array([-1, -0.5, -1, 2])
               >>> x, exit_code = bicgstab(A, b)
               >>> print(exit_code)  # 0 indicates successful convergence
               0
               >>> np.allclose(A.dot(x), b)
               True
               c                    t        | ||      \  } }}t              }	||	dz  }| j                  |j                  }
t        j                  j
                     }t        t        |dz         }fd}t        ||t        j                  j                        |d      }|dk(  r
 |      dfS |}d}d}t        d	|	z  j                  
      }d}d}d}|}	 |} ||||||||	      \	  }}}}}}}}|||kD  r |       t        |dz
  |dz
  |	z         }t        |dz
  |dz
  |	z         }|dk(  r| |       n|dk(  r'||xx   |z  cc<   ||xx   | ||         z  z  cc<   nZ|dk(  r |
||         ||<   nF|dk(  r$||xx   |z  cc<   ||xx   |       z  z  cc<   n|dk(  r|rd}d}t        ||   |      \  }}d}|dkD  r||k(  r||k  s|} |      |fS )NrF   bicgstabrevcomc                  T    t         j                  j                          z
        S rI   r   rJ   s   r   r2   zbicgstab.<locals>.get_residual   rN   r!   r   r)   r   r
   rO      rQ   TrS   rT   r$   Fr   rV   rL   rX   rR   rY   rZ   r   r4   r   r   r   r   r[   r    r\   rK   r]   r0   r^   r_   r`   r   ra   rb   rc   re   rf   r2   r   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rL   rM   s    `                         @@r   r   r      s@   4 *!QA6Aq!QAAB$XXFXXF
QWW\\
"CZ'7!78F-S$		q 1<LDv~1~q  EDD!A#AGG,DDDFE
!QeUD$dC 	>5%tT5%EGOQKtAvtAvax(tAvtAvax(BJ#aiLE!LLE&f"666Lai!$v,/DLaiLE!LLE&)O+Lai#DL$7KE43 6 axEW$etmq>4r!   z5Use Conjugate Gradient iteration to solve ``Ax = b``.zThe real or complex N-by-N matrix of the linear system.
``A`` must represent a hermitian, positive definite matrix.
Alternatively, ``A`` can be a linear operator which can
produce ``Ax`` using, e.g.,
``scipy.sparse.linalg.LinearOperator``.a                 Examples
               --------
               >>> import numpy as np
               >>> from scipy.sparse import csc_matrix
               >>> from scipy.sparse.linalg import cg
               >>> P = np.array([[4, 0, 1, 0],
               ...               [0, 5, 0, 0],
               ...               [1, 0, 3, 2],
               ...               [0, 0, 2, 4]])
               >>> A = csc_matrix(P)
               >>> b = np.array([-1, -0.5, -1, 2])
               >>> x, exit_code = cg(A, b)
               >>> print(exit_code)    # 0 indicates successful convergence
               0
               >>> np.allclose(A.dot(x), b)
               True

               c                 "   t        | ||      \  } }}t              }	||	dz  }| j                  |j                  }
t        j                  j
                     }t        t        |dz         }fd}t        ||t        j                  j                        |d      }|dk(  r
 |      dfS |}d}d}t        d	|	z  j                  
      }d}d}d}|}	 |} ||||||||	      \	  }}}}}}}}|||kD  r |       t        |dz
  |dz
  |	z         }t        |dz
  |dz
  |	z         }|dk(  r| |       n|dk(  r'||xx   |z  cc<   ||xx   | ||         z  z  cc<   n|dk(  r |
||         ||<   np|dk(  r$||xx   |z  cc<   ||xx   |       z  z  cc<   nG|d	k(  rB|rd}d}t        ||   |      \  }}|dk(  r%|dkD  r        z
  ||<   t        ||   |      \  }}d}|dkD  r||k(  r||k  s|} |      |fS )NrF   cgrevcomc                  T    t         j                  j                          z
        S rI   r   rJ   s   r   r2   zcg.<locals>.get_residualT  rN   r!   r   r)   r   r
   rO   r$   rQ   TrS   rT   Frw   rx   s    `                         @@r   r   r   .  st   6 *!QA6Aq!QAAB$XXFXXF
QWW\\
"CZz!12F-S$		q 1<FDv~1~q  EDD!A#AGG,DDDFE
!QeUD$dC 	>5%tT5%EGOQKtAvtAvax(tAvtAvax(BJ#aiLE!LLE&f"666Lai!$v,/DLaiLE!LLE&)O+Lai#DL$7KE4qyUQY  !6!9}V'Vd;t= @ axEW$etmq>4r!   z=Use Conjugate Gradient Squared iteration to solve ``Ax = b``.zThe real-valued N-by-N matrix of the linear system.
Alternatively, ``A`` can be a linear operator which can
produce ``Ax`` using, e.g.,
``scipy.sparse.linalg.LinearOperator``.a                 Examples
               --------
               >>> import numpy as np
               >>> from scipy.sparse import csc_matrix
               >>> from scipy.sparse.linalg import cgs
               >>> R = np.array([[4, 2, 0, 1],
               ...               [3, 0, 0, 2],
               ...               [0, 1, 1, 1],
               ...               [0, 2, 1, 0]])
               >>> A = csc_matrix(R)
               >>> b = np.array([-1, -0.5, -1, 2])
               >>> x, exit_code = cgs(A, b)
               >>> print(exit_code)  # 0 indicates successful convergence
               0
               >>> np.allclose(A.dot(x), b)
               True
               c                 d   t        | ||      \  } }}t              }	||	dz  }| j                  |j                  }
t        j                  j
                     }t        t        |dz         }fd}t        ||t        j                  j                        |d      }|dk(  r
 |      dfS |}d}d}t        d	|	z  j                  
      }d}d}d}|}	 |} ||||||||	      \	  }}}}}}}}|||kD  r |       t        |dz
  |dz
  |	z         }t        |dz
  |dz
  |	z         }|dk(  r| |       n|dk(  r'||xx   |z  cc<   ||xx   | ||         z  z  cc<   n|dk(  r |
||         ||<   np|dk(  r$||xx   |z  cc<   ||xx   |       z  z  cc<   nG|dk(  rB|rd}d}t        ||   |      \  }}|dk(  r%|dkD  r        z
  ||<   t        ||   |      \  }}d}|dk(  rt               z
  |      \  }}|rd}|dkD  r||k(  r||k  s|} |      |fS )NrF   	cgsrevcomc                  T    t         j                  j                          z
        S rI   r   rJ   s   r   r2   zcgs.<locals>.get_residual  rN   r!   r   r)   r   r
   rO   rv   rQ   TrS   rT   r$   Firw   )r\   rK   r]   r0   r^   r_   r`   r   ra   rb   rc   re   rf   r2   r   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   okrL   rM   s    `                          @@r   r   r     s   4 *!QA6Aq!QAAB$XXFXXF
QWW\\
"CZ{!23F-S$		q 1<GDv~1~q  EDD!A#AGG,DDDFE
!QeUD$dC 	>5%tT5%EGOQKtAvtAvax(tAvtAvax(BJ#aiLE!LLE&f"666Lai!$v,/DLaiLE!LLE&)O+Lai#DL$7KE4qyUQY  !6!9}V'Vd;t= @ s{a&)mT2	rDaxEW$etmq>4r!   c                   )* ||}n+|t        d      d}t        j                  |t        d       ||
t        j                  dt        d       |
d}
|
d	vrt        d
|
      |d}
t	        | ||      \  } }*}t              }||dz  }|d}t        ||      }| j                  )|j                  }t        *j                  j                     }t        t        |dz         }t        j                  j                        }t        j                  j                   |            })*fd}t!        ||	||d      }	|	dk(  r
 |*      dfS |dk(  r
 |      dfS d}|t        ||	|z        z  }t        j"                  }t        j"                  }d}d}t%        d|z   |z  *j                        }t%        |dz   d|z  dz   z  *j                        }d}d}d}|}|} d}!d}"d}#	 |}$ |*||||||||||      \	  *}}}}}}%}&}|
dk(  r||$k7  r |*       t'        |dz
  |dz
  |z         }'t'        |dz
  |dz
  |z         }(|dk(  r |
dv r|"r |||z         n|
dk(  r |*       n|dk(  r$||(xx   |&z  cc<   ||(xx   |% )*      z  z  cc<   n|dk(  r |||(         ||'<   |!s| dk(  rd}"d}!n|dk(  r?||(xx   |&z  cc<   ||(xx   |% )||'         z  z  cc<   |"rw|
dv r |||z         d}"|#dz   }#n`|dk(  r[|rd}d}t)        ||'   |	      \  }}|s||kD  rt        dd|z        }nt+        dd|z        }|dk7  r|t        ||	|z        z  }n||z  }|} d}|
dk(  r|#|kD  r|}n|dk\  r||	k  s|} |*      |fS ) aU  
    Use Generalized Minimal RESidual iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse matrix, ndarray, LinearOperator}
        The real or complex N-by-N matrix of the linear system.
        Alternatively, ``A`` can be a linear operator which can
        produce ``Ax`` using, e.g.,
        ``scipy.sparse.linalg.LinearOperator``.
    b : ndarray
        Right hand side of the linear system. Has shape (N,) or (N,1).

    Returns
    -------
    x : ndarray
        The converged solution.
    info : int
        Provides convergence information:
          * 0  : successful exit
          * >0 : convergence to tolerance not achieved, number of iterations
          * <0 : illegal input or breakdown

    Other parameters
    ----------------
    x0 : ndarray
        Starting guess for the solution (a vector of zeros by default).
    tol, atol : float, optional
        Tolerances for convergence, ``norm(residual) <= max(tol*norm(b), atol)``.
        The default for ``atol`` is ``'legacy'``, which emulates
        a different legacy behavior.

        .. warning::

           The default value for `atol` will be changed in a future release.
           For future compatibility, specify `atol` explicitly.
    restart : int, optional
        Number of iterations between restarts. Larger values increase
        iteration cost, but may be necessary for convergence.
        Default is 20.
    maxiter : int, optional
        Maximum number of iterations (restart cycles).  Iteration will stop
        after maxiter steps even if the specified tolerance has not been
        achieved.
    M : {sparse matrix, ndarray, LinearOperator}
        Inverse of the preconditioner of A.  M should approximate the
        inverse of A and be easy to solve for (see Notes).  Effective
        preconditioning dramatically improves the rate of convergence,
        which implies that fewer iterations are needed to reach a given
        error tolerance.  By default, no preconditioner is used.
        In this implementation, left preconditioning is used,
        and the preconditioned residual is minimized.
    callback : function
        User-supplied function to call after each iteration.  It is called
        as `callback(args)`, where `args` are selected by `callback_type`.
    callback_type : {'x', 'pr_norm', 'legacy'}, optional
        Callback function argument requested:
          - ``x``: current iterate (ndarray), called on every restart
          - ``pr_norm``: relative (preconditioned) residual norm (float),
            called on every inner iteration
          - ``legacy`` (default): same as ``pr_norm``, but also changes the
            meaning of 'maxiter' to count inner iterations instead of restart
            cycles.
    restrt : int, optional, deprecated

        .. deprecated:: 0.11.0
           `gmres` keyword argument `restrt` is deprecated infavour of
           `restart` and will be removed in SciPy 1.12.0.

    See Also
    --------
    LinearOperator

    Notes
    -----
    A preconditioner, P, is chosen such that P is close to A but easy to solve
    for. The preconditioner parameter required by this routine is
    ``M = P^-1``. The inverse should preferably not be calculated
    explicitly.  Rather, use the following template to produce M::

      # Construct a linear operator that computes P^-1 @ x.
      import scipy.sparse.linalg as spla
      M_x = lambda x: spla.spsolve(P, x)
      M = spla.LinearOperator((n, n), M_x)

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_matrix
    >>> from scipy.sparse.linalg import gmres
    >>> A = csc_matrix([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
    >>> b = np.array([2, 4, -1], dtype=float)
    >>> x, exitCode = gmres(A, b)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    zOCannot specify both restart and restrt keywords. Preferably use 'restart' only.zj'gmres' keyword argument 'restrt' is deprecated infavour of 'restart' and will be removed in SciPy 1.12.0.rS   )r'   a4  scipy.sparse.linalg.gmres called without specifying `callback_type`. The default value will be changed in a future release. For compatibility, specify a value for `callback_type` explicitly, e.g., ``{name}(..., callback_type='pr_norm')``, or to retain the old behavior ``{name}(..., callback_type='legacy')``rT   r%   r(   )rM   pr_normr(   zUnknown callback_type: nonerF      gmresrevcomc                  T    t         j                  j                          z
        S rI   r   rJ   s   r   r2   zgmres.<locals>.get_residual  rN   r!   r   r)   r   g      ?r
   rO   rP   rQ   TFrM   )r   r(   r$   g      ?gؗҜ<g      ?)
ValueErrorr*   r+   r-   r   rV   minrL   rX   rR   rY   rZ   r   r   r   r   r4   nanr   r[   r    r/   )+r\   rK   r]   r0   restartr^   r_   r`   restrtr   callback_typemsgra   rb   rc   re   rf   r1   Mb_nrm2r2   ptol_max_factorptolr   presidrg   rh   ri   work2rj   rk   rl   rm   old_ijob
first_passresid_readyiter_numrn   ro   rp   rq   rr   rL   rM   s+    `                                       @@r   r   r     sI   N ~		 : ; 	;@c-!< 5 	 @
  2a	A  662=2CDEE(Ar15Aq!Q{AAB$~^FXXFXXF
QWW\\
"CZ}!45FIINN1EiinnVAY'G-S$|W=Dv~1~q  z1~q   OS$,77DFFEVVFDD1V8Q,QWW5DF1Hqxz2AEDDFEHJKH
!QeUFD$dTXY 	?5&$dE5$CEW$4QKtAvtAvax(tAvtAvax(BJ 55Ve^,#%aiLE!LLE&)O+Lai!$v,/DL(a-"JaiLE!LLE&f"666L $99Ve^,##A:ai#DL$7KE4 v}"%c3+@"A #&eTO-C"DzOTE\ BB/H$'!u x qy%4-q>4r!   c	                    !"  !t         d|      \   }	"}
||t        !d      rE!fd}!fd}!fd}!fd}t         j                  ||      }t         j                  ||      }n3d }t         j                  ||      }t         j                  ||      }t	              }||d	z  }t
        "j                  j                     }t        t        |d
z         } "fd}t        ||t        j                  j                        |d      }|dk(  r
 |
"      dfS |}d}d}t        d|z  "j                        }d}d}d}|}	 |} |"|||||||	      \	  "}}}}}}}}|||kD  r |"       t        |dz
  |dz
  |z         }t        |dz
  |dz
  |z         } |dk(  r| |"       n1|dk(  r0|| xx   |z  cc<   || xx   | j!                  ||         z  z  cc<   n|dk(  r0|| xx   |z  cc<   || xx   | j#                  ||         z  z  cc<   n|dk(  r|j!                  ||          ||<   n|dk(  r|j!                  ||          ||<   n|dk(  r|j#                  ||          ||<   nl|dk(  r|j#                  ||          ||<   nO|dk(  r-|| xx   |z  cc<   || xx   | j!                  "      z  z  cc<   n|dk(  r|rd}d}t%        ||   |      \  }}d}|dkD  r||k(  r||k  s|} |
"      |fS )a	  Use Quasi-Minimal Residual iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse matrix, ndarray, LinearOperator}
        The real-valued N-by-N matrix of the linear system.
        Alternatively, ``A`` can be a linear operator which can
        produce ``Ax`` and ``A^T x`` using, e.g.,
        ``scipy.sparse.linalg.LinearOperator``.
    b : ndarray
        Right hand side of the linear system. Has shape (N,) or (N,1).

    Returns
    -------
    x : ndarray
        The converged solution.
    info : integer
        Provides convergence information:
            0  : successful exit
            >0 : convergence to tolerance not achieved, number of iterations
            <0 : illegal input or breakdown

    Other Parameters
    ----------------
    x0 : ndarray
        Starting guess for the solution.
    tol, atol : float, optional
        Tolerances for convergence, ``norm(residual) <= max(tol*norm(b), atol)``.
        The default for ``atol`` is ``'legacy'``, which emulates
        a different legacy behavior.

        .. warning::

           The default value for `atol` will be changed in a future release.
           For future compatibility, specify `atol` explicitly.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M1 : {sparse matrix, ndarray, LinearOperator}
        Left preconditioner for A.
    M2 : {sparse matrix, ndarray, LinearOperator}
        Right preconditioner for A. Used together with the left
        preconditioner M1.  The matrix M1@A@M2 should have better
        conditioned than A alone.
    callback : function
        User-supplied function to call after each iteration.  It is called
        as callback(xk), where xk is the current solution vector.

    See Also
    --------
    LinearOperator

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_matrix
    >>> from scipy.sparse.linalg import qmr
    >>> A = csc_matrix([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
    >>> b = np.array([2, 4, -1], dtype=float)
    >>> x, exitCode = qmr(A, b)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    Nrc   c                 (    j                  | d      S Nleftrc   rK   A_s    r   left_psolvezqmr.<locals>.left_psolve(  s    yy6**r!   c                 (    j                  | d      S Nrightr   r   s    r   right_psolvezqmr.<locals>.right_psolve+  s    yy7++r!   c                 (    j                  | d      S r   rd   r   s    r   left_rpsolvezqmr.<locals>.left_rpsolve.  s    zz!F++r!   c                 (    j                  | d      S r   r   r   s    r   right_rpsolvezqmr.<locals>.right_rpsolve1  s    zz!G,,r!   )rL   rW   c                     | S rI   rB   )rK   s    r   idzqmr.<locals>.id6  s    r!   rF   	qmrrevcomc                  f    t         j                  j                   j                        z
        S rI   )r   r   r   rL   )r\   rK   rM   s   r   r2   zqmr.<locals>.get_residualB  s"    yy~~ahhqkAo..r!   r   r)   r   r
   rO      TrS   rT   r$   rU   rP   rv      F)r   hasattrr   shaperV   rX   rR   rY   rZ   r   r4   r   r   r   r   r[   rL   rW   r    )#r\   rK   r]   r0   r^   M1M2r`   r   r_   ra   r   r   r   r   r   rb   re   rf   r2   r   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   r   rM   s#   ``                               @@r   r   r     sB   H 
B)!T2q9Aq!Q	zbj2h+,,-\RBmTBB?BB?BAAB$
QWW\\
"CZ{!23F/S$		q 1<GDv~1~q  EDD"Q$qww'DDDFE
!QeUD$dC 	>5%tT5%EGOQKtAvtAvax(tAvtAvax(BJ#aiLE!LLE!((4<"888LaiLE!LLE!))DL"999Lai99T&\2DLai99T&\2DLai::d6l3DLai::d6l3DLaiLE!LLE!((1+--Lai#DL$7KE4E H axEW$etmq>4r!   ) 0)Nh㈵>NNNN)	Nr   NNNNNNN)Nr   NNNNN)r<   __all__r*   textwrapr	   numpyr   r   r   scipy.sparse.linalg._interfacer   utilsr   scipy._lib._utilr   scipy._lib._threadsafetyr   rX   r9   r;   r    r4   rD   r   r   r   r   r   r   rB   r!   r   <module>r      se   2
6     9  + 23CS1

0 % R&4R H9* ?  +,? D  90 :  12: z F9
2 ?  34? D N90 E  12E P PT04p  p f JNY  Y r!   