
    `i                     L   d dl 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l
mZ d dlmZ d dlZ	 d dlZd dlZd	Zn# e$ r d
ZY nw xY wd Z	 	 ddZd Z	 	 ddZd Z G d d          Z G d de          Zd Zddddi fdZdddddddi fdZd Z	 	 d dZd Z dS )!    N)cublas)device)runtime)_util)sparse)
_interface)_make_systemTFc                    ddl m} t          j        rt	          d          t          j        |           st          j        |           } t          j	        |            t          j
        |           | j        d         }|j        dk    st          |          |k    rt          d          | j        dk    s| j        dk    r| j        }nt!          j        | j        d          }t%          j                    }| j        }d}d}t+          j        ||	          }	t!          j        dt           j                  }
|dk    r|j        }n|j        } ||||| j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        |||	j        j        |
j         j                   |	!                    t           j"                  }	|	d
d
d
d
d
d
d
d
d
f
}|S )a  Solves linear system with QR decomposition.

    Find the solution to a large, sparse, linear system of equations.
    The function solves ``Ax = b``. Given two-dimensional matrix ``A`` is
    decomposed into ``Q * R``.

    Args:
        A (cupy.ndarray or cupyx.scipy.sparse.csr_matrix): The input matrix
            with dimension ``(N, N)``
        b (cupy.ndarray): Right-hand side vector.

    Returns:
        tuple:
            Its length must be ten. It has same type elements
            as SciPy. Only the first element, the solution vector ``x``, is
            available and other elements are expressed as ``None`` because
            the implementation of cuSOLVER is different from the one of SciPy.
            You can easily calculate the fourth element by ``norm(b - Ax)``
            and the ninth element by ``norm(x)``.

    .. seealso:: :func:`scipy.sparse.linalg.lsqr`
    r   )cusolverzHIP does not support lsqr   z+b must be 1-d array whose size is same as Afd      ?dtypeN)#cupy_backends.cuda.libsr   r   is_hipRuntimeErrorr   isspmatrix_csr
csr_matrixr   _assert_stacked_square_assert_cupy_arrayshapendimlen
ValueErrorr   numpypromote_typesr   get_cusolver_sp_handlennzcupyemptyint32	scsrlsvqr	dcsrlsvqr_descr
descriptordataptrindptrindicesctypesastypefloat64)Abr   mr   handler    tolreorderxsingularitycsrlsvqrrets                t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/sparse/linalg/_solve.pylsqrr:      s   . 100000~ 86777 ## !a  	 ###	Q	
Av{{c!ffkkFGGG 	w#~~C#AGS11*,,F
%C
CG
1E"""A+a--K||%%H3+QV[_	19>-qvz3	
K&+- - - 	
AdD$dD$d
CCJ            ư>    חAc                    t          j        |           } |                                }| j        }| j        }	| j        \  }
}t          |
|g          }||dz  }|t          j        | |t                    }nt          j        | ||t                    }|
                                }t          j        |          }|
                                }|                                                                }|t          j        |f|          }nv|j        |fk    s|j        |dfk    st!          d          |                    | j                                                  }| ||          z  }t          j        |          }|                                                                }t          j        ||          }t          j        d|j                  }d}|dk    rJ||z  } |	|          }t          j        |          }|                                                                }|dk    r||z  }d}||z  }|}d}d}d}d}|
                                }t          j        ||          }|}d} d}!d}"d}#d}$d}%||z  }&d}'d}(|})d}*d}+d},d}-|dk    rd|z  }-|}.||z  }/|/dk    r
||,||.|/|)|*|+fS ||k     r|dz   }|| z  }| ||          z  }t          j        |          }|                                                                }|dk    r^||z  }|| z  }| |	|          z  }t          j        |          }|                                                                }|dk    r||z  }t)          ||          \  }0}1}2|}3t)          |2|          \  }4}5}|5|z  }6|4|z  }|}7|$}8||z  }9||z  }:t)          ||z  |6          \  }}}||z  }$| |z  }||9|z  |3|7z  z   z  }||z  }||$||z  z  |z  z  }||6|z   z  }||z  }|0|z  };|1 |z  }<|4|;z  }=|5 |;z  }|#}>t)          |!|9          \  }?}@}A|@|z  }#|?|z  }!|@ | z  |?|=z  z   } |8|>|"z  z
  |Az  }"|$|#|"z  z
  |!z  }B|%|<|<z  z   }%t+          j        |%| |Bz
  d	z  z   ||z  z             }.|&||z  z   }&t+          j        |&          })|&||z  z   }&t/          |'|7          }'|dk    rt          |(|7          }(t/          |'|:          t          |(|:          z  }*t1          |          }/t          j        |          }+|+                                                                }+|.|z  }C|)|.z  dk    r	|/|)|.z  z  }Dnt*          j        }Dd|*z  }E|Cd|)|+z  |z  z   z  }F|||)z  |+z  |z  z   }G||k    rd
},d|Ez   dk    rd},d|Dz   dk    rd},d|Fz   dk    rd},|E|-k    rd},|D|k    rd	},|C|Gk    rd},|,dk    rn||k     |                    t*          j                  }||,||.|/|)|*|+fS )a  Iterative solver for least-squares problems.

    lsmr solves the system of linear equations ``Ax = b``. If the system
    is inconsistent, it solves the least-squares problem ``min ||b - Ax||_2``.
    A is a rectangular matrix of dimension m-by-n, where all cases are
    allowed: m = n, m > n, or m < n. B is a vector of length m.
    The matrix A may be dense or sparse (usually sparse).

    Args:
        A (ndarray, spmatrix or LinearOperator): The real or complex
            matrix of the linear system. ``A`` must be
            :class:`cupy.ndarray`, :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        b (cupy.ndarray): Right hand side of the linear system with shape
            ``(m,)`` or ``(m, 1)``.
        x0 (cupy.ndarray): Starting guess for the solution. If None zeros are
            used.
        damp (float): Damping factor for regularized least-squares.
            `lsmr` solves the regularized least-squares problem
            ::

                min ||(b) - (  A   )x||
                    ||(0)   (damp*I) ||_2

            where damp is a scalar. If damp is None or 0, the system
            is solved without regularization.
        atol, btol (float):
            Stopping tolerances. `lsmr` continues iterations until a
            certain backward error estimate is smaller than some quantity
            depending on atol and btol.
        conlim (float): `lsmr` terminates if an estimate of ``cond(A)`` i.e.
            condition number of matrix exceeds `conlim`. If `conlim` is None,
            the default value is 1e+8.
        maxiter (int): Maximum number of iterations.

    Returns:
        tuple:
            - `x` (ndarray): Least-square solution returned.
            - `istop` (int): istop gives the reason for stopping::

                    0 means x=0 is a solution.

                    1 means x is an approximate solution to A*x = B,
                    according to atol and btol.

                    2 means x approximately solves the least-squares problem
                    according to atol.

                    3 means COND(A) seems to be greater than CONLIM.

                    4 is the same as 1 with atol = btol = eps (machine
                    precision)

                    5 is the same as 2 with atol = eps.

                    6 is the same as 3 with CONLIM = 1/eps.

                    7 means ITN reached maxiter before the other stopping
                    conditions were satisfied.

            - `itn` (int): Number of iterations used.
            - `normr` (float): ``norm(b-Ax)``
            - `normar` (float): ``norm(A^T (b - Ax))``
            - `norma` (float): ``norm(A)``
            - `conda` (float): Condition number of A.
            - `normx` (float): ``norm(x)``

    .. seealso:: :func:`scipy.sparse.linalg.lsmr`

    References:
        D. C.-L. Fong and M. A. Saunders, "LSMR: An iterative algorithm for
        sparse least-squares problems", SIAM J. Sci. Comput.,
        vol. 33, pp. 2950-2971, 2011.
    N   r   r   zx0 has incompatible dimensions r   g}Ô%IT               )r   aslinearoperatorsqueezematvecrmatvecr   minr!   result_typefloatcopyr   nrm2getitemzerosr   r-   r   ravel	_symOrthor   sqrtmaxabsinfr.   )Hr/   r0   x0dampatolbtolconlimmaxiterrI   rJ   r1   nminDimr   unormbbetar5   beta_cpuvalpha	alpha_cpuitnzetabaralphabarrhorhobarcbarsbarhhbarbetaddbetadrhodoldtautildeold
thetatildezetar   normA2maxrbarminrbarnormAcondAnormxistopctolnormrnormarchatshatalphahatrhooldcsthetanew	rhobaroldzetaoldthetabarrhotemp	betaacute	betacheckbetahatthetatildeold	ctildeold	stildeoldrhotildeoldtaudtest1test2test3t1rtolsH                                                                           r9   lsmrr   U   s   X 	#A&&A			AXFiG7DAq!Q[[F1*	z Au-- Ar511	AKNNE::<<DIIKKE	zJt5)))QD  BHA$6$6=>>>IIag$$&&	VVAYY{1~~xxzz  H
1eAJr,,,EI!||	T	GAJJAIIKK$$&&	1}}	U
 C("GH
CFDD	A:aD
 FEGKJD	A "FGGEEE EDzz6zE
 !F{{%eVUE5@@ --Ag 	
eV	VVAYY{1~~88::??$$a<<IA$JAOAKNNE		((**I1}}U
  )488dH h11	1cy=y= 	#:*&tcz8<<dFg~&7"
 	(S.FY$6788		dcFl#t++	x#~	Q
 6M	EFN	 i-i
 #,5gx,H,H)	9k'
f$e#i'&99
 !<<KzK//7:	I%%
1q006F?BCC (X--
6"")i// gy))77'9--GGW%%GW(=(==
 WA		  ""
 EMaeem,EEIEE	a%+e++,d5j&u,, '>>Eu9>>Eu9>>Er6Q;;E D==ED==ED==E199S --X 	
AeS%u<<r;   c                 Z    ddl m} t          j        s|                                dk    S dS )Nr   cusparsei.  F)r   r   r   r   get_build_version)r0   	_cusparses     r9   _should_use_spsmr     s<    ======> **,,55 ur;   c                 4   ddl m} |                    d          s|                    d          st          t	          j        |           st          d          t          |t          j	                  st          d          | j
        d         | j
        d         k    rt          d| j
         d	          |j        d
vrt          d|j
         d	          | j
        d         |j
        d         k    r t          d| j
         d|j
         d	          | j        j        dvrt          d| j         d	          |                    d          rt          |          rt	          j        |           s[t	          j        |           sGt	          j        |           s3t'          j        dt          j                   |                                 } |                                  |                    | |||          }n|                    d          rt	          j        |           sGt	          j        |           s3t'          j        dt          j                   |                                 } |                                  |r!| j        |j        k    r|j        s|j        r|}n|                    | j        d          }|                    | |||           nJ |j        j        dv r/t;          j        |j        d          }|                    |          }|S )a   Solves a sparse triangular system ``A x = b``.

    Args:
        A (cupyx.scipy.sparse.spmatrix):
            Sparse matrix with dimension ``(M, M)``.
        b (cupy.ndarray):
            Dense vector or matrix with dimension ``(M)`` or ``(M, K)``.
        lower (bool):
            Whether ``A`` is a lower or upper triangular matrix.
            If True, it is lower triangular, otherwise, upper triangular.
        overwrite_A (bool):
            (not supported)
        overwrite_b (bool):
            Allows overwriting data in ``b``.
        unit_diagonal (bool):
            If True, diagonal elements of ``A`` are assumed to be 1 and will
            not be referenced.

    Returns:
        cupy.ndarray:
            Solution to the system ``A x = b``. The shape is the same as ``b``.
    r   r   spsmcsrsm2%A must be cupyx.scipy.sparse.spmatrixb must be cupy.ndarrayr   z$A must be a square matrix (A.shape: )r   rB   z#b must be 1D or 2D array (b.shape: z\The size of dimensions of A must be equal to the size of the first dimension of b (A.shape: z, b.shape: fdFDzunsupported dtype (actual: z=CSR, CSC or COO format is required. Converting to CSR format.)lower	unit_diagz8CSR or CSC format is required. Converting to CSR format.TrN   FfFr.   )cupyxr   check_availabilityNotImplementedErrorr   
isspmatrix	TypeError
isinstancer!   ndarrayr   r   r   r   charr   r   isspmatrix_cscisspmatrix_coowarningswarnSparseEfficiencyWarningtocsrsum_duplicatesr   _c_contiguous_f_contiguousr-   r   r   r   )	r/   r0   r   overwrite_Aoverwrite_bunit_diagonalr   r5   r   s	            r9   spsolve_triangularr     sK   0 ''// "''11"!!Q A?@@@a&& 20111wqzQWQZJJJJKKKvVIqwIIIJJJwqzQWQZ E&'gE E:;'E E E F F 	F 	w|6!!@ag@@@AAA""6** /?/B/B %a(( 	%a((	%a((	 M ()/)GI I I		A	MM!Qe}MEE		$	$X	.	. %a(( 	F,A!,D,D 	M $%+%CE E E		A	 	-AGqw.. /$%O /AAt,,A1E]CCCCuw|t#AGY77HHUOOHr;   c                    ddl }|j                            d          st          t	          j        |           st          d          t          |t          j	                  st          d          | j
        d         | j
        d         k    r't          d                    | j
                            |j        dk    s2|j        dk    s't          d	                    |j
                            | j
        d         |j
        d         k    r-t          d
                    | j
        |j
                            t	          j        |           s3t          j        dt          j                   |                                 } |                                  |                    | j        d          }|j        dk    rut          j        |          }t/          |j
        d                   D ].}|j                            | |dd|f                   |dd|f<   /t          j        |d          }|S |j                            | |          S )aN  Solves a sparse linear system ``A x = b``

    Args:
        A (cupyx.scipy.sparse.spmatrix):
            Sparse matrix with dimension ``(M, M)``.
        b (cupy.ndarray):
            Dense vector or matrix with dimension ``(M)`` or ``(M, N)``.

    Returns:
        cupy.ndarray:
            Solution to the system ``A x = b``.
    r   Nr7   r   r   r   'A must be a square matrix (A.shape: {})rB   zInvalid b.shape (b.shape: {})z4matrix dimension mismatch (A.shape: {}, b.shape: {})z1CSR format is required. Converting to CSR format.Fr   Forder)cupyx.cusolverr   r   r   r   r   r   r   r!   r   r   r   formatr   r   r   r   r   r   r   r-   r   
empty_likeranger7   asarray)r/   r0   r   resjs        r9   spsolver     s!    >,,Z88 "!!Q A?@@@a&& 20111wqzQWQZB* * 	*FaKK16Q;;8??HHIIIwqzQWQZO &!'224 4 	4  ## I4	6 	6 	6GGII	u%%Avzzoa  sy|$$ 	< 	<A//1QQQT7;;C1IIl3c***
~&&q!,,,r;   c                       e Zd Zd ZddZdS )SuperLUc                 x   t           st          d          t          |t          j        j        j                  st          d          |j        | _        |j	        | _	        t          j        |j                  | _        t          j        |j                  | _        t	          j        |j                                                  | _        t	          j        |j                                                  | _        t          j        | j                  | _        t          j        | j                  | _        dS )zLU factorization of a sparse matrix.

        Args:
            obj (scipy.sparse.linalg.SuperLU): LU factorization of a sparse
                matrix, computed by `scipy.sparse.linalg.splu`, etc.
        scipy is not availablez'obj must be scipy.sparse.linalg.SuperLUN)scipy_availabler   r   scipyr   linalgr   r   r   r    r!   arrayperm_rperm_cr   Lr   Uargsort_perm_r_rev_perm_c_rev)selfobjs     r9   __init__zSuperLU.__init__  s      	97888#u|2:;; 	GEFFFY
7j,,j,,"35;;==11"35;;==11<44<44r;   Nc                    ddl m t          |t          j                  st          d          |j        dvr't          d                    |j                            |j	        d         | j	        d         k    r-t          d                    | j	        |j	                            |dvrt          d          
                    d	          rt          |          rfd
}|}n$
                    d          rfd}|}nt          |                    | j        j                  }|dk    r| j        ;|j        dk    r#|j        r|j        dd| j        f         j        }n|| j                 } || j        |d|          } || j        |d|          }| j        || j                 }n~| j        ;|j        dk    r#|j        r|j        dd| j        f         j        }n|| j                 } || j        |d|          } || j        |d|          }| j        || j                 }|j        s|                    d          }|S )a  Solves linear system of equations with one or several right-hand sides.

        Args:
            rhs (cupy.ndarray): Right-hand side(s) of equation with dimension
                ``(M)`` or ``(M, K)``.
            trans (str): 'N', 'T' or 'H'.
                'N': Solves ``A * x = rhs``.
                'T': Solves ``A.T * x = rhs``.
                'H': Solves ``A.conj().T * x = rhs``.

        Returns:
            cupy.ndarray:
                Solution vector(s)
        r   r   zojb must be cupy.ndarrayr   z$rhs.ndim must be 1 or 2 (actual: {})z.shape mismatch (self.shape: {}, rhs.shape: {}))r   THztrans must be 'N', 'T', or 'H'r   c                 6                         | |||          S Nr   transa)r   r/   Br   r   r   s       r9   r   zSuperLU.solve.<locals>.spsmJ  s    }}Qv}FFFr;   r   c                 :                         | |||           |S r   )r   r   s       r9   r   zSuperLU.solve.<locals>.csrsm2N  s"    1E&AAAr;   r   NrB   Tr   Fr   r   )r   r   r   r!   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r   r   r   r   r   rN   )r   rhstransr   smr   r5   r   s          @r9   solvezSuperLU.solve-  s    	#"""""#t|,, 	8677786!!C#VCH--/ / /9Q<4:a=((M$fTZ;;= = =''CDDD&&v.. 
	&3CC3H3H 
	&G G G G GBB((22 	&     BB%%JJtv|$$C<<{&6Q;;1?;AAAt//02AA$*+A461D777A461E%888A{&dkN{&6Q;;1?;AAAt//02AA$*+A461E%888A461D777A{&dkN 	"S!!Ar;   N)r   )__name__
__module____qualname__r   r   rA   r;   r9   r   r     s=        5 5 5,A A A A A Ar;   r   c                       e Zd Zd ZdS )
CusparseLUc                 >   t           st          d          t          j        |          st	          d          |j        | _        |j        | _        d| _        d| _        |	                                }t          j                            |          }|                    d           t          j                            |          }t          j        |                                          | _        t          j        |                                          | _        dS )zIncomplete LU factorization of a sparse matrix.

        Args:
            a (cupyx.scipy.sparse.csr_matrix): Incomplete LU factorization of a
                sparse matrix, computed by `cusparse.csrilu02`.
        r   z'a must be cupyx.scipy.sparse.csr_matrixNr   )r   r   r   r   r   r   r    r   r   rP   r   trilsetdiagtriur   r   r   r   )r   aalaus       r9   r   zCusparseLU.__init__s  s      	97888$Q'' 	GEFFFW
5EEGG\q!!


3\q!!"288::.."288::..r;   N)r   r   r   r   rA   r;   r9   r   r   q  s#        / / / / /r;   r   c                 *    t          |           j        S )a  Return a function for solving a sparse linear system, with A pre-factorized.

    Args:
        A (cupyx.scipy.sparse.spmatrix): Sparse matrix to factorize.

    Returns:
        callable: a function to solve the linear system of equations given in
        ``A``.

    Note:
        This function computes LU decomposition of a sparse matrix on the CPU
        using `scipy.sparse.linalg.splu`. Therefore, LU decomposition is not
        accelerated on the GPU. On the other hand, the computation of solving
        linear equations using the method returned by this function is
        performed on the GPU.

    .. seealso:: :func:`scipy.sparse.linalg.factorized`
    )splur   )r/   s    r9   
factorizedr     s    & 77=r;   c                 "   t           st          d          t          j        |           st	          d          | j        d         | j        d         k    r't          d                    | j                            | j        j	        dvr't	          d                    | j                            | 
                                                                }t          j        j                            ||||||          }t          |          S )	aO  Computes the LU decomposition of a sparse square matrix.

    Args:
        A (cupyx.scipy.sparse.spmatrix): Sparse matrix to factorize.
        permc_spec (str): (For further augments, see
            :func:`scipy.sparse.linalg.splu`)
        diag_pivot_thresh (float):
        relax (int):
        panel_size (int):
        options (dict):

    Returns:
        cupyx.scipy.sparse.linalg.SuperLU:
            Object which has a ``solve`` method.

    Note:
        This function LU-decomposes a sparse matrix on the CPU using
        `scipy.sparse.linalg.splu`. Therefore, LU decomposition is not
        accelerated on the GPU. On the other hand, the computation of solving
        linear equations using the ``solve`` method, which this function
        returns, is performed on the GPU.

    .. seealso:: :func:`scipy.sparse.linalg.splu`
    r   r   r   r   r   r   Invalid dtype (actual: {}))
permc_specdiag_pivot_threshrelax
panel_sizeoptions)r   r   r   r   r   r   r   r   r   r   rP   tocscr   r   r   r   )r/   r   r  r  r  r  r   a_invs           r9   r   r     s    4  53444Q A?@@@wqzQWQZB &//+ + 	+w|6!!4;;AGDDEEE	AL$$	j4E
G % = =E 5>>r;   c	                    ddl m}	 t          st          d          t	          j        |           st          d          | j        d         | j        d         k    r't          d	                    | j                            | j
        j        dvr't          d	                    | j
                            |dk    rat	          j        |           s|                                 }
n|                                 }
|	                    |
           t!          |
          S |                                                                 }
t&          j        j                            |
||||||||		  	        }t-          |          S )
a  Computes the incomplete LU decomposition of a sparse square matrix.

    Args:
        A (cupyx.scipy.sparse.spmatrix): Sparse matrix to factorize.
        drop_tol (float): (For further augments, see
            :func:`scipy.sparse.linalg.spilu`)
        fill_factor (float):
        drop_rule (str):
        permc_spec (str):
        diag_pivot_thresh (float):
        relax (int):
        panel_size (int):
        options (dict):

    Returns:
        cupyx.scipy.sparse.linalg.SuperLU:
            Object which has a ``solve`` method.

    Note:
        This function computes incomplete LU decomposition of a sparse matrix
        on the CPU using `scipy.sparse.linalg.spilu` (unless you set
        ``fill_factor`` to ``1``). Therefore, incomplete LU decomposition is
        not accelerated on the GPU. On the other hand, the computation of
        solving linear equations using the ``solve`` method, which this
        function returns, is performed on the GPU.

        If you set ``fill_factor`` to ``1``, this function computes incomplete
        LU decomposition on the GPU, but without fill-in or pivoting.

    .. seealso:: :func:`scipy.sparse.linalg.spilu`
    r   r   r   r   r   r   r   r   )fill_factordrop_tol	drop_ruler   r  r  r  r  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   rN   csrilu02r   rP   r  r   r   spilur   )r/   r	  r  r
  r   r  r  r  r  r   r   r  s               r9   r  r    sj   D  53444Q A?@@@wqzQWQZB &//+ + 	+w|6!!4;;AGDDEEEa$Q'' 			AAA!!}}	AL%%	{X1B
G & = =E 5>>r;   c                    |dk    r$t          j        |           dt          |           fS | dk    r$dt          j        |          t          |          fS t          |          t          |           k    r?| |z  }t          j        |          t          j        d||z  z             z  }||z  }||z  }n>|| z  }t          j        |           t          j        d||z  z             z  }||z  }| |z  }|||fS )a  
    A stable implementation of Givens rotation according to
    S.-C. Choi, "Iterative Methods for Singular Linear Equations
      and Least-Squares Problems", Dissertation,
      http://www.stanford.edu/group/SOL/dissertations/sou-cheng-choi-thesis.pdf
    r   r   )r   signrW   rU   )r   r0   taur   r   rs         r9   rT   rT     s     	Avvz!}}aQ''	
a%*Q--Q''	Q#a&&!eJqMMEJqSy111GE!eJqMMEJqSy111GEa7Nr;   h㈵>c	                 h	   t          | |||          \  } }}	}| j        }
|j        }|j        d         }||dz  }d}d}d}d}d}d}|	j        }t	          j        |          j        } |
|	          }||z
  } ||          }t	          j        ||          }|dk     rt          d          |dk    r|	dfS t	          j	        |          }|
                                                                }|rBt          | ||	|          st          d          t          ||||          st          d          d}|}d}d}|}|}|}d} d}!d}"t	          j        |          j        }#d}$d}%t	          j        ||          }&t	          j        ||          }'|}(||k     r|d	z  }d
|z  })|)|z  }* |
|*          }|||*z  z  }|dk    r|||z  |z  z  }t	          j        |*|          }+|+
                                                                }+||+|z  |(z  z  }|(}|}( ||(          }|}t	          j        |(|          }|
                                                                }t          j	        |          }|dk     rt          d          |!|+dz  |dz  z   |dz  z   z  }!|d	k    r||z  d|z  k    rd}|},|$|z  |%|+z  z   }-|%|z  |$|+z  z
  }.|%|z  }|$ |z  }t          j                            |.|g          }/t          j                            |.|g          }0t          |0|          }0|.|0z  }$||0z  }%|$|z  }1|%|z  }d
|0z  }2|'}3|&}'|*|,|3z  z
  |-|'z  z
  |2z  }&|	|1|&z  z  }	t          |"|0          }"t%          |#|0          }#||0z  }4| |-|4z  z
  }| |4z  } t          j	        |!          }t          j                            |	          }|
                                                                }||z  }5||z  |z  }6|.}7|7dk    r|5}7|}|}|dk    s|dk    rt          j        }8n|||z  z  }8|dk    rt          j        }9n|/|z  }9|"|#z  }|dk    rEd	|8z   }:d	|9z   };|;d	k    rd}|:d	k    rd	}||k    rd}|d|z  k    rd}|6|k    rd}|9|k    rd}|8|k    rd	}| ||	           |dk    rn||k     |dk    r|}<nd}<|	|<fS )a-  Uses MINimum RESidual iteration to solve  ``Ax = b``.

    Args:
        A (ndarray, spmatrix or LinearOperator): The real or complex matrix of
            the linear system with shape ``(n, n)``.
        b (cupy.ndarray): Right hand side of the linear system with shape
            ``(n,)`` or ``(n, 1)``.
        x0 (cupy.ndarray): Starting guess for the solution.
        shift (int or float): If shift != 0 then the method solves
            ``(A - shift*I)x = b``
        tol (float): Tolerance for convergence.
        maxiter (int): Maximum number of iterations.
        M (ndarray, spmatrix or LinearOperator): Preconditioner for ``A``.
            The preconditioner should approximate the inverse of ``A``.
            ``M`` must be :class:`cupy.ndarray`,
            :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        callback (function): User-specified function to call after each
            iteration. It is called as ``callback(xk)``, where ``xk`` is the
            current solution vector.

    Returns:
        tuple:
            It returns ``x`` (cupy.ndarray) and ``info`` (int) where ``x`` is
            the converged solution and ``info`` provides convergence
            information.

    .. seealso:: :func:`scipy.sparse.linalg.minres`
    r   Nr@   zindefinite preconditionerznon-symmetric matrixznon-symmetric preconditionerr   r   r   rB   
   rD   g?rE   rF   )r	   rI   r   r   r!   finfoepsinnerr   rU   rP   rQ   _check_symmetricrV   rR   r   r   normrK   rX   )=r/   r0   rY   shiftr3   r^   Mcallbackcheckr5   rI   psolver_   r}   rh   AnormAcondrnormynormxtyper  Axr1ybeta1oldbrc   dbarepslnqrnormphibarrhs1rhs2tnorm2gmaxgmincssnww2r2r   re   rf   oldepsdeltagbarrootgammaphidenomw1zepsaepsxdiagr   r   r   t2infos=                                                                r9   minresrE  $  s   > aB**JAq!QXFXF	
Aa%E
CEEEEGE
*U


C	B	
RBr

AJr1Eqyy4555	!!tIeEIIKKE =2q#.. 	53444  1b#.. 	=;<<<DDDEFFDDFD:e D	B	
B
1E"""A	AU	#	#	#B	B
--q$JEF1II	UQY!88$+##A
1a  		  ""	edlb  F2JJz"a  xxzz  z$!883444%1*tqy(41944!88e|rCx'' T	BJ&Dy2:%T	td{|  $.. !!4,//E3E\E\6kf e"_urz)U2	S1W 445Leaiw{ 
6""  ##		  ""s{u}s"199DA::!IEEUU]+EA::IEE5LE t
 A::UBUBQwwQwwg~~c	!!u}} ||||HQKKKA:: --B zzd7Nr;   c                     | |z  }t          j        ||          }t          j        ||          }t          ||z
            }||z   |dz  z  }||k    rdS dS )NgUUUUUU?FT)r!   r  rW   )	op1op2vecr  r6  r   tr?  r@  s	            r9   r  r  	  sb    	sB
3A
3AAE

AGsy))D4xxu4r;   )Nr<   r=   r=   r>   N)TFFF)Nr<   r  NNNF)!r   r!   r   	cupy.cudar   r   cupy.linalgr   cupyx.scipyr   cupyx.scipy.sparse.linalgr   $cupyx.scipy.sparse.linalg._iterativer	   r   scipy.sparser   scipy.sparse.linalgr   ImportErrorr:   r   r   r   r   r   r   r   r   r  rT   rE  r  rA   r;   r9   <module>rS     s[                                   0 0 0 0 0 0 = = = = = = OO   OOO= = =@ @Cu= u= u= u=p	   IN%*K K K K\,- ,- ,-^Y Y Y Y Y Y Y Yx/ / / / / / / /6  , t4"( ( ( (V T2< < < <~  0 8<(-b b b bJ    s   
A AA