
    Uin*              	       b    d dl mZmZmZmZ d dlmZ d dlmZ ddl	m
Z
 dgZddd	dddd
d
ddZdS )    )innerzerosinffinfo)norm)sqrt   )make_systemminresNgh㈵>g        F)rtolshiftmaxiterMcallbackshowcheckc                ^   t          | |||          \  } }}
}| j        }|j        }d}d}| j        d         }|d|z  }g d}|rTt          |dz              t          |d|d	d
|dz              t          |d|d	d|dz              t                       d}d}d}d}d}d}|
j        }t          |          j        }||                                }n|| |
z  z
  } ||          }t          ||          }|dk     rt          d          |dk    r|
dfS t          |          }|dk    r|}
|
dfS t          |          }|	r ||          } ||          }t          ||          }t          ||          } t          || z
            }!||z   |dz  z  }"|!|"k    rt          d           ||          }t          ||          }t          ||          } t          || z
            }!||z   |dz  z  }"|!|"k    rt          d          d}#|}$d}%d}&|}'|}(|})d}*d}+d},t          |          j        }-d}.d}/t          ||          }t          ||          }0|}|r+t                       t                       t          d           ||k     r|dz  }d|$z  }||z  }1 ||1          }|||1z  z
  }|dk    r||$|#z  |z  z
  }t          |1|          }2||2|$z  |z  z
  }|}|} ||          }|$}#t          ||          }$|$dk     rt          d          t          |$          }$|+|2dz  |#dz  z   |$dz  z   z  }+|dk    r|$|z  d|z  k    rd}|&}3|.|%z  |/|2z  z   }4|/|%z  |.|2z  z
  }5|/|$z  }&|. |$z  }%t          |5|%g          }6|(|6z  }7t          |5|$g          }8t          |8|          }8|5|8z  }.|$|8z  }/|.|(z  }9|/|(z  }(d|8z  }:|0};|}0|1|3|;z  z
  |4|0z  z
  |:z  }|
|9|z  z   }
t          |,|8          },t          |-|8          }-|)|8z  }!|*|4|!z  z
  })|& |!z  }*t          |+          }t          |
          }||z  }"||z  |z  }<||z  |z  }=|5}>|>dk    r|"}>|(}'|'}|dk    s|dk    rt           }?n|||z  z  }?|dk    rt           }@n|6|z  }@|,|-z  }|dk    rEd|?z   }Ad|@z   }B|Bdk    rd}|Adk    rd}||k    rd}|d|z  k    rd}|<|k    rd}|@|k    rd}|?|k    rd}d}C|dk    rd }C|dk    rd }C||dz
  k    rd }C|dz  dk    rd }C|'d|<z  k    rd }C|'d|=z  k    rd }C|d!|z  k    rd }C|dk    rd }C|rX|CrV|d"d#|
d         d$d#|?d%}Dd#|@d%}Ed#|d&d#|d&d#|5|z  d&}Ft          |D|Ez   |Fz              |dz  dk    rt                       | ||
           |dk    rn||k     |rt                       t          |d'|d	d(|d)z              t          |d*|d+d,|d+z              t          |d-|d+d.|d+z              t          |d/|7d+z              t          |||dz            z              |dk    r|}Gnd}G|
