
    _MhE              	           d dl Z d dlZd dlmZ ddlmZ d dlmZ g dZ	dd	Z
ddddddd
dZddddddd
dZddddddd
dZddddddd
dZddddddddddZdddddddddZdS )    N)LinearOperator   )make_system)get_lapack_funcs)bicgbicgstabcgcgsgmresqmr        h㈵>c                     |dk    s||dk     rd|  d| d}t          |          t          t          |          t          |          t          |          z            }||fS )z=
    A helper function to handle tolerance normalization
    legacyNr   z'scipy.sparse.linalg.z' called with invalid `atol`=z5; if set, `atol` must be a real, non-negative number.)
ValueErrormaxfloat)nameb_normatolrtolmsgs        e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_isolve/iterative.py_get_atol_rtolr   
   s     x4<4!88Et E E$ E E EoouT{{E$KK%--788D:    )r   r   maxiterMcallbackc                ,   t          | |||          \  } }}}}	t          j                            |          }
t	          d|
||          \  }}|
dk    r |	|          dfS t          |          }t          j        |          rt          j        nt          j        }||dz  }| j	        | j
        }}|j	        |j
        }}t          j        |j        j                  j        dz  }d\  }}}|                                r| ||          z
  n|                                }|                                }t#          |          D ]D}t          j                            |          |k     r |	|          dfc S  ||          } ||          } |||          }t          j        |          |k     r|	dfc S |dk    r,||z  }||z  }||z  }||                                z  }||z  }n(|                                }|                                } ||          } ||          } |||          }|dk    r |	|          dfc S ||z  } || |z  z  }|| |z  z  }||                                 |z  z  }|}|r ||           F |	|          |fS )	a	  Use BIConjugate Gradient iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, 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`` 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).
    x0 : ndarray
        Starting guess for the solution.
    rtol, atol : float, optional
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse array, ndarray, LinearOperator}
        Preconditioner for `A`. It should approximate the
        inverse of `A` (see Notes). 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.

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

    Notes
    -----
    The preconditioner `M` should be a matrix such that ``M @ A`` has a smaller
    condition number than `A`, see [1]_ .

    References
    ----------
    .. [1] "Preconditioner", Wikipedia, 
           https://en.wikipedia.org/wiki/Preconditioner
    .. [2] "Biconjugate gradient method", Wikipedia, 
           https://en.wikipedia.org/wiki/Biconjugate_gradient_method

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> from scipy.sparse.linalg import bicg
    >>> A = csc_array([[3, 2, 0], [1, -1, 0], [0, 5, 1.]])
    >>> b = np.array([2., 4., -1.])
    >>> x, exitCode = bicg(A, b, atol=1e-5)
    >>> print(exitCode)  # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    r   r   N
      )NNN)r   nplinalgnormr   leniscomplexobjvdotdotmatvecrmatvecfinfodtypecharepsanycopyrangeabsconj)!Abx0r   r   r   r   r   xpostprocessbnrm2_ndotprodr+   r,   psolverpsolverhotolrho_prevpptilderrtilde	iterationzztilderho_curbetaqqtildervalphas!                                    r   r   r      s   B *!QA66Aq!QINN1EVUD$77GD!zz{1~~q  AA++7bggGB$h	GFh	GFXagl##'*F +Ha.FF1IIaffhhAVVXXF7^^ (' ('	9>>!t##;q>>1$$$$F1II'&!$$6'??V######q==X%DIAFAdiikk!FfFFA[[]]FF1IIWVQ77;q>>3&&&&"	U1W	U1W%**,,v%% 	HQKKK {1~~w&&r   c                   t          | |||          \  } }}}}	t          j                            |          }
t	          d|
||          \  }}|
dk    r |	|          dfS t          |          }t          j        |          rt          j        nt          j        }||dz  }| j	        }|j	        }t          j
        |j        j                  j        dz  }|}d\  }}}}}|                                r| ||          z
  n|                                }|                                }t!          |          D ]}t          j                            |          |k     r |	|          dfc S  |||          }t          j        |          |k     r |	|          dfc S |dk    rEt          j        |          |k     r |	|          dfc S ||z  ||z  z  }|||z  z  }||z  }||z  }n(t          j        |          }|                                } ||          } ||          } |||          }|dk    r |	|          dfc S ||z  }|||z  z  }|dd         |dd<   t          j                            |          |k     r|||z  z  } |	|          dfc S  ||          } ||          }  || |           || |           z  }|||z  z  }|||z  z  }||| z  z  }|}|r ||            |	|          |fS )	a.
  Use BIConjugate Gradient STABilized iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, 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`` 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).
    x0 : ndarray
        Starting guess for the solution.
    rtol, atol : float, optional
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse array, ndarray, LinearOperator}
        Preconditioner for `A`. It should approximate the
        inverse of `A` (see Notes). 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.

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

    Notes
    -----
    The preconditioner `M` should be a matrix such that ``M @ A`` has a smaller
    condition number than `A`, see [1]_ .

    References
    ----------
    .. [1] "Preconditioner", Wikipedia, 
           https://en.wikipedia.org/wiki/Preconditioner
    .. [2] "Biconjugate gradient stabilized method", 
           Wikipedia, https://en.wikipedia.org/wiki/Biconjugate_gradient_stabilized_method

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> 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_array(R)
    >>> b = np.array([-1, -0.5, -1, 2])
    >>> x, exit_code = bicgstab(A, b, atol=1e-5)
    >>> print(exit_code)  # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    r   r   Nr    r!   NNNNNr"   r#   r   r$   r%   r&   r   r'   r(   r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   r4   
