
    Ug                         d Z dgZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ  G d	 d
ej        j                  ZddlmZ ddZddZdS )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rsf2csfc                       e Zd ZdS )
SqrtmErrorN)__name__
__module____qualname__     [/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/scipy/linalg/_matfuncs_sqrtm.pyr   r      s        Dr   r   )within_block_loop@   c           	      :   t          j        |           }t          j        |           ot          j        |d          dk    }|sBt          j        | t           j        d          } t          j        |t           j                  }nAt          j        | t           j        d          } t          j        |t           j                  }t          j        t          j        |                    }| j        \  }}t          ||z  d          }t          ||          \  }}|dz   }	||z
  }
|
|z  ||	z  z   |k    rt          d          g }d}|
|f||	ffD ]6\  }}t          |          D ]!}|                    |||z   f           ||z  }"7	 t          || ||           n!# t          $ r}t!          |j         |d	}~ww xY wt          |          D ]}||         \  }}t          |dz
  d
d
          D ]}||         \  }}| ||||f         }||z
  dk    r0||||||f                             |||||f                   z
  }|||||f         }|||||f         }|rt'          |||          \  }}}nt)          |||          \  }}}||z  |||||f<   |S )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_triurH      s5   4 WQZZF<??Frvfb'A'A'AQ'FL  6JqS999F"-888Jq
#666F"*555
  A 7DAq!y.!$$G Aw''NFFaZFvF&(A--0111 E(66*:;  tu 	 	A##UEDL$9:::TMEE	
)!Q 0':::: ) ) )!&!q() 7^^ 6 6(+qsB## 	6 	6A,Q/MFE&,u,-A1uqyy&,f4599!E&L<B5L=I ;J K K K F5L&,./CF5L&,./C 5!'S!!4!45$$!'S!!4!45$,-IAfUlF5L())!	6& Hs   
F 
F;'F66F;Tc                 D   t          j        |           j        j        }t	          | dd          } t          | j                  dk    rt          d          |dk     rt          d          t          j        |           }|rt          |           \  }}t          j
        |          }t          j
        |d          }t          j        |j                  j        }	t          |          |	t          |dd                   t          |dd                   z   z  k    }
|
                                rt          ||          \  }}nt          | d	
          \  }}d}	 t!          ||          }t          j        |          j        }|                    |                              |          }t          j        |          s/|                    dt          j        |dd           d          }nxt/          t           d          r2|                    dt          j        |dz  dd           d          }n1|                    dt          j        |dz  dd           d          }nE# t0          $ r8 d}t          j        |           }|                    t           j                   Y nw xY w|r|rt9          d           |S 	 t;          |                    |          | z
  d          dz  t;          | d          z  }n# t          $ r t           j        }Y nw xY w||fS )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`. When the dtype is float, the precision is the same as `A`.
        When the dtype is complex, the precision is double that of `A`. The
        precision might be clipped by each dtype precision range.

    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)r-   f   )copy
complex256c       zFailed to find a square root.fro)r   r   r   itemsizer   lenr#   
ValueErrorr   r	   diagonalfinfoepsabsanyr
   rH   	conjugater,   r+   iscomplexobjastypecliphasattrr   
empty_likefillnanprintr   inf)Adispr-   	byte_sizer/   r,   Zd0d1r\   needs_conversionfailflagr0   ZHXarg2s                   r   r   r   u   s    b 
1#,I14DAAAA
17||q?@@@1}}>???<??L 	*Qxx1[^^[Bhqw#r77SC122KK#b"g,,,F%GG!! 	!1a==DAqQy)))1HY///\!__EE!HHLLq!! 		L8RWY26688uEEAA r<(( LHH>1a!<!<>>UHKKHH>1a!<!<>>UHKK   M!	rv
   	31222	a1e,,a/$q%..@DD 	 	 	6DDD	 $ws%   DI, ,?J.-J.:L LL)r   )Tr   )__doc____all__numpyr   scipy._lib._utilr   _miscr   lapackr   r   _decomp_schurr	   r
   linalgLinAlgErrorr   _matfuncs_sqrtm_triur   rH   r   r   r   r   <module>r~      s     )     / / / / / /       " " " " " " " " ) ) ) ) ) ) ) )	 	 	 	 	& 	 	 	 4 3 3 3 3 3W W W Wta a a a a ar   