§
    ´³ÙgYb  ã                   óª  — d dl Z d dlmZ d dlZd dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZmZ ddlmZ ddlmZ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" ddl#m$Z$m%Z% ddl&m'Z' g d¢Z( ej)        d¦  «        j*        Z* ej)        d¦  «        j*        Z+ddd dd ddœZ,d„ Z-d"d„Z.d„ Z/d#d„Z0d„ Z1d„ Z2d„ Z3d„ Z4d„ Z5d„ Z6d„ Z7d„ Z8d#d„Z9d#d „Z:d!„ Z;dS )$é    N)Úproduct)ÚdotÚdiagÚprodÚlogical_notÚravelÚ	transposeÚ	conjugateÚabsoluteÚamaxÚsignÚisfiniteÚtriu)ÚLinAlgErrorÚ	bandwidthé   )Únorm)ÚsolveÚinv)Úsvd)ÚschurÚrsf2csf)Úexpm_frechetÚ	expm_cond)Úsqrtm)Úpick_pade_structureÚpade_UV_calc)Ú_funm_loops)ÚexpmÚcosmÚsinmÚtanmÚcoshmÚsinhmÚtanhmÚlogmÚfunmÚsignmr   Úfractional_matrix_powerr   r   Ú
khatri_raoÚdÚf)ÚiÚlr,   r+   ÚFÚDc                 ó´   — t          j        | ¦  «        } t          | j        ¦  «        dk    s| j        d         | j        d         k    rt	          d¦  «        ‚| S )aƒ  
    Wraps asarray with the extra requirement that the input be a square matrix.

    The motivation is that the matfuncs module has real functions that have
    been lifted to square matrix functions.

    Parameters
    ----------
    A : array_like
        A square matrix.

    Returns
    -------
    out : ndarray
        An ndarray copy or view or other representation of A.

    é   r   r   z expected square array_like input)ÚnpÚasarrayÚlenÚshapeÚ
ValueError©ÚAs    úL/granary/edev/quantum/lib/python3.11/site-packages/scipy/linalg/_matfuncs.pyÚ_asarray_squarer;   $   sN   € õ$ 	Œ
�1‰Œ€AÝ
ˆ1Œ7�|„|�qÒÐ˜AœG AœJ¨!¬'°!¬*Ò4Ð4ÝÐ;Ñ<Ô<Ð<Ø€Hó    c                 ó   — t          j        | ¦  «        rit          j        |¦  «        rU|€0t          dz  t          dz  dœt
          |j        j                          }t          j        |j	        d|¬¦  «        r|j
        }|S )a(  
    Return either B or the real part of B, depending on properties of A and B.

    The motivation is that B has been computed as a complicated function of A,
    and B may be perturbed by negligible imaginary components.
    If A is real and B is complex with small imaginary components,
    then return a real copy of B.  The assumption in that case would be that
    the imaginary components of B are numerical artifacts.

    Parameters
    ----------
    A : ndarray
        Input array whose type is to be checked as real vs. complex.
    B : ndarray
        Array to be returned, possibly without its imaginary part.
    tol : float
        Absolute tolerance.

    Returns
    -------
    out : real or complex array
        Either the input array B or only the real part of the input array B.

    Nç     @�@g    €„.A©r   r   ç        )Úatol)r3   Ú	isrealobjÚiscomplexobjÚfepsÚepsÚ_array_precisionÚdtypeÚcharÚallcloseÚimagÚreal)r9   ÚBÚtols      r:   Ú_maybe_realrN   <   su   € õ4 
