o
    Ö­j|  ã                   @   sŽ   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 dd	lmZ G d
d„ dejjƒZddlmZ ddd„Zd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Úrsf2csf)Ú_ensure_dtype_cdszc                   @   s   e Zd ZdS )Ú
SqrtmErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úY/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/linalg/_matfuncs_sqrtm.pyr      s    r   )Úwithin_block_loopé@   c              
   C   sN  t  | ¡}t  | ¡ot j|dd�dk}|s't j| t jdd�} t j|t jd�}nt j| t jdd�} t j|t jd�}t  t  |¡¡}| j\}}t	|| dƒ}t
||ƒ\}}|d }	|| }
|
| ||	  |kritdƒ‚g }d}|
|f||	ffD ]\}}t|ƒD ]}| ||| f¡ ||7 }q}quz	t|| ||ƒ W n ty© } zt|jŽ |‚d	}~ww t|ƒD ]v}|| \}}t|d d
d
ƒD ]e}|| \}}| ||…||…f }|| dkrí||||…||…f  |||…||…f ¡ }|||…||…f }|||…||…f }|�rt|||ƒ\}}}n	t|||ƒ\}}}|| |||…||…f< q¾q®|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Úinfor   r   r   Ú_sqrtm_triu   sZ   


þ€ÿ
ÿðrE   Tc                 C   s¸  t | ddd�} t| jƒdkrtdƒ‚|dk rtdƒ‚t| ƒ\} t | ¡}|r`t| ƒ\}}t |¡}t |d¡}t 	|j
¡j}t|ƒ|t|dd… ƒt|dd… ƒ  k}	|	 ¡ r_t||ƒ\}}nt| d	d
�\}}d}
z+t||d�}t |¡j}| |¡ |¡}t | j
t |¡rŠdnd¡}|j|dd�}W n ty«   d}
t | ¡}| tj¡ Y nw |r¶|
r´tdƒ |S zt| |¡|  dƒd t| dƒ }W ||fS  tyÛ   tj}Y ||fS w )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)r*   y              ð?)ÚcopyzFailed to find a square root.Úfro)r   Úlenr    Ú
ValueErrorr
   r   r   r   ÚdiagonalÚfinfor   ÚepsÚabsÚanyr	   rE   Ú	conjugater)   r(   Úresult_typeÚiscomplexobjÚastyper   Ú
empty_likeÚfillÚnanÚprintr   Úinf)ÚAÚdispr*   r,   r)   ÚZÚd0Úd1rQ   Úneeds_conversionÚfailflagr-   ÚZHÚXr   Úarg2r   r   r   r   v   sP   /


,€
ý$üü)r   )Tr   )Ú__doc__Ú__all__Únumpyr   Úscipy._lib._utilr   Ú_miscr   Úlapackr   r   Ú_decomp_schurr   r	   Ú_basicr
   ÚlinalgÚLinAlgErrorr   Ú_matfuncs_sqrtm_triur   rE   r   r   r   r   r   Ú<module>   s    
Z