|GfS )0a  
    Solve ``Ax = b`` with the MINimum RESidual method, for a symmetric `A`.

    MINRES minimizes norm(Ax - b) for a real symmetric matrix A.  Unlike
    the Conjugate Gradient method, A can be indefinite or singular.

    If shift != 0 then the method solves (A - shift*I)x = b

    Parameters
    ----------
    A : {sparse array, ndarray, LinearOperator}
        The real symmetric 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 : 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.
    shift : float
        Value to apply to the system ``(A - shift * I)x = b``. Default is 0.
    rtol : float
        Tolerance to achieve. The algorithm terminates when the relative
        residual is below ``rtol``.
    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.  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.
    show : bool
        If ``True``, print out a summary and metrics related to the solution
        during iterations. Default is ``False``.
    check : bool
        If ``True``, run additional input validation to check that `A` and
        `M` (if specified) are symmetric. Default is ``False``.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> from scipy.sparse.linalg import minres
    >>> A = csc_array([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
    >>> A = A + A.T
    >>> b = np.array([2, 4, -1], dtype=float)
    >>> x, exitCode = minres(A, b)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True

    References
    ----------
    Solution of sparse indefinite systems of linear equations,
        C. C. Paige and M. A. Saunders (1975),
        SIAM J. Numer. Anal. 12(4), pp. 617-629.
        https://web.stanford.edu/group/SOL/software/minres/

    This file is a translation of the following MATLAB implementation:
        https://web.stanford.edu/group/SOL/software/minres/minres-matlab.zip

    zEnter minres.   zExit  minres.   r   N   )z3 beta2 = 0.  If M = I, b and x are eigenvectors    z/ beta1 = 0.  The exact solution is x0          z3 A solution to Ax = b was found, given rtol        z3 A least-squares solution was found, given rtol    z3 Reasonable accuracy achieved, given eps           z3 x has converged to an eigenvector                 z3 acond has exceeded 0.1/eps                        z3 The iteration limit was reached                   z3 A  does not define a symmetric matrix             z3 M  does not define a symmetric matrix             z3 M  does not define a pos-def preconditioner       zSolution of symmetric Ax = bz
n      =  3gz     shift  =  z23.14ez
itnlim =  z     rtol   =  z11.2ezindefinite preconditionergUUUUUU?znon-symmetric matrixznon-symmetric preconditioner)dtypezD   Itn     x(1)     Compatible    LS       norm(A)  cond(A) gbar/|A|r	   g      ?   
      g?      F(   Tg{Gz?6g z12.5ez10.3ez8.1ez istop   =  z               itn   =5gz Anorm   =  z12.4ez      Acond =  z rnorm   =  z      ynorm =  z Arnorm  =  )r
   matvecshapeprintr   r   epscopyr   
ValueErrorr   r   absmaxr   minr   )HAbx0r   r   r   r   r   r   r   xr!   psolvefirstlastnmsgistopitnAnormAcondrnormynormxtyper$   r1ybeta1bnormwr2stzepsaoldbbetadbarepslnqrnormphibarrhs1rhs2tnorm2gmaxgmincssnw2valfaoldepsdeltagbarrootArnormgammaphidenomw1epsxepsrdiagtest1test2t1t2prntstr1str2str3infosH                                                                           z/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_isolve/minres.pyr   r   
   s   d Q2q))JAq!QXFXFED	
Aa%
C 
C 
CC  e44555eF1FFFFFFFGGGeJ7JJJdJJJJKKKE
CEEEEGE
,,
C 
zVVXX1Wr

A"aLLEqyy4555	!1vGGEzz1vKKE = F1IIVAYY!AJJ!BKKAJJC3>)t883444 VAYY!AJJ"RLLAJJC3>)t88;<<< DDDEFFDDFD<<D	B	
BauA	q			B	B VTUUU
--qHaCF1II	M!88T$YN"AQqzzdBF2JJR{{!883444Dzz$'D!G#dAg--!88EzRV## T	BI%Dy29$T	td{T4L!!$ dD\""E3E\E\6kf E	]U2X%.AI 445LeAg~wqy VQs{u}s"u}t#199DA::!EEU5[)EA::EE5LE T	
 A::UBUBQwwQwwg~~Cu}} }}}} 77D"99D'"*D8q==DRWDRWDDHDA::D 	D 	999qt999E999D$u$$$DBuBBBEBBBeBBBD$+$%%%Rx1}}HQKKKA::u --x  #dLELLLCLLLLMMMdMEMMMMMMMNNNdMEMMMMMMMNNNd2F2222333dSq\!"""zzd8O    )N)numpyr   r   r   r   numpy.linalgr   mathr   utilsr
   __all__r    rj   ri   <module>rq      s    * * * * * * * * * * * *                  *j$c4DuEj j j j j j jrj   