
    (ph|                         S r S/rSSKrSSKJr  SSKJr  SSKJ	r	J
r
  SSKJrJr  SS	KJr   " S
 S\R                   R"                  5      rSSKJr  SS jrSS jrg)zz
Matrix square root for general matrices and for upper triangular matrices.

This module exists to avoid cyclic imports.

sqrtm    N)_asarray_validated   )norm)ztrsyldtrsyl)schurrsf2csf)_ensure_dtype_cdszc                       \ rS rSrSrg)
SqrtmError    N)__name__
__module____qualname____firstlineno____static_attributes__r       O/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_matfuncs_sqrtm.pyr   r      s    r   r   )within_block_loopc           	         [         R                  " U 5      n[         R                  " U 5      =(       a    [         R                  " USS9S:  nU(       dH  [         R                  " U [         R
                  SS9n [         R                  " U[         R
                  S9nOG[         R                  " U [         R                  SS9n [         R                  " U[         R                  S9n[         R                  " [         R                  " U5      5      nU R                  u    n[        XQ-  S5      n[        XV5      u  pxUS-   n	Xh-
  n
X-  X-  -   U:w  a  [        S5      e/ nSnX4X44 H0  u  p[        U5       H  nUR                  XU-   45        X-  nM     M2      [        X@X5        [        U5       H  nUU   u  nn[        US-
  S
S
5       H  nX   u  nnU UU2UU24   nUU-
  S:  a&  UUUU2UU24   R%                  UUU2UU24   5      -
  nUUU2UU24   nUUU2UU24   nU(       a  ['        UUU5      u  nnnO[)        UUU5      u  nnnUU-  UUU2UU24'   M     M     U$ ! [         a  n[!        UR"                  6 UeS	nAff = f)a  
Matrix square root of an upper triangular matrix.

This is a helper function for `sqrtm` and `logm`.

Parameters
----------
T : (N, N) array_like upper triangular
    Matrix whose square root to evaluate
blocksize : int, optional
    If the blocksize is not degenerate with respect to the
    size of the input array, then use a blocked algorithm. (Default: 64)

Returns
-------
sqrtm : (N, N) ndarray
    Value of the sqrt function at `T`

References
----------
.. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013)
       "Blocked Schur Algorithms for Computing the Matrix Square Root,
       Lecture Notes in Computer Science, 7782. pp. 171-182.

g        )initialr   C)dtypeorder)r   r   zinternal inconsistencyN)npdiag	isrealobjminasarray
complex128float64sqrtshapemaxdivmod	Exceptionrangeappendr   RuntimeErrorr   argsdotr   r   )T	blocksizeT_diagkeep_it_realRnnblocksbsmallnlargeblargensmallstart_stop_pairsstartcountsizeiejjstartjstopistartistopSRiiRjjxscaleinfos                               r   _sqrtm_triurK      s   4 WWQZF<<?Frvvfb'AQ'FL JJqS9F"--8JJq

#6F"**5
 A 77DAq!.!$G A'NFaZFF(A-011 E(6*:;uA##UDL$9:ME  <)! 0:
 7^(+qsB#A,/MFE&,u,-A1uqy&,f4599!E&L<B5L=I ;J K K F5L&,./CF5L&,./C!'S!!45$!'S!!45$,-IAfUlF5L()! $ * H3  )!&&!q()s   I$ $
J.JJc                    [        U SSS9n [        U R                  5      S:w  a  [        S5      eUS:  a  [        S5      e[	        U 5      u  n [
        R                  " U 5      nU(       a  [        U 5      u  pE[
        R                  " U5      n[
        R                  " US5      n[
        R                  " UR                  5      R                  n[        U5      U[        USS 5      [        USS 5      -   -  :  n	U	R                  5       (       a  [        XE5      u  pEO[        U S	S
9u  pESn
 [        XBS9n[
        R                   " U5      R"                  nUR%                  U5      R%                  U5      n[
        R&                  " U R                  [
        R(                  " U5      (       a  SOS5      nUR+                  USS9nU(       a  U
(       a  [5        S5        U$  [7        UR%                  U5      U -
  S5      S-  [7        U S5      -  nX4$ ! [,         a:    Sn
[
        R.                  " U 5      nUR1                  [
        R2                  5         Nf = f! [         a    [
        R8                  n X4$ f = f)a  
Matrix square root.

Parameters
----------
A : (N, N) array_like
    Matrix whose square root to evaluate
disp : bool, optional
    Print warning if error in the result is estimated large
    instead of returning estimated error. (Default: True)
blocksize : integer, optional
    If the blocksize is not degenerate with respect to the
    size of the input array, then use a blocked algorithm. (Default: 64)

Returns
-------
sqrtm : (N, N) ndarray
    Value of the sqrt function at `A`. The dtype is float or complex.
    The precision (data size) is determined based on the precision of
    input `A`.

errest : float
    (if disp == False)

    Frobenius norm of the estimated error, ||err||_F / ||A||_F

References
----------
.. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013)
       "Blocked Schur Algorithms for Computing the Matrix Square Root,
       Lecture Notes in Computer Science, 7782. pp. 171-182.

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import sqrtm
>>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
>>> r = sqrtm(a)
>>> r
array([[ 0.75592895,  1.13389342],
       [ 0.37796447,  1.88982237]])
>>> r.dot(r)
array([[ 1.,  3.],
       [ 1.,  4.]])

T)check_finite
as_inexact   z$Non-matrix input to matrix function.r   z#The blocksize should be at least 1.r   Ncomplex)outputF)r0   y              ?)copyzFailed to find a square root.fro)r   lenr&   
ValueErrorr   r   r    r	   diagonalfinfor   epsabsanyr
   rK   	conjugater/   r.   result_typeiscomplexobjastyper   
empty_likefillnanprintr   inf)Adispr0   r2   r/   Zd0d1rX   needs_conversionfailflagr3   ZHXr   arg2s                   r   r   r   v   s   ^ 	14DAA
177|q?@@1}>??	A	BA<<?LQx[[^[[Bhhqww##r7SC12K#b"g,,F%GG!!1=DAQy)H	/\\!_EE!HLLqwwbooa.@.@aHHHUH' 12	a1e,a/$q%.@D
 w!  MM!	rvv  	66Dw		s&   -BH  .I AIII76I7)@   )Trn   )__doc____all__numpyr   scipy._lib._utilr   _miscr   lapackr   r   _decomp_schurr	   r
   _basicr   linalgLinAlgErrorr   _matfuncs_sqrtm_triur   rK   r   r   r   r   <module>rz      sO    )  /  " ) &	&& 	 4WtWr   