„|�A�„ð �2œ?¨1Ñ-Ô-ð Øˆ;Ý˜3‘h¥3 s¡7Ð+Ð+Õ,<¸Q¼W¼\Ô,JÔKˆCÝŒ;�q”v˜s¨Ð-Ñ-Ô-ð 	Ø”ˆAØ€Hr<   c                 óh   — t          | ¦  «        } ddl}|j        j                             | |¦  «        S )aò  
    Compute the fractional power of a matrix.

    Proceeds according to the discussion in section (6) of [1]_.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix whose fractional power to evaluate.
    t : float
        Fractional power.

    Returns
    -------
    X : (N, N) array_like
        The fractional power of the matrix.

    References
    ----------
    .. [1] Nicholas J. Higham and Lijing lin (2011)
           "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
           SIAM Journal on Matrix Analysis and Applications,
           32 (3). pp. 1056-1078. ISSN 0895-4798

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import fractional_matrix_power
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> b = fractional_matrix_power(a, 0.5)
    >>> b
    array([[ 0.75592895,  1.13389342],
           [ 0.37796447,  1.88982237]])
    >>> np.dot(b, b)      # Verify square root
    array([[ 1.,  3.],
           [ 1.,  4.]])

    r   N)r;   Úscipy.linalg._matfuncs_inv_ssqÚlinalgÚ_matfuncs_inv_ssqÚ_fractional_matrix_power)r9   ÚtÚscipys      r:   r)   r)   b   s9   € õR 	˜ÑÔ€AØ)Ð)Ð)Ð)ØŒ<Ô)×BÒBÀ1ÀaÑHÔHÐHr<   Tc                 ó&  — t          j        | ¦  «        } ddl}|j        j                             | ¦  «        }t          | |¦  «        }dt          z  }t          j        dd¬¦  «        5  t          t          |¦  «        | z
  d¦  «        t          j        t          | d¦  «        | j        ¬¦  «        j        d         z  }ddd¦  «         n# 1 swxY w Y   |r8t          |¦  «        r||k    r!d	|› �}t          j        |t           d