empty_like)!r6   r7   r8   r   r   r   r   r   r9   r:   r;   r<   r=   r>   r+   r?   rA   omegatolrB   omegarO   rC   vrE   rF   rG   rhorK   sphatrN   shatts!                                    r   r   r      sf   L *!QA66Aq!QINN1EZd;;GD!zz{1~~q  AA++7bggGB$XFXF Xagl##'*FH $@ HeUAq.FF1IIaffhhAVVXXF7^^ .' .'	9>>!t##;q>>1$$$$gfa  6#;;;q>>3&&&&q==ve}}x''"{1~~s****(Nuu}5DqLAIAFAAa  AAvayyF4LLWVQ77;q>>3&&&&b	U1Wt!!!9>>!t##tOA;q>>1$$$$vayyF4LL11-	U4Z	U4Z	U1W 	HQKKK {1~~w&&r   c                   t          | |||          \  } }}}}	t          j                            |          }
t	          d|
||          \  }}|
dk    r |	|          dfS t          |          }||dz  }t          j        |          rt          j        nt          j        }| j	        }|j	        }|
                                r| ||          z
  n|                                }d\  }}t          |          D ]}t          j                            |          |k     r |	|          dfc S  ||          } |||          }|dk    r||z  }||z  }||z  }n#t          j        |          }|dd         |dd<    ||          }| |||          z  }|||z  z  }|||z  z  }|}|r ||            |	|          |fS )aL
  Use Conjugate Gradient iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, ndarray, LinearOperator}
        The 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``.
    b : ndarray
        Right hand side of the linear system. Has shape (N,) or (N,1).
    x0 : ndarray
        Starting guess for the solution.
    rtol, atol : float, optional
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse array, ndarray, LinearOperator}
        Preconditioner for `A`. `M` must represent a hermitian, positive definite
        matrix. It should approximate the inverse of `A` (see Notes).
        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.

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

    Notes
    -----
    The preconditioner `M` should be a matrix such that ``M @ A`` has a smaller
    condition number than `A`, see [2]_.

    References
    ----------
    .. [1] "Conjugate Gradient Method, Wikipedia, 
           https://en.wikipedia.org/wiki/Conjugate_gradient_method
    .. [2] "Preconditioner", 
           Wikipedia, https://en.wikipedia.org/wiki/Preconditioner

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> 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_array(P)
    >>> b = np.array([-1, -0.5, -1, 2])
    >>> x, exit_code = cg(A, b, atol=1e-5)
    >>> print(exit_code)    # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    r	   r   Nr    )NN)r   r$   r%   r&   r   r'   r(   r)   r*   r+   r1   r2   r3   rS   )r6   r7   r8   r   r   r   r   r   r9   r:   r;   r<   r=   r>   r+   r?   rE   rB   rC   rG   rH   rJ   rK   rL   rO   s                            r   r	   r	   1  s   L *!QA66Aq!QINN1ET5$55GD!zz{1~~q  AAB$++7bggGXFXF.FF1IIaffhhA KHa7^^ ' '	9>>!t##;q>>1$$$$F1II'!Q--q==X%DIAFAAa  AQQQ4AaaaDF1II''!Q--'	U1W	U1W 	HQKKK {1~~w&&r   c                   t          | |||          \  } }}}}	t          j                            |          }
