Ë
    ü�Dj¼b  ã                   ó°  — 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$ g d¢Z% ejL                  d«      jN                  Z' ejL                  d«      jN                  Z(ddd dd ddœZ)d„ Z*d d„Z+d„ Z,d!d„Z-d„ Z.d„ Z/d„ Z0d„ Z1d„ Z2d„ Z3d„ Z4d„ Z5d!d„Z6d!d„Z7d„ Z8y)"é    )ÚproductN)Ú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)Ú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k7  s| j                  d   | j                  d   k7  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    úZC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\scipy/linalg/_matfuncs.pyÚ_asarray_squarer:   "   sI   € ô$ 	�
‰
�1‹€AÜ
ˆ1�7‰7ƒ|�qÒ˜AŸG™G A™J¨!¯'©'°!©*Ò4ÜÐ;Ó<Ð<Ø€Hó    c                 ó  — t        j                  | «      rvt        j                  |«      ra|€1t        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.

    ç     @�@g    €„.A©r   r   ç        )Úatol)r2   Ú	isrealobjÚiscomplexobjÚfepsÚepsÚ_array_precisionÚdtypeÚcharÚallcloseÚimagÚreal)r8   ÚBÚtols      r9   Ú_maybe_realrM   :   sf   € ô4 
‡|�|�A„œ2Ÿ?™?¨1Ô-Øˆ;Ü˜3‘h¤3 s¡7Ñ+Ô,<¸Q¿W¹W¿\¹\Ñ,JÑKˆCÜ�;‰;�q—v‘v˜s¨Õ-Ø—‘ˆAØ€Hr;   c                 ól   — t        | «      } ddl}|j                  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)r8   ÚtÚscipys      r9   r(   r(   `   s/   € ôR 	˜Ó€AÛ)Ø�<‰<×)Ñ)×BÑBÀ1ÀaÓHÐHr;   c                 ó&  — t        | «      } ddl}|j                  j                  j	                  | «      }t        | |«      }dt        z  }t        t        |«      | z
  d«      t        | d«      z  }|rt        |«      r||k\  r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
        Print 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éè  r   z0logm result may be inaccurate, approximate err =)r:   rO   rP   rQ   Ú_logmrM   rD   r   r   r   Úprint)r8   ÚdisprT   r.   ÚerrtolÚerrests         r9   r%   r%   Ž   sŒ   € ôn 	˜Ó€Aã)Ø�‰×&Ñ&×,Ñ,¨QÓ/€AÜ�A�qÓ€AØ”#‰X€Fä”$�q“'˜!‘)˜QÓ¤$ q¨!£*Ñ,€FÙÜ˜Ô 6¨VÒ#3ÜÐDÀfÔMØˆà�&ˆyÐr;   c           
      óþ  — t        j                  | «      }|j                  dk(  rG|j                  dk  r8t        j                  t        j
                  |j                  «       «      gg«      S |j                  dk  rt        d«      ‚|j                  d   |j                  d   k7  rt        d«      ‚|j                  d   }t        |j                  Ž dk(  rt        j                  |«      S |j                  dd d	k(  rt        j
                  |«      S t        j                  |j                  t         j                  «      s |j                  t         j                  «      }n<|j                  t         j                   k(  r|j                  t         j"                  «      }|j                  d   }t        j$                  |j                  |j                  ¬
«      }t        j$                  d||f|j                  ¬
«      }t'        |j                  dd D �cg c]  }t)        |«      ‘Œ c}Ž D �]P  }||   }t+        |«      }t-        |«      s?t        j.                  t        j
                  t        j.                  |«      «      «      ||<   Œ^||ddd…dd…f<   t1        |«      \  }	}
|
dk7  r*|ddxxx d|
 z  ggd|
 z  ggd|
 z  ggd|
 z  gggz  ccc t3        |||	«       |d   }|
dk7  �rF|d   dk(  s	|d   dk(  �r t        j.                  |«      }t        j
                  |d|
 z  z  «      t        j4                  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        j4                  d|«      dd t7        |d| z  z  «      |d| z  z  z  }|d   dk(  r#|t        j4                  d|dd…dd…f   «      dd Œ‚|t        j4                  d|dd…dd…f   «      dd Œ¥ nt)        |
«      D ]  }||z  }Œ	 |d   dk(  s|d   dk(  r7|d   dk(  rt        j8                  |«      nt        j:                  |«      ||<   �ŒL|||<   �ŒS |S c c}w )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   r1   z0The input array must be at least two-dimensionaléÿÿÿÿéþÿÿÿz-Last 2 dimensions of the array must be squarer   N)r   r   )rF   é   é   é   é@   zii->i)Úkg       @)r2   r3   ÚsizeÚndimÚarrayÚexpÚitemr   r5   ÚminÚ
empty_likeÚ
issubdtyperF   ÚinexactÚastypeÚfloat64Úfloat16Úfloat32Úemptyr   Úranger   Úanyr   r   r   ÚeinsumÚ
_exp_sinchr   Útril)r8   ÚaÚnÚeAÚAmÚxÚindÚawÚluÚmÚsÚeAwÚdiag_awÚsdr,   Úexp_sdÚ_s                    r9   r   r   Õ   sò  € ôF 	�
‰
�1‹€AØ‡v�v�‚{�q—v‘v ’zÜ�x‰xœ"Ÿ&™& §¡£Ó*Ð+Ð,Ó-Ð-à‡v�v�‚zÜÐLÓMÐMØ‡w�wˆr�{�a—g‘g˜b‘kÒ!ÜÐIÓJÐJØ	�‰�‰€Aä
ˆA�G‰G€}˜ÒÜ�}‰}˜QÓÐð 	‡w�wˆrˆs€|�vÒÜ�v‰v�a‹yÐä�=‰=˜Ÿ™¤"§*¡*Ô-Ø�H‰H”R—Z‘ZÓ ‰Ø	
�‰”B—J‘JÒ	Ø�H‰H”R—Z‘ZÓ ˆð 	
�‰�‰€AÜ	�‰�!—'‘' §¡Ô	)€Bä	�‰�1�a˜�) 1§7¡7Ô	+€Bô ¨1¯7©7°3°B¨<Ö8 aœ˜q�Ò8Ð9ó 1ˆØˆs‰Vˆä�r‹]ˆÜ�2ŒwÜ—g‘gœbŸf™f¤R§W¡W¨R£[Ó1Ó2ˆBˆs‰GØð ˆˆ1Ša’ˆ7‰Ü" 2Ó&‰ˆˆ1à�Š6Øˆr�‹F˜˜a˜R™˜	�{ a¨1¨"¡g Y K°2¸¸±8°*°ÀÀqÀbÁÀ
¸|ÐLÑL‹Fä�R˜˜AÔØ�‰eˆà�‹6à�1‘˜’
  1¡¨£
ô Ÿ'™' "›+�ä-/¯V©V°G¸aÀ1À"¹gÑ4EÓ-F”—	‘	˜' 3Ó'©Ð*ä—W‘W˜R¨¨A©°!ª¡2¸Ô;�ä˜q ™s B¨Ó+ò 	E�AØ ™)�Cô 24·±¸À"ÈÀrÁ(Ñ8JÓ1K”B—I‘I˜g sÓ+©AÐ.Ü'¨°2¸¸±8Ñ(<Ó=ÀÀaÈ1È"ÁgÁÑN�FØ˜!‘u ’zØ>DœŸ	™	 '¨3¨q©r°3°B°3¨w©<Ó8¹Ñ;à>DœŸ	™	 '¨3¨s°¨s°A±B¨w©<Ó8¹Ñ;ñ	Eô ˜q›ò $�AØ ™)‘Cð$ð ˆq‰E�QŠJ˜B˜q™E QšJØ&(¨¡e¨q¢j”b—g‘g˜c”l´b·g±g¸c³lˆBˆs‹GàˆBˆs‹Gðc1ðf €Iùòg 9s   ÈQ:c                 óê   — t        j                  t        j                  | «      «      }t        j                  | «      }|dk(  }|| xx   ||    z  cc<   t        j                  | d d |   «      ||<   |S )Nr?   r]   )r2   Údiffrg   )r{   Ú	lexp_diffÚl_diffÚmask_zs       r9   ru   ru   n  si   € ä—‘œŸ™˜q›	Ó"€IÜ�W‰W�Q‹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:   r2   rB   r   rJ   r7   s    r9   r   r   x  sM   € ôB 	˜Ó€AÜ	‡��qÔØ”D˜˜A™“J¤ c¨!¡e£Ñ,Ñ-Ð-ä�B�q‘D‹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:   r2   rB   r   rI   r7   s    r9   r    r       sM   € ôB 	˜Ó€AÜ	‡��qÔØ”d˜2˜a™4“j¤4¨¨A©£;Ñ.Ñ/Ð/ä�B�q‘D‹z�‰Ðr;   c           	      óh   — 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:   rM   r   r   r    r7   s    r9   r!   r!   È  s+   € ôF 	˜Ó€AÜ�qœ%¤ Q£¬¨a«Ó1Ó2Ð2r;   c                 ób   — 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:   rM   r   r7   s    r9   r"   r"   ï  ó0   € ôF 	˜Ó€AÜ�q˜#¤ a£¬4°°«8Ñ!3Ñ4Ó5Ð5r;   c                 ób   — 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’   r7   s    r9   r#   r#     r“   r;   c           	      óh   — 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:   rM   r   r"   r#   r7   s    r9   r$   r$   =  s+   € ôF 	˜Ó€AÜ�qœ%¤ a£¬%°«(Ó3Ó4Ð4r;   c                 ó²  — t        | «      } t        | «      \  }}t        ||«      \  }}|j                  \  }}t	         |t	        |«      «      «      }|j                  |j                  j                  «      }t        |d   «      }t        d|«      D ]Û  }t        d||z
  dz   «      D ]Ä  }	|	|z   }
||	dz
  |
dz
  f   ||
dz
  |
dz
  f   ||	dz
  |	dz
  f   z
  z  }t        |	|
dz
  «      }t        ||	dz
  |f   |||
dz
  f   «      t        ||	dz
  |f   |||
dz
  f   «      z
  }||z   }||
dz
  |
dz
  f   ||	dz
  |	dz
  f   z
  }|dk7  r||z  }|||	dz
  |
dz
  f<   t        |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/        t1        t3        |«      «      «      d¬«      rt4        j6                  }|r|d|z  kD  rt9        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   )ÚaxisrV   z0funm result may be inaccurate, approximate err =)r:   r   r   r5   r   rm   rF   rG   Úabsrr   Úslicer   ri   r	   r
   rM   rC   rD   rE   Úmaxr   r   r   r   r   r   r2   ÚinfrX   )r8   ÚfuncrY   ÚTÚZrx   r.   ÚmindenÚpr,   Újr€   ÚkslÚvalÚdenrL   Úerrs                    r9   r&   r&   d  sk  € ô~ 	˜Ó€Aä�‹8�D€A€qÜ�1�a‹=�D€A€qØ�7‰7�D€A€qÜ‰T”$�q“'‹]Ó€AØ	�‰�—‘—‘Ó€Aä��4‘‹\€Fô �1�a‹[ò +ˆÜ�q˜!˜A™#˜a™%“ò 
	+ˆAØ�A‘ˆAØ�!�A‘#�q˜‘s�(‘˜q  1¡ a¨¡c ™{¨Q¨q°©s°A°a±C¨x©[Ñ8Ñ9ˆAÜ˜˜1˜Q™3“-ˆCÜ�a˜˜!™˜S˜‘k 1 S¨!¨A©# X¡;Ó/´#°a¸¸!¹¸S¸±kÀ1ÀSÈ!ÈAÉ#ÀXÁ;Ó2OÑOˆCØ�C‘ˆAØ�A�a‘C˜˜1™�H‘+  ! A¡# q¨¡s (¡Ñ+ˆCØ�cŠzØ˜‘G�ØˆAˆa�‰c�1�Q‘3ˆh‰KÜ˜¤ S£Ó*‰Fñ
	+ð+ô 	ŒC��1‹I”y¤¨1£Ó.Ó/€AÜ�A�qÓ€Aä”sÑ
Ô,¨Q¯W©W¯\©\Ñ:Ñ
;€CØ�‚}ØˆÜ
ˆa”�S˜3˜v™:¤t¬D°°A«J¸Ó':Ñ:Ó;Ó
<€CÜŒE”+œh q›kÓ*Ó+°!Õ4Ü�f‰fˆÙØ��c‘Š>ÜÐDÀcÔJØˆà�#ˆvˆr;   c                 óR  — 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 ]N  }t        |	«      }d|	|z   z  }	dt        |	|	«      |	z   z  }t        t        ||«      |z
  d
«      }||k  s|
|k(  r n|}
ŒP |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        |«      |kD  |z  «      S )Nr+   r=   )	r2   rJ   rF   rG   rC   r   rD   r   r   )r{   ÚrxÚcs      r9   Úrounded_signzsignm.<locals>.rounded_signð  s[   € Ü�W‰W�Q‹ZˆØ�8‰8�=‰=˜CÒØ”D‘œ˜a›Ñ ‰Aà”C‘œ˜Q›‘ˆAÜ”X˜b“\ AÑ%¨Ñ+Ó,Ð,r;   r   )rY   r=   r>   F)Ú
compute_uvrŒ   éd   r   z1signm result may be inaccurate, approximate err =)r:   r&   rC   rD   rE   rF   rG   r   r2   r   Úidentityr5   rr   r   r   r   r   rX   )r8   rY   rª   Úresultr[   rZ   ÚvalsÚmax_svr©   ÚS0Úprev_errestr,   ÚiS0ÚPps                 r9   r'   r'   Í  s=  € ôB 	˜Ó€Aò-ô ˜!˜\°Ô2�N€FˆFØ”T‘˜c¤#™gÑ&Ô'7¸¿¹×8IÑ8IÑ'JÑK€FØ�‚Øˆô ˆq˜UÔ#€DÜ�W‰W�T‹]€Fð 	ˆF‰
€AØ	
ˆQŒr�{‰{˜1Ÿ7™7 1™:Ó&Ñ&Ñ	&€BØ€KÜ�3‹Zò ˆÜ�"‹gˆØ�"�s‘(‰^ˆØ”#�b˜"“+˜b‘.Ñ!ˆÜ”c˜"˜b“k "‘n aÓ(ˆØ�FŠ?˜k¨VÒ3ÙØ‰ðñ Ü˜Ô 6¨VÒ#3ÜÐEÀvÔNØˆ	à�6ˆzÐr;   c                 óv  — 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(  rG| 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  }|j                  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`.

    See Also
    --------
    kron : Kronecker product

    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]])

    r1   z(The both arrays should be 2-dimensional.r   z6The number of columns for both arrays should be equal.r   )r5   .N)r]   )	r2   r3   re   r6   r5   rd   rj   ÚnewaxisÚreshape)rw   Úbr   rx   r©   s        r9   r)   r)     s  € ôb 	�
‰
�1‹€AÜ
�
‰
�1‹€Aà�F‰F�aŠK˜AŸF™F ašKÜÐCÓDÐDà�7‰7�1‰:˜Ÿ™ ™Ò#Üð ,ó -ð 	-ð 	‡v�v�‚{�a—f‘f ’kØ�G‰G�A‰J˜Ÿ™ ™Ñ#ˆØ�G‰G�A‰JˆÜ�}‰}˜Q q¨! fÔ-Ð-ð 	
ˆ#Šq”"—*‘*šaÐ
Ñ  1 S¬"¯*©*²aºÐ%:Ñ#;Ñ;€AØ�9‰9�U˜QŸW™W Q R˜[Ñ(Ó)Ð)r;   )N)T)9Ú	itertoolsr   Únumpyr2   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   Ú__all__ÚfinforD   rC   rE   r:   rM   r(   r%   r   ru   r   r    r!   r"   r#   r$   r&   r'   r)   © r;   r9   ú<module>rÆ      sâ   ðõ ã ÷D÷ D÷ Dó D÷ 0Ý ß Ý ß )ß 2Ý "ß =ò&€ð €b‡h�hˆsƒm×Ñ€Ø€r‡x�x�ƒ}×Ñ€à ¨°¸ÀÑCÐ òó0òL+Ió\DòNVòrò%òP%òP$3òN$6òN$6òN$5óNfóRMó`C*r;   