¬¦  «         |S ||fS )a  
    Compute matrix logarithm.

    The matrix logarithm is the inverse of
    expm: expm(logm(`A`)) == `A`

    Parameters
    ----------
    A : (N, N) array_like
        Matrix whose logarithm to evaluate
    disp : bool, optional
        Emit warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    logm : (N, N) ndarray
        Matrix logarithm of `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    References
    ----------
    .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2012)
           "Improved Inverse Scaling and Squaring Algorithms
           for the Matrix Logarithm."
           SIAM Journal on Scientific Computing, 34 (4). C152-C169.
           ISSN 1095-7197

    .. [2] Nicholas J. Higham (2008)
           "Functions of Matrices: Theory and Computation"
           ISBN 978-0-898716-46-7

    .. [3] Nicholas J. Higham and Lijing lin (2011)
           "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
           SIAM Journal on Matrix Analysis and Applications,
           32 (3). pp. 1056-1078. ISSN 0895-4798

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import logm, expm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> b = logm(a)
    >>> b
    array([[-1.02571087,  2.05142174],
           [ 0.68380725,  1.02571087]])
    >>> expm(b)         # Verify expm(logm(a)) returns a
    array([[ 1.,  3.],
           [ 1.,  4.]])

    r   Néè  Úignore)ÚdivideÚinvalidr   ©rG   © z1logm result may be inaccurate, approximate err = r2   )Ú
stacklevel)r3   r4   rP   rQ   rR   Ú_logmrN   rE   Úerrstater   r   rG   rK   r   ÚwarningsÚwarnÚRuntimeWarning)r9   ÚdisprU   r/   ÚerrtolÚerrestÚmessages          r:   r&   r&   �   s_  € õn 	Œ
�1‰Œ€Aà)Ð)Ð)Ð)ØŒÔ&×,Ò,¨QÑ/Ô/€AÝ�A�qÑÔ€AØ•#‰X€Få	Œ˜H¨hÐ	7Ñ	7Ô	7ð Uð UÝ•d˜1‘g”g˜a‘i Ñ#Ô#¥b¤jµ°a¸±´À1Ä7Ð&KÑ&KÔ&KÔ&PÐQSÔ&TÑTˆðUð Uð Uñ Uô Uð Uð Uð Uð Uð Uð Uøøøð Uð Uð Uð Uàð Ý˜ÑÔð 	A 6¨VÒ#3Ð#3ØRÈ&ÐRÐRˆGÝŒM˜'¥>¸aÐ@Ñ@Ô@Ð@Øˆà�&ˆyÐs   Á(AC
Ã
CÃCc           
      óh	  — t          j        | ¦  «        }|j        dk    rE|j        dk     r:t          j        t          j        |                     ¦   «         ¦  «        gg¦  «        S |j        dk     rt          d¦  «        ‚|j        d         |j        d         k    rt          d¦  «        ‚t          |j        Ž dk    rCt          t          j        d|j        ¬¦  «        ¦  «        j        }t          j        ||¬¦  «        S |j        dd	…         d
k    rt          j        |¦  «        S t          j        |j        t           j        ¦  «        s |                     t           j        ¦  «        }n4|j        t           j        k    r|                     t           j        ¦  «        }|j        d         }t          j        |j        |j        ¬¦  «        }t          j        d||f|j        ¬¦  «        }t+          d„ |j        d	d…         D ¦   «         Ž D �]§}||         }t-          |¦  «        }t/          |¦  «        s<t          j        t          j        t          j        |¦  «        ¦  «        ¦  «        ||<   Œe||dd	d	…d	d	…f<   t3          |¦  «        \  }	}
|	dk     rt5          d|	› d�¦  «        ‚t7          ||	¦  «        }|dk    r,|dk    rt5          d|› d�¦  «        ‚t9          d|› d�¦  «        ‚|d         }|
dk    �rd|d         dk    s|d         dk    �r4t          j        |¦  «        }t          j        |d|
 z  z  ¦  «        t          j        d|¦  «        d	d	…<   t          j        ||d         dk    rdnd¬¦  «        }t=          |
dz
  dd¦  «        D ]³}||z  }t          j        |d| z  z  ¦  «        t          j        d|¦  «        d	d	…<   t?          |d| z  z  ¦  «        |d| z  z  z  }|d         dk    r'|t          j        d|dd	…d	d…f         ¦  «        d	d	…<   Œ�|t          j        d|d	d…dd	…f         ¦  «        d	d	…<   Œ´nt=          |
¦  «        D ]}||z  }Œ|d         dk    s|d         dk    r9|d         dk    rt          j         |¦  «        nt          j!        |¦  «        ||<   �Œ¢|||<   �Œ©|S )a�  Compute the matrix exponential of an array.

    Parameters
    ----------
    A : ndarray
        Input with last two dimensions are square ``(..., n, n)``.

    Returns
    -------
    eA : ndarray
        The resulting matrix exponential with the same shape of ``A``

    Notes
    -----
    Implements the algorithm given in [1], which is essentially a Pade
    approximation with a variable order that is decided based on the array
    data.

    For input with size ``n``, the memory usage is in the worst case in the
    order of ``8*(n**2)``. If the input data is not of single and double
    precision of real and complex dtypes, it is copied to a new array.

    For cases ``n >= 400``, the exact 1-norm computation cost, breaks even with
    1-norm estimation and from that point on the estimation scheme given in
    [2] is used to decide on the approximation order.

    References
    ----------
    .. [1] Awad H. Al-Mohy and Nicholas J. Higham, (2009), "A New Scaling
           and Squaring Algorithm for the Matrix Exponential", SIAM J. Matrix
           Anal. Appl. 31(3):970-989, :doi:`10.1137/09074721X`

    .. [2] Nicholas J. Higham and Francoise Tisseur (2000), "A Block Algorithm
           for Matrix 1-Norm Estimation, with an Application to 1-Norm
           Pseudospectra." SIAM J. Matrix Anal. Appl. 21(4):1185-1201,
           :doi:`10.1137/S0895479899356080`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import expm, sinm, cosm

    Matrix version of the formula exp(0) = 1:

    >>> expm(np.zeros((3, 2, 2)))
    array([[[1., 0.],
            [0., 1.]],
    <BLANKLINE>
           [[1., 0.],
            [0., 1.]],
    <BLANKLINE>
           [[1., 0.],
            [0., 1.]]])

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    r   r2   z0The input array must be at least two-dimensionaléÿÿÿÿéþÿÿÿz-Last 2 dimensions of the array must be squarer   r[   N)r   r   é   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r\   )Úrange)Ú.0Úxs     r:   ú
<listcomp>zexpm.<locals>.<listcomp>=  s   € Ð8Ð8Ð8 a�˜q™œÐ8Ð8Ð8r<   znscipy.linalg.expm could not allocate sufficient memory while trying to compute the Pade structure (error code z).iõÿÿÿzkscipy.linalg.expm could not allocate sufficient memory while trying to compute the exponential (error code z^scipy.linalg.expm got an internal LAPACK error during the exponential computation (error code ú)zii->i)Úkg       @)"r3   r4   ÚsizeÚndimÚarrayÚexpÚitemr   r6   Úminr   ÚeyerG   Ú
empty_likeÚ
issubdtypeÚinexactÚastypeÚfloat64Úfloat16Úfloat32Úemptyr   r   Úanyr   r   ÚMemoryErrorr   ÚRuntimeErrorÚeinsumrl   Ú
_exp_sinchr   Útril)r9   ÚarG   ÚnÚeAÚAmÚindÚawÚluÚmÚsÚinfoÚeAwÚdiag_awÚsdr-   Úexp_sdÚ_s                     r:   r   r   Ù   sÖ  € õF 	Œ
�1‰Œ€AØ„v�‚{€{�q”v ’z�zÝŒx�"œ& §¢¡¤Ñ*Ô*Ð+Ð,Ñ-Ô-Ð-à„v�‚z€zÝÐLÑMÔMÐMØ„wˆr„{�a”g˜b”kÒ!Ð!ÝÐIÑJÔJÐJõ ˆAŒG€}˜ÒÐÝ•R”V˜A Q¤WÐ-Ñ-Ô-Ñ.Ô.Ô4ˆÝŒ}˜Q eÐ,Ñ,Ô,Ð,ð 	„wˆrˆsˆs„|�vÒÐÝŒv�a‰yŒyÐåŒ=˜œ¥"¤*Ñ-Ô-ð !Ø�HŠH•R”ZÑ Ô ˆˆØ	
Œ•B”JÒ	Ð	Ø�HŠH•R”ZÑ Ô ˆð 	
Œ�Œ€AÝ	Œ�!”' ¤Ð	)Ñ	)Ô	)€Bå	Œ�1�a˜�) 1¤7Ð	+Ñ	+Ô	+€Bõ Ð8Ð8¨1¬7°3°B°3¬<Ð8Ñ8Ô8Ð9ð >ñ >ˆØˆsŒVˆå�r‰]Œ]ˆÝ�2‰wŒwð 	Ý”g�bœf¥R¤W¨R¡[¤[Ñ1Ô1Ñ2Ô2ˆBˆs‰GØð
 ˆˆ1ˆaˆaˆa���ˆ7‰Ý" 2Ñ&Ô&‰ˆˆ1Ø�ŠEˆEÝð =à78ð=ð =ð =ñ >ô >ð >õ ˜B Ñ"Ô"ˆØ�1Š9ˆ9Ø�sŠ{ˆ{å!ð #Bà9=ð#Bð #Bð #Bñ Cô Cð Cõ #ð $:à26ð$:ð $:ð $:ñ ;ô ;ð ;ð �Œeˆà�Š6‰6à�1”˜’
�
  1¤¨¢
¡
õ œ' "™+œ+�å-/¬V°G¸aÀ1À"¹gÑ4EÑ-FÔ-F•”	˜' 3Ñ'Ô'¨¨¨Ñ*å”W˜R¨¨A¬°!ª¨ 2 2¸Ð;Ñ;Ô;�å˜q ™s B¨Ñ+Ô+ð 	Eð 	E�AØ ™)�Cõ 24´¸À"ÈÀrÁ(Ñ8JÑ1KÔ1K•B”I˜g sÑ+Ô+¨A¨A¨AÑ.Ý'¨°2¸¸±8Ñ(<Ñ=Ô=ÀÀaÈ1È"ÁgÁÑN�FØ˜!”u ’z�zØ>D�œ	 '¨3¨q¨r¨r°3°B°3¨w¬<Ñ8Ô8¸¸¸Ñ;Ð;à>D�œ	 '¨3¨s°¨s°A°B°B¨w¬<Ñ8Ô8¸¸¸Ñ;Ð;ð	Eõ ˜q™œð $ð $�AØ ™)�C�Cð ˆqŒE�QŠJˆJ˜B˜qœE QšJ˜JØ&(¨¤e¨q¢j j•b”g˜c‘l”l�lµb´g¸c±l´lˆBˆs‰G‰GàˆBˆs‰G‰Gà€Ir<   c                 ó   — t          j        t          j        | ¦  «        ¦  «        }t          j        | ¦  «        }|dk    }|| xx         ||          z  cc<   t          j        | d d…         |         ¦  «        ||<   |S )Nr@   rh   )r3   Údiffru   )rn   Ú	lexp_diffÚl_diffÚmask_zs       r:   r…   r…   €  sy   € å”�œ˜q™	œ	Ñ"Ô"€IÝŒW�Q‰ZŒZ€FØ�rŠ\€FØˆvˆgÐÐÔ˜& & œ/Ñ)ÐÐÑÝœ˜q  " œv fœ~Ñ.Ô.€IˆfÑØÐr<   c                 óÆ   — t          | ¦  «        } t          j        | ¦  «        r(dt          d| z  ¦  «        t          d| z  ¦  «        z   z  S t          d| z  ¦  «        j        S )a!  
    Compute the matrix cosine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array

    Returns
    -------
    cosm : (N, N) ndarray
        Matrix cosine of A

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import expm, sinm, cosm

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    ç      à?ù              ð?ù       €      ð¿)r;   r3   rC   r   rK   r8   s    r:   r    r    Š  sZ   € õB 	˜ÑÔ€AÝ	„�qÑÔð Ø•D˜˜A™‘J”J¥ c¨!¡e¡¤Ñ,Ñ-Ð-å�B�q‘D‰zŒzŒÐr<   c                 óÆ   — t          | ¦  «        } t          j        | ¦  «        r(dt          d| z  ¦  «        t          d| z  ¦  «        z
  z  S t          d| z  ¦  «        j        S )a   
    Compute the matrix sine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    sinm : (N, N) ndarray
        Matrix sine of `A`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import expm, sinm, cosm

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    y       €      à¿r�   rž   )r;   r3   rC   r   rJ   r8   s    r:   r!   r!   ²  sZ   € õB 	˜ÑÔ€AÝ	„�qÑÔð Ø•d˜2˜a™4‘j”j¥4¨¨A©¡;¤;Ñ.Ñ/Ð/å�B�q‘D‰zŒzŒÐr<   c           	      ó�   — t          | ¦  «        } t          | t          t          | ¦  «        t	          | ¦  «        ¦  «        ¦  «        S )aÙ  
    Compute the matrix tangent.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    tanm : (N, N) ndarray
        Matrix tangent of `A`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import tanm, sinm, cosm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> t = tanm(a)
    >>> t
    array([[ -2.00876993,  -8.41880636],
           [ -2.80626879, -10.42757629]])

    Verify tanm(a) = sinm(a).dot(inv(cosm(a)))

    >>> s = sinm(a)
    >>> c = cosm(a)
    >>> s.dot(np.linalg.inv(c))
    array([[ -2.00876993,  -8.41880636],
           [ -2.80626879, -10.42757629]])

    )r;   rN   r   r    r!   r8   s    r:   r"   r"   Ú  s8   € õF 	˜ÑÔ€AÝ�q�%¥ Q¡¤­¨a©¬Ñ1Ô1Ñ2Ô2Ð2r<   c                 ó‚   — t          | ¦  «        } t          | dt          | ¦  «        t          |  ¦  «        z   z  ¦  «        S )a  
    Compute the hyperbolic matrix cosine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    coshm : (N, N) ndarray
        Hyperbolic matrix cosine of `A`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> c = coshm(a)
    >>> c
    array([[ 11.24592233,  38.76236492],
           [ 12.92078831,  50.00828725]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> t = tanhm(a)
    >>> s = sinhm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    rœ   ©r;   rN   r   r8   s    r:   r#   r#     ó:   € õF 	˜ÑÔ€AÝ�q˜#¥ a¡¤­4°°©8¬8Ñ!3Ñ4Ñ5Ô5Ð5r<   c                 ó‚   — t          | ¦  «        } t          | dt          | ¦  «        t          |  ¦  «        z
  z  ¦  «        S )a  
    Compute the hyperbolic matrix sine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    sinhm : (N, N) ndarray
        Hyperbolic matrix sine of `A`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> s = sinhm(a)
    >>> s
    array([[ 10.57300653,  39.28826594],
           [ 13.09608865,  49.86127247]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> t = tanhm(a)
    >>> c = coshm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    rœ   r¢   r8   s    r:   r$   r$   (  r£   r<   c           	      ó�   — t          | ¦  «        } t          | t          t          | ¦  «        t	          | ¦  «        ¦  «        ¦  «        S )a  
    Compute the hyperbolic matrix tangent.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array

    Returns
    -------
    tanhm : (N, N) ndarray
        Hyperbolic matrix tangent of `A`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> t = tanhm(a)
    >>> t
    array([[ 0.3428582 ,  0.51987926],
           [ 0.17329309,  0.86273746]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> s = sinhm(a)
    >>> c = coshm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    )r;   rN   r   r#   r$   r8   s    r:   r%   r%   O  s8   € õF 	˜ÑÔ€AÝ�q�%¥ a¡¤­%°©(¬(Ñ3Ô3Ñ4Ô4Ð4r<   c                 ó˜  — t          | ¦  «        } t          | ¦  «        \  }}t          ||¦  «        \  }}|j        \  }}t	           |t	          |¦  «        ¦  «        ¦  «        }|                     |j        j        ¦  «        }t          |d         ¦  «        }t          ||||¦  «        \  }}t          t          ||¦  «        t          t          |¦  «        ¦  «        ¦  «        }t          | |¦  «        }t          t          dœt           |j        j                          }|dk    r|}t#          dt%          |||z  t'          t)          |d¦  «        d¦  «        z  ¦  «        ¦  «        }	t+          t-          t/          t1          |¦  «        ¦  «        ¦  «        d¬¦  «        rt2          j        }	|r|	d|z  k    rt7          d|	¦  «         |S ||	fS )	a–  
    Evaluate a matrix function specified by a callable.

    Returns the value of matrix-valued function ``f`` at `A`. The
    function ``f`` is an extension of the scalar-valued function `func`
    to matrices.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix at which to evaluate the function
    func : callable
        Callable object that evaluates a scalar function f.
        Must be vectorized (eg. using vectorize).
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    funm : (N, N) ndarray
        Value of the matrix function specified by func evaluated at `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    Notes
    -----
    This function implements the general algorithm based on Schur decomposition
    (Algorithm 9.1.1. in [1]_).

    If the input matrix is known to be diagonalizable, then relying on the
    eigendecomposition is likely to be faster. For example, if your matrix is
    Hermitian, you can do

    >>> from scipy.linalg import eigh
    >>> def funm_herm(a, func, check_finite=False):
    ...     w, v = eigh(a, check_finite=check_finite)
    ...     ## if you further know that your matrix is positive semidefinite,
    ...     ## you can optionally guard against precision errors by doing
    ...     # w = np.maximum(w, 0)
    ...     w = func(w)
    ...     return (v * w).dot(v.conj().T)

    References
    ----------
    .. [1] Gene H. Golub, Charles F. van Loan, Matrix Computations 4th ed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import funm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> funm(a, lambda x: x*x)
    array([[  4.,  15.],
           [  5.,  19.]])
    >>> a.dot(a)
    array([[  4.,  15.],
           [  5.,  19.]])

    )r   r   r?   r@   r   r   )ÚaxisrW   z0funm result may be inaccurate, approximate err =)r;   r   r   r6   r   r|   rG   rH   Úabsr   r   r	   r
   rN   rD   rE   rF   rw   Úmaxr   r   r   r   r   r   r3   ÚinfÚprint)