t	          d|
||          \  }}|
dk    r |	|          dfS t          |          }t          j        |          rt          j        nt          j        }||dz  }| j	        }|j	        }t          j
        |j        j                  j        dz  }|                                r| ||          z
  n|                                }|                                }t          j                            |          }|dk    rd}d\  }}}}t!          |          D ]Z}t          j                            |          }||k     r |	|          dfc S  |||          }t          j        |          |k     r|	dfc S |dk    r1||z  }|dd         |dd<   |||z  z  }||z  }||z  }||z  }||z  }n<|                                }|                                }t          j        |          } ||          } ||          } |||          }|dk    r |	|          d	fc S ||z  }|dd         |dd<   |||z  z  } |||z             } ||| z  z  }| ||          z
  }|}|r ||           \ |	|          |fS )
a	  Use Conjugate Gradient Squared iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, ndarray, LinearOperator}
        The 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``.
    b : ndarray
        Right hand side of the linear system. Has shape (N,) or (N,1).
    x0 : ndarray
        Starting guess for the solution.
    rtol, atol : float, optional
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse array, ndarray, LinearOperator}
        Preconditioner for ``A``. It should approximate the
        inverse of `A` (see Notes). 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.

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

    Notes
    -----
    The preconditioner `M` should be a matrix such that ``M @ A`` has a smaller
    condition number than `A`, see [1]_.

    References
    ----------
    .. [1] "Preconditioner", Wikipedia, 
           https://en.wikipedia.org/wiki/Preconditioner
    .. [2] "Conjugate gradient squared", Wikipedia,
           https://en.wikipedia.org/wiki/Conjugate_gradient_squared_method

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> 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_array(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
    r
   r   Nr    r!   r   )NNNNr"   r#   rR   )!r6   r7   r8   r   r   r   r   r   r9   r:   r;   r<   r=   r>   r+   r?   rA   rE   rF   bnormrB   rC   urL   rG   rnormrJ   rK   rY   vhatrN   rO   uhats!                                    r   r
   r
     s   J *!QA66Aq!QINN1EUE466GD!zz{1~~q  AA++7bggGB$XFXFXagl##'*F.FF1IIaffhhAVVXXFINN1Ezz /HaA7^^ 7' 7'		q!!4<<;q>>1$$$$'&!$$6'??V######q==X%D QQQ4AaaaDaKAIAFAIAFAA AAa  Avayyvd||WVT""77;q>>3&&&&"t!!!	U4Zva!e}}	U4Z q		M 	HQKKK {1~~w&&r   )r   r   restartr   r   r   callback_typec          
      6   | |	d}
