
    9=ea                        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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 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j*        d          j+        Z+ ej*        d          j+        Z,ddd dd ddZ-d Z.d"dZ/d Z0d#dZ1d Z2d Z3d Z4d Z5d Z6d Z7d Z8d Z9d#dZ:d#d Z;d! Z<dS )$    )productN)Infdotdiagprodlogical_notravel	transpose	conjugateabsoluteamaxsignisfinitetriu)sqrt)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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    6lib/python3.11/site-packages/scipy/linalg/_matfuncs.py_asarray_squarer<   #   sN    $ 	
1A
17||qAGAJ!'!*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)r4   	isrealobjiscomplexobjfepseps_array_precisiondtypecharallcloseimagreal)r:   Btols      r;   _maybe_realrO   ;   su    4 
|A 2?1-- ;#XS))*:17<*HIC;qvs--- 	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)r:   tscipys      r;   r*   r*   a   s9    R 	A))))<)BB1aHHHr=   Tc                 V   t          |           } ddl}|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<   rQ   rR   rS   _logmrO   rF   r   r    r   print)r:   disprV   r0   errtolerrests         r;   r'   r'      s    n 	A))))&,,Q//AAqA#XF$q''!)Aa*F  	N6V#3#3DfMMM&yr=   c           
         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          |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|                    t                    }n4|j        t           j        k    r|                    t           j                  }|j        dd         d
k    rQ|ddgdgf         |ddgdgf         |ddgdgf         |ddgdgf         f\  }}}}t%          ||z
  dz  d|z  |z  z             dz  }t          j        ||z   dz            }||z
  dz  }	t          j        |          }
t          j        |
          }|dk    }t          j        ||                   ||         z  ||<   t          j        |j        |j                  }||
|	|z  z   z  |ddgdgf<   ||z  |z  |ddgdgf<   ||z  |z  |ddgdgf<   ||
|	|z  z
  z  |ddgdgf<   t          j        |          r|j        S |S |j        d         }t          j        |j        |j                  }t          j        d||f|j                  }t3          d |j        dd         D              D ]}||         }t5          |          }t7          |          s<t          j        t          j        t          j        |                              ||<   e||dddddf<   t;          |          \  }}|dk    r.|ddxx         d| z  ggd| z  ggd| z  ggd| z  gggz  cc<   t=          |||           |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          }tA          |dz
  dd          D ]}||z  }t          j        |d| z  z            t          j        d|          dd<   tC          |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tA          |          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   r3   z0The input array must be at least two-dimensionalz-Last 2 dimensions of the array must be squarer   N)r   r   )r3   r3   .   g       @)rH      c                 ,    g | ]}t          |          S  )range).0xs     r;   
<listcomp>zexpm.<locals>.<listcomp>N  s    888aq888r=      @   zii->i)k)$r4   r5   sizendimarrayexpitemr   r7   min
empty_like
issubdtyperH   inexactastypefloatfloat16float32csqrtcosh	ones_likesinhemptyrC   rL   r   r   anyr   r   r   einsumre   
_exp_sinchr   tril)r:   ana1a2a3a4mueApD2AmD2coshMusinchMumaskeAAmindawlumseAwdiag_awsdr.   exp_sd_s                             r;   r    r       s   F 	
1Av{{qvzzx"&**+,---vzzLMMMwr{agbk!!IJJJ	A
AG}}Q 	wrss|vvayy="*-- !HHUOO	
BJ		HHRZ   	wrss|vC!qcM*C!qcM*C!qcM*C!qcM*,BB BrEA:"R'((+2rz""R|,v&&Qw4))BtH4Xqwrx000!Vd7l%:;3aS=!BJ03aS=!BJ03aS=!Vd7l%:;3aS=<?? 	7N	 	
A	!'	)	)	)B	1a)17	+	+	+B 88173B3<8889 1 1sVr]]2ww 	gbfRWR[[1122BsG 1aaa7"2&&166rrFFFaR	{a1"gYK28*qb
|LLFFFRAe661