r9   Úfuncrc   ÚTÚZrˆ   r/   ÚmindenrM   Úerrs
             r:   r'   r'   v  s�  € õ~ 	˜ÑÔ€Aå�‰8Œ8�D€A€qÝ�1�a‰=Œ=�D€A€qØŒ7�D€A€qÝˆTˆT•$�q‘'”'‰]Œ]ÑÔ€AØ	�Š�””ÑÔ€Aå��4”‰\Œ\€Fõ ˜A˜q ! VÑ,Ô,�I€A€vå�C��1‰IŒI•y¥¨1¡¤Ñ.Ô.Ñ/Ô/€AÝ�A�qÑÔ€Aå•sÐ
Ð
Õ,¨Q¬W¬\Ô:Ô
;€CØ�‚}€}ØˆÝ
ˆa•�S˜3˜v™:¥t­D°°A©J¬J¸Ñ':Ô':Ñ:Ñ;Ô;Ñ
<Ô
<€CÝ�E•+�h q™kœkÑ*Ô*Ñ+Ô+°!Ð4Ñ4Ô4ð ÝŒfˆØð Ø��c‘Š>ˆ>ÝÐDÀcÑJÔJÐJØˆà�#ˆvˆr<   c                 óœ  — t          | ¦  «        } d„ }t          | |d¬¦  «        \  }}dt          z  dt          z  dœt          |j        j                          }||k     r|S t          | d¬¦  «        }t          j	        |¦  «        }d|z  }| |t          j
        | j        d         ¦  «        z  z   }	|}