t          j        |
t          d           |	d}	|	dvrt          d|	          |d}	t	          | |||          \  } }}}}| j        }|j        }t          |          }t          j        	                    |          }t          d|||          \  }}|d	k    r ||          d	fS t          j        |j        j                  j        }t          j        |          rt          j        nt          j        }||d
z  }|d}t%          ||          }t          j        	                     ||                    }d}|t%          |||z            z  }d}t'          d|j                  }t          j        |dz   |g|j                  }t          j        ||dz   g|j                  }t          j        |dg|j                  }d	}t-          |          D ]}|d	k    rh|                                r| ||          z
  n|                                }t          j        	                    |          |k     r ||          d	fc S  ||          |d	ddf<   t          j        	                    |d	ddf                   }|d	ddfxx         d|z  z  cc<   t          j        |dz   |j                  } || d	<   d}!t-          |          D ]}" |||"ddf                   }# ||#          }$t          j        	                    |$          }%t-          |"dz             D ]1}& |||&ddf         |$          }|||"|&f<   |$|||&ddf         z  z  }$2t          j        	                    |$          }'|'||"|"dz   f<   |$dd         ||"dz   ddf<   |'||%z  k    rd	||"|"dz   f<   d}!n||"dz   ddfxx         d|'z  z  cc<   t-          |"          D ][}&||&d	f         ||&df         })}(||"|&|&dz   gf         \  }*}+|(|*z  |)|+z  z   |)                                 |*z  |(|+z  z   g||"|&|&dz   gf<   \ |||"|"f         ||"|"dz   f                   \  }(})},|(|)g||"ddf<   |,d	f||"|"|"dz   gf<   t          j        |)           | |"         z  }|(| |"         z  |g| |"|"dz   g<   t          j        |          }|dz  }|	dv r |||z             |	dk    r||k    r n||k    s|!r n||"|"f         d	k    rd	| |"<   t          j        |"dz   g|j                  }-| d|"dz            |-dd<   t-          |"d	d          D ]M}&|-|&         d	k    r?|-|&xx         ||&|&f         z  cc<   |-|&         }|-d|&xx         |||&d|&f         z  z  cc<   N|-d	         d	k    r|-d	xx         |d         z  cc<   ||-|d|"dz   ddf         z  z  }| ||          z
  }t          j        	                    |          }.|	dk    r||k    r ||          |.|k    rd	n|fc S |	dk    r ||           |.|k    r nI|!r nE||k    rt9          |d|z            }nt%          dd|z            }|t%          |||.z            z  }|.|k    rd	n|}/ ||          |/fS )ae  
    Use Generalized Minimal RESidual iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, 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).
    x0 : ndarray
        Starting guess for the solution (a vector of zeros by default).
    atol, rtol : float
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    restart : int, optional
        Number of iterations between restarts. Larger values increase
        iteration cost, but may be necessary for convergence.
        If omitted, ``min(20, n)`` is used.
    maxiter : int, optional
        Maximum number of iterations (restart cycles).  Iteration will stop
        after maxiter steps even if the specified tolerance has not been
        achieved. See `callback_type`.
    M : {sparse array, 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. However, the final
        convergence is tested with respect to the ``b - A @ x`` residual.
    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.

        This keyword has no effect if `callback` is not set.

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

    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_array
    >>> from scipy.sparse.linalg import gmres
    >>> A = csc_array([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
    >>> b = np.array([2, 4, -1], dtype=float)
    >>> x, exitCode = gmres(A, b, atol=1e-5)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    Na2  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., ``gmres(..., callback_type='pr_norm')``, or to retain the old behavior ``gmres(..., callback_type='legacy')``   )category
stacklevelr   )r9   pr_normr   zUnknown callback_type: r   r   r       g      ?r   lartg)r.   r   r!   FT)r   ri   )r   r   r9   g      ?g      ?)warningswarnDeprecationWarningr   r   r+   r'   r$   r%   r&   r   r-   r.   r/   r0   r(   r)   r*   minr   emptyzerosr3   r1   r2   r5   	conjugater4   r   )0r6   r7   r8   r   r   rc   r   r   r   rd   r   r9   r:   r+   r?   r=   r;   r<   r0   r>   Mb_nrm2ptol_max_factorptolpresidrk   rV   hgivens
inner_iterrG   rE   tmpS	breakdowncolavwh0kh1crX   n0n1magyr`   infos0                                                   r   r   r   F  s   r  5E 	c$61EEEE 666D=DDEEE)!QA66Aq!QXFXFAAINN1EWeT488GD!zz{1~~q  
(17<
 
 
$C++7bggGB$'1ooGinnVVAYY''G OS$,777DFWAG444E 	'!)Qqw///A
'719%QW555AXwl!'222F J7^^ f; f;	>>!"6FF1IIaffhhAy~~a  4''"{1~~q((((&))!QQQ$innQq!!!tW%%	!QQQ$AGHWQYag...!	>> -	 -	C#qqq&	""Br

A ""B3q5\\ ! !ga111gq))#q&	S1aaa4[ ""B Ac37lOaaaDAcAgqqqjM SV||"##sQw, 		#'111*!b&) 3ZZ H Had|VAqD\13AaC=)B&'dQrTkAFFHH9R<!B$3F%G#1q5z/"" aSk1S#a%Z=99IAq#VF36N#&6AcCQ<  <??"1S6)C!"1S63AsC!GnVC[[F!OJ 555%(((((Z7-B-B~~~  S#X;!AcFHc!eWAG,,,#a%y!!!sAr"" 	& 	&Atqyy!!Q$d"1"Qq"1"uX%Q4199aDDDAdGODDD	Q6CE61119q		M	q!! H$$w)>)>;q>>117BBBBCHQKKKD==E 	> Et^^!#to'=>>OO!#s_'<==OOTE\:::$11WD;q>>4r   )r   r   r   M1M2r   c                j  5 | 5t          | d||          \  } }	}
}}t          j                            |          }t	          d|||          \  }}|dk    r ||          dfS ||t          5d          rC5fd}5fd}5fd}5fd}t          | j        ||	          }t          | j        ||	          }n1d
 }t          | j        ||	          }t          | j        ||	          }t          |          }||dz  }t          j	        |
          rt          j
        nt          j        }t          j        |
j        j                  j        }|}|}|}|}|}|
                                r||                     |
          z
  n|                                }|                                }|                    |          }t          j                            |          }|                                }|                    |          } t          j                            |           }!d\  }"}#}$t          j        |          }%t          j        |          }&d\  }'}(})}*}+t+          |          D ]},t          j                            |          |k     r ||
          dfc S t          j        |          |k     r ||
          dfc S t          j        |!          |k     r ||
          dfc S |dd         |%dd<   |%d|z  z  }%|d|z  z  }|dd         |&dd<   |&d|!z  z  }&| d|!z  z  }  || |          }-t          j        |-          |k     r ||
          dfc S |                    |          }.|                    |           }/|,dk    rM|.|!|-z  |'z  |*z  z  }.|.dd         |*dd<   |/||-|'z                                  z  |(z  z  }/|/dd         |(dd<   n(|.                                }*|/                                }(|                     |*          }0 ||(|0          }'t          j        |'          |k     r ||
          dfc S |'|-z  }1t          j        |1          |k     r ||
          dfc S |0dd         |dd<   ||1|%z  z  }|                    |          }|}2t          j                            |          }|&dd         |dd<   ||1                                 z  }||                     |(          z  }|                    |          } t          j                            |           }!|"}3|$}4||3t          j        |1          z  z  }$dt          j        d|$dz  z             z  }"t          j        |"          |k     r ||
          dfc S |#|2|1z   |"|3z  dz  z  z  }#|,dk    r'|)|4|"z  dz  z  })|)|#|*z  z  })|+|4|"z  dz  z  }+|+|#|0z  z  }+n2|*                                })|)|#z  })|0                                }+|+|#z  }+|
|)z  }
||+z  }|r ||
            ||
          |fS )a-  Use Quasi-Minimal Residual iteration to solve ``Ax = b``.

    Parameters
    ----------
    A : {sparse array, 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).
    x0 : ndarray
        Starting guess for the solution.
    atol, rtol : float, optional
        Parameters for the convergence test. For convergence,
        ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
        The default is ``atol=0.`` and ``rtol=1e-5``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M1 : {sparse array, ndarray, LinearOperator}
        Left preconditioner for A.
    M2 : {sparse array, 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.

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

    See Also
    --------
    LinearOperator

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> from scipy.sparse.linalg import qmr
    >>> A = csc_array([[3., 2., 0.], [1., -1., 0.], [0., 5., 1.]])
    >>> b = np.array([2., 4., -1.])
    >>> x, exitCode = qmr(A, b, atol=1e-5)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True
    Nr   r   r?   c                 0                         | d          S Nleftr?   r7   A_s    r   left_psolvezqmr.<locals>.left_psolve  s    yyF+++r   c                 0                         | d          S Nrightr   r   s    r   right_psolvezqmr.<locals>.right_psolve  s    yyG,,,r   c                 0                         | d          S r   r@   r   s    r   left_rpsolvezqmr.<locals>.left_rpsolve  s    zz!V,,,r   c                 0                         | d          S r   r   r   s    r   right_rpsolvezqmr.<locals>.right_rpsolve  s    zz!W---r   )r+   r,   c                     | S N )r7   s    r   idzqmr.<locals>.id  s    r   r    )r   rl   r   rQ   r"   ir   iir#   r!   i)r   r$   r%   r&   r   hasattrr   shaper'   r(   r)   r*   r-   r.   r/   r0   r1   r+   r2   r,   rS   r3   r4   r5   sqrt)6r6   r7   r8   r   r   r   r   r   r   r   r9   r:   r;   r<   r   r   r   r   r   r=   r>   rA   betatolgammatoldeltatol
epsilontolxitolrE   vtilder   rW   wtilderH   xigammaetathetarV   r   epsilonrL   drC   rX   rG   deltaytilderI   rD   rK   rB   
gamma_prev
theta_prevr   s6                                                        @r   r   r   L  s   v 
B)!T2q99Aq!QINN1EUE466GD!zz{1~~q  	zbj2x   	@, , , , ,- - - - -- - - - -. . . . .'2(46 6 6B  '3(57 7 7BB  B???BB???BAAB$++7bggGXagl##'FGHHJE55770AHHQKKAVVXXF
		&A
)..

CVVXXF


6A			B E3
fA
fA 7GQ1a7^^ P' P'	9>>!t##;q>>1$$$$6#;;;q>>3&&&&6"::;q>>3&&&&aaay!!!	a#g	a#gaaay!!!	a"f	a"f16%==8##;q>>3&&&&1Aq==rEzG+q00F!!!9AaaaDsego33555::F!!!9AaaaDDAA!'!V$$6'??Z'';q>>3&&&&6$<<'!!;q>>3&&&&111Iqqq	$q&IIfinnQaaaDqqq	DIIKK-!))A,,JJvY^^A

zBF4LL01BGAqL)))6%==8##;q>>3&&&&D!UZ%7!$;;;q==*u$**AQJA*u$**AVOAAAHAAHA	Q	Q 	HQKKK {1~~w&&r   )r   r   r   )rm   numpyr$   scipy.sparse.linalg._interfacer   utilsr   scipy.linalgr   __all__r   r   r   r	   r
   r   r   r   r   r   <module>r      s        9 9 9 9 9 9       ) ) ) ) ) )
;
;
;   B'2ttd B' B' B' B' B'JQ'Dr44Q' Q' Q' Q' Q'hu'dTTD u' u' u' u' u'pZ't"ddT Z' Z' Z' Z' Z'zC BddtC  C  C  C  C LI't"dtI' I' I' I' I' I' I'r   