1

 '"++-/VGa1"g4E-F-F	'3''*WRA!22;;;qsB++ 	E 	EA)C 24"r(8J1K1KBIgs++AAA.'28(<==a1"gNF!uzz>D	'3qrr3B3w<88;;>D	'3ssABBw<88;;	E q $ $A)CC qEQJJBqEQJJ&(eqjjbgclllbgcllBsGGBsGGIr=   c                     t          j        t          j        |                     }t          j        |           }|dk    }|| xx         ||          z  cc<   t          j        | d d         |                   ||<   |S )NrA   r_   )r4   diffro   )rg   	lexp_diffl_diffmask_zs       r;   r   r     sy    q		""IWQZZFr\Fvg&&/)q"vf~..If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<   r4   rD   r    rL   r9   s    r;   r!   r!     sZ    B 	A	q DAJJc!e,--BqDzz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<   r4   rD   r    rK   r9   s    r;   r"   r"     sZ    B 	A	q d2a4jj4A;;.//BqDzz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<   rO   r   r!   r"   r9   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<   rO   r    r9   s    r;   r$   r$     :    F 	Aq#a488!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   r9   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<   rO   r   r$   r%   r9   s    r;   r&   r&   S  s8    F 	Aq%a%((33444r=   c                    t          |           } t          |           \  }}t          ||          \  }}|j        \  }}t	           |t	          |                              }|                    |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k    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          }|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   rA   r@   r   )axisrX   z0funm result may be inaccurate, approximate err =)r<   r   r   r7   r   ru   rH   rI   absre   slicer   rq   r
   r   rO   rE   rF   rG   maxr   r   r   r	   r   r   r   rZ   )r:   funcr[   TZr   r0   mindenpr.   jr   kslvaldenrN   errs                    r;   r(   r(   z  s   ~ 	A88DAq1Q<<DAq
'CAaTT$q'']]A	A3[[F 1QZZ * *q1Q 
	* 
	*AAA!A#ac'
a!AaCj1QqS1W:56A!A#,,Ca!Cj3qs7,,s1QqSW:aAaCj/I/IICCAAaC!G*q1QqSz)CczzGAac!A#gJC))FF
	* 	C1IIy1..//AAqAS//*17<8
9C}}
aS#f*d4!99Q&7&7788
9
9CE+hqkk**++333  c>>Dc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?   )	r4   rL   rH   rI   rE   r   rF   r   r   )rg   rxcs      r;   rounded_signzsignm.<locals>.rounded_sign  se    WQZZ8=CDa AACQAXb\\A%+,,,r=   r   )r[   r?   r@   F)
compute_uvr   d   r   z1signm result may be inaccurate, approximate err =)r<   r(   rE   rF   rG   rH   rI   r   r4   r   identityr7   re   r   r   r   r   rZ   )r:   r[   r   resultr]   r\   valsmax_svr   S0prev_errestr.   iS0Pps                 r;   r)   r)     s~   B 	A- - - !\222NFFDCG$$%5fl6G%HIF qU###DWT]]F 	F
A	
Qr{171:&&&	&BK3ZZ  "gg"s(^#b**R- c"RjjmA&&F??kV33E  	O6V#3#3EvNNN	6zr=   c                    t          j        |           } t          j        |          }| j        dk    r|j        dk    st          d          | j        d         |j        d         k    st          d          | 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`.

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

    r3   z(The both arrays should be 2-dimensional.r   z6The number of columns for both arrays should be equal..N)r_   )r4   r5   rm   r8   r7   newaxisreshape)r   br   s      r;   r+   r+   3  s    b 	
1A

1AFaKKAFaKKCDDD71:## , - - 	- 	
#qqq"*aaa
 1S"*aaa%:#;;A99UQWQRR[()))r=   )N)T)=	itertoolsr   numpyr4   r   r   r   r   r   r	   r
   r   r   r   r   r   r   numpy.lib.scimathr   ry   scipy.linalgr   r   _miscr   _basicr   r   _decomp_svdr   _decomp_schurr   r   _expm_frechetr   r   _matfuncs_sqrtmr   _matfuncs_expmr   r   __all__finforF   rE   rG   r<   rO   r*   r'   r    r   r!   r"   r#   r$   r%   r&   r(   r)   r+   rd   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 D D + + + + + + 0 / / / / / / /                     ) ) ) ) ) ) ) ) 2 2 2 2 2 2 2 2 " " " " " " = = = = = = = =& & & bhsmmrx}}CC   0   L+I +I +I\D D D DNk k k\  % % %P% % %P$3 $3 $3N$6 $6 $6N$6 $6 $6N$5 $5 $5Nf f f fRM M M M`=* =* =* =* =*r=   