t          d	¦  «        D ]`}t          |	¦  «        }d|	|z   z  }	dt          |	|	¦  «        |	z   z  }t          t          ||¦  «        |z
  d
¦  «        }||k     s|
|k    r n|}
Œa|r't!          |¦  «        r||k    rt#          d|¦  «         |	S |	|fS )a'  
    Matrix sign function.

    Extension of the scalar sign(x) to matrices.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix at which to evaluate the sign function
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    signm : (N, N) ndarray
        Value of the sign function at `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    Examples
    --------
    >>> from scipy.linalg import signm, eigvals
    >>> a = [[1,2,3], [1,2,1], [1,1,1]]
    >>> eigvals(a)
    array([ 4.12488542+0.j, -0.76155718+0.j,  0.63667176+0.j])
    >>> eigvals(signm(a))
    array([-1.+0.j,  1.+0.j,  1.+0.j])

    c                 óú   — t          j        | ¦  «        }|j        j        dk    rdt          z  t          | ¦  «        z  }ndt          z  t          | ¦  «        z  }t          t          |¦  «        |k    |z  ¦  «        S )Nr,   r>   )	r3   rK   rG   rH   rD   r   rE   r   r   )rn   ÚrxÚcs      r:   Úrounded_signzsignm.<locals>.rounded_sign÷  se   € ÝŒW�Q‰ZŒZˆØŒ8Œ=˜CÒÐØ•D‘�˜a™œÑ ˆAˆAà•C‘�˜Q™œ‘ˆAÝ•X˜b‘\”\ AÒ%¨Ñ+Ñ,Ô,Ð,r<   r   )rc   r>   r?   F)Ú
compute_uvrœ   éd   r   z1signm result may be inaccurate, approximate err =)r;   r'   rD   rE   rF   rG   rH   r   r3   r   Úidentityr6   rl   r   r   r   r   r«   )r9   rc   rµ   Úresultre   rd   ÚvalsÚmax_svr´   ÚS0Úprev_errestr-   ÚiS0ÚPps                 r:   r(   r(   Ô  s~  € õB 	˜ÑÔ€Að-ð -ð -õ ˜!˜\°Ð2Ñ2Ô2�N€FˆFØ•T‘˜c¥#™gÐ&Ð&Õ'7¸¼Ô8IÔ'JÔK€FØ�‚€Øˆõ ˆq˜UÐ#Ñ#Ô#€DÝŒW�T‰]Œ]€Fð 	ˆF‰
€AØ	
ˆQ�rŒ{˜1œ7 1œ:Ñ&Ô&Ñ&Ñ	&€BØ€KÝ�3‰ZŒZð ð ˆÝ�"‰gŒgˆØ�"�s‘(‰^ˆØ•#�b˜"‘+”+˜b‘.Ñ!ˆÝ•c˜"˜b‘k”k "‘n aÑ(Ô(ˆØ�FŠ?ˆ?˜k¨VÒ3Ð3ØˆEØˆˆØð Ý˜ÑÔð 	O 6¨VÒ#3Ð#3ÝÐEÀvÑNÔNÐNØˆ	à�6ˆzÐr<   c                 óR  — t          j        | ¦  «        } t          j        |¦  «        }| j        dk    r|j        dk    st          d¦  «        ‚| j        d         |j        d         k    st          d¦  «        ‚| j        dk    s|j        dk    r@| j        d         |j        d         z  }| j        d         }t          j        | ||f¬¦  «        S | ddd…t           j        dd…f         |dt           j        dd…dd…f         z  }|                     d	|j        dd…         z   ¦  «        S )
aÁ  
    Khatri-rao product

    A column-wise Kronecker product of two matrices

    Parameters
    ----------
    a : (n, k) array_like
        Input array
    b : (m, k) array_like
        Input array

    Returns
    -------
    c:  (n*m, k) ndarray
        Khatri-rao product of `a` and `b`.

    Notes
    -----
    The mathematical definition of the Khatri-Rao product is:

    .. math::

        (A_{ij}  \bigotimes B_{ij})_{ij}

    which is the Kronecker product of every column of A and B, e.g.::

        c = np.vstack([np.kron(a[:, k], b[:, k]) for k in range(b.shape[1])]).T

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import linalg
    >>> a = np.array([[1, 2, 3], [4, 5, 6]])
    >>> b = np.array([[3, 4, 5], [6, 7, 8], [2, 3, 9]])
    >>> linalg.khatri_rao(a, b)
    array([[ 3,  8, 15],
           [ 6, 14, 24],
           [ 2,  6, 27],
           [12, 20, 30],
           [24, 35, 48],
           [ 8, 15, 54]])

    r2   z(The both arrays should be 2-dimensional.r   z6The number of columns for both arrays should be equal.r   )r6   .N)rh   )	r3   r4   rs   r7   r6   rr   ry   ÚnewaxisÚreshape)r‡   ÚbrŽ   rˆ   r´   s        r:   r*   r*   $  s  € õZ 	Œ
�1‰Œ€AÝ
Œ
�1‰Œ€AàŒF�aŠKˆK˜AœF ašK˜KÝÐCÑDÔDÐDàŒ7�1Œ:˜œ œÒ#Ð#Ýð ,ñ -ô -ð 	-ð 	„v�‚{€{�a”f ’k�kØŒG�AŒJ˜œ œÑ#ˆØŒG�AŒJˆÝŒ}˜Q q¨! fÐ-Ñ-Ô-Ð-ð 	
ˆ#ˆqˆqˆq•"”*˜a˜a˜aÐ
Ô  1 S­"¬*°a°a°a¸¸¸Ð%:Ô#;Ñ;€AØ�9Š9�U˜QœW Q R Rœ[Ñ(Ñ)Ô)Ð)r<   )N)T)<r`   Ú	itertoolsr   Únumpyr3   r   r   r   r   r   r	   r
   r   r   r   r   r   Úscipy.linalgr   r   Ú_miscr   Ú_basicr   r   Ú_decomp_svdr   Ú_decomp_schurr   r   Ú_expm_frechetr   r   Ú_matfuncs_sqrtmr   Ú_matfuncs_expmr   r   Ú_linalg_pythranr   Ú__all__ÚfinforE   rD   rF   r;   rN   r)   r&   r   r…   r    r!   r"   r#   r$   r%   r'   r(   r*   r\   r<   r:   ú<module>rÑ      s  ðð €€€Ø Ð Ð Ð Ð Ð à Ð Ð Ð ðDð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð Dð 0Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø "Ð "Ð "Ð "Ð "Ð "Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø (Ð (Ð (Ð (Ð (Ð (ð&ð &ð &€ð €b„hˆs�m„mÔ€Ø€r„x��}„}Ô€à ¨°¸ÀÐCÐCÐ ðð ð ð0ð ð ð ðL+Ið +Ið +Ið\Fð Fð Fð FðRdð dð dðNð ð ð%ð %ð %ðP%ð %ð %ðP$3ð $3ð $3ðN$6ð $6ð $6ðN$6ð $6ð $6ðN$5ð $5ð $5ðN[ð [ð [ð [ð|Mð Mð Mð Mð`?*ð ?*ð ?*ð ?*ð ?*r<   