L i,|ddlZddlmZddlZddlmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZddlmZddlmZmZmZddlmZdd lmZmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%dd l&m'Z'ddl(m)Z)m*Z*ddl+m,Z,gdZ-ej\dj^Z/ej\dj^Z0dddddddZ1dZ2d'dZ3eddZ4edefdZ5dZ6dZ7eefdZ8eddZ9eddZ:eddZ;eddZ<edd Z=edd!Z>edd(d"Z?edefd#Z@ed$d%d&ZAy))N)product) dotdiagprod logical_notravel transpose conjugateabsoluteamaxsignisfinitetriu)_apply_over_batch)_NoValue) LinAlgError bandwidth LinAlgWarning)norm)solveinv)svd)schurrsf2csf) expm_frechet expm_cond)recursive_schur_sqrtm)pick_pade_structure pade_UV_calc) _funm_loops)expmcosmsinmtanmcoshmsinhmtanhmlogmfunmsignmsqrtmfractional_matrix_powerrr khatri_raodf)ilr0r/FDctj|}t|jdk7s|jd|jdk7r 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. rrz expected square array_like input)npasarraylenshape ValueErrorAs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/linalg/_matfuncs.py_asarray_squarer?'sI$ 1 A 177|qAGGAJ!''!*4;<< Hctj|rvtj|ra|1tdztdzdt |j j}tj|jd|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.Arr)atol) r7 isrealobj iscomplexobjfepseps_array_precisiondtypecharallcloseimagreal)r=Btols r> _maybe_realrR?sf4 ||A2??1- ;3h3s7+,>> 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.]]) rN)r?scipy.linalg._matfuncs_inv_ssqlinalg_matfuncs_inv_ssq_fractional_matrix_power)r=tscipys r>r-r-es/T A) << ) ) B B1a HHr@cb|turd}ntjdtdt j |}ddl}|jjj|}t||}dtz}t jdd 5tt||z d t j t|d |j j d z }ddd|r3t#r||k\r!d |}tj|t$d|S|fS#1swYBxYw)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) .. deprecated:: 1.16.0 The `disp` argument is deprecated and will be removed in SciPy 1.18.0. The previously returned error estimate can be computed as ``norm(expm(logm(A)) - A, 1) / norm(A, 1)``. 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.]]) TFThe `disp` argument is deprecated and will be removed in SciPy 1.18.0.r6 stacklevelrNignore)divideinvalidrrKz1logm result may be inaccurate, approximate err = )rwarningswarnDeprecationWarningr7r8rTrUrV_logmrRrIerrstaterr"rKrOrRuntimeWarning)r=disprYr3errtolerrestmessages r>r)r)sz x =(Q 8 1 A) &&,,Q/AAqA #XF Hh 7Ud1gai#bjja177&K&P&PQS&TTU 6V#3I&RG MM'>a @&yUUs AD%%D.c d tj|}|jdk(rG|jdkr8tjtj |j ggS|jdkr td|jd|jdk7r tdt|jdk(rKttjd|jj}tj||S|jdd d k(rtj |Stj|jtjs |j!tj"}n<|jtj$k(r|j!tj&}|jd}tj(|j|j}tj(d ||f|j}t+|jd dDcgc] }t-|c}D]\}||}t/|} t1| s?tj2tj tj2|||<^||dd d d d f<t5|\} } | dkrt7d | d t9|| } | dk7r#| dkrt7d| d t;d| d|d} | dk7rF| ddk(s | ddk(r tj2|}tj |d| zztj<d| d d tj2|| ddk(rdnd}t-| dz ddD]}| | z} tj |d| zztj<d| d d t?|d| zz|d| zzz}| ddk(r#|tj<d| dd d dfd d |tj<d| d ddd fd d nt-| D]}| | z} | ddk(s| ddk(r7| ddk(rtj@| ntjB| ||<X| ||<_|Scc}w)aCompute 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.]], [[1., 0.], [0., 1.]], [[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]]) rr60The input array must be at least two-dimensional-Last 2 dimensions of the array must be squarerrbNrrznscipy.linalg.expm could not allocate sufficient memory while trying to compute the Pade structure (error code z).izkscipy.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 )zii->i)kg@)"r7r8sizendimarrayexpitemrr:minr"eyerK empty_like issubdtypeinexactastypefloat64float16float32emptyrrangeranyrr MemoryErrorr RuntimeErroreinsum _exp_sinchrtril)r=arKneAAmxindawlumsinfoeAwdiag_awsdr1exp_sd_s r>r"r"s8F 1 Avv{qvvzxx"&&*+,--vvzLMMwwr{aggbk!IJJ AGG}RVVAQWW-.44}}Qe,, wwrs|vvvay =="** - HHRZZ  BJJ  HHRZZ   A !'' )B 1a)177 +B1773B<8aq89> sV r]2wggbffRWWR[12BsG  1a7 "2&1 E778c=> >B" 19s{!#99=b#BCC #$226q$:;;e 61 1 ''"+-/VVGa1"g4E-F '3'*WWRA!2;qsB+ EA)C24"r(8J1KBIIgs+A.'28(<=a1"gNF!uz>D '3qr3B3w<8;>D '3ssABw<8; Eq$A)C$ qEQJBqEQJ&(eqjbggclbggclBsGBsG}>@ IA9s5R-ctjtj|}tj|}|dk(}||xx||zcc<tj|dd|||<|S)NrDrp)r7diffrz)r lexp_diffl_diffmask_zs r>rrsiq "I WWQZF r\F vg&&/)q"vf~.If r@c|turd}ntjdtd|turtjdtdt j |}|j dk(rG|jdkr8t jt j|jggS|jdkr td|jd|jd k7r td t|jd k(rKtt jd|j j }t j"|| S|jd d dk(rtj$j'|St j(|j tj*s |j-tj.}n|j tj0k(r |j-tj2}no|j j4dvr |j-tj6}n7|j j4dvr|j-tj.}|j j4dvrt9d|j t;|\}}}}|d krtd||s|r#|rd} nd} tj| t<d|dur' t?||z|z ddzt?|dz } || fS|S#t@$rtjB} Y|| fSwxYw)a\ Compute, if exists, the matrix square root. The matrix square root of ``A`` is a matrix ``X`` such that ``X @ X = A``. Every square matrix is not guaranteed to have a matrix square root, for example, the array ``[[0, 1], [0, 0]]`` does not have a square root. Moreover, not every real matrix has a real square root. Hence, for real-valued matrices the return type can be complex if, numerically, there is an eigenvalue on the negative real axis. Parameters ---------- A : ndarray Input with last two dimensions are square ``(..., n, n)``. disp : bool, optional Print warning if error in the result is estimated large instead of returning estimated error. (Default: True) .. deprecated:: 1.16.0 The `disp` argument is deprecated and will be removed in SciPy 1.18.0. The previously returned error estimate can be computed as ``norm(X @ X - A, 'fro')**2 / norm(A, 'fro')`` blocksize : integer, optional .. deprecated:: 1.16.0 The `blocksize` argument is deprecated as it is unused by the algorithm and will be removed in SciPy 1.18.0. Returns ------- sqrtm : ndarray Computed matrix squareroot of `A` with same size ``(..., n, n)``. errest : float Frobenius norm of the estimated error, ||err||_F / ||A||_F. Only returned, if ``disp`` is set to ``False``. This return argument will be removed in version 1.20.0 and only the sqrtm result will be returned. .. deprecated:: 1.16.0 Notes ----- This function uses the Schur decomposition method to compute the matrix square root following [1]_ and for real matrices [2]_. Moreover, note that, there exist matrices that have square roots that are not polynomials in ``A``. For a classical example from [2]_, the matrix satisfies:: [ a, a**2 + 1]**2 [-1, 0] [-1, -a] = [ 0, -1] for any scalar ``a`` but it is not a polynomial in ``-I``. Thus, they will not be found by this function. 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. :doi:`10.1016/0024-3795(87)90118-2` .. [2] Nicholas J. Higham (1987) "Computing real square roots of a real matrix", Linear Algebra and its Applications, 88/89:405-430. :doi:`10.1016/0024-3795(87)90118-2` 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.]]) Tr[r6r\zKThe `blocksize` argument is deprecated and will be removed in SciPy 1.18.0.rrorprqrrrrbNrsGgfdFDz6scipy.linalg.sqrtm is not supported for the data type z&Internal error in scipy.linalg.sqrtm: z]Matrix is singular. The result might be inaccurate or the array might not have a square root.zdMatrix is ill-conditioned. The result might be inaccurate or the array might not have a square root.Ffro)"rrdrerfr7r8rwrxryrzr{rr:r|r,r}rKr~emathsqrtrrrrrrrL complex128 TypeErrorrrrr;inf) r=rj blocksizerrKresisIllconditioned isSingularrmsgarg2s r>r,r,s` x  (Q 8  &(Q 8 1 Avv{qvvzxx"&&*+,--vvzLMMwwr{aggbk!IJJ AGG}bffQagg./55}}Qe,, wwrs|vxx}}Q =="** - HHRZZ  BJJ  HHRZZ    HHR]] #   HHRZZ ww||6!GG9&' '/DA.F+C :t axB4&IJJ% :CAC c=Q7 u} c A u-q045>ADDy  66DDy s"L--M  M ct|}tj|r dtd|ztd|zzzStd|zjS)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?r7rGr"rOr<s r>r#r#,sMD A qDAJc!e,--BqDzr@ct|}tj|r dtd|ztd|zz zStd|zjS)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]]) yrr)r?r7rGr"rNr<s r>r$r$UsMD A qd2a4j4A;.//BqDzr@c ht|}t|tt|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?rRrr#r$r<s r>r%r%~s+H A q%Qa1 22r@cbt|}t|dt|t| zzS)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]]) rr?rRr"r<s r>r&r&0H A q#a48!34 55r@cbt|}t|dt|t| z zS)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]]) rrr<s r>r'r'rr@c ht|}t|tt|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?rRrr&r'r<s r>r(r(s+H A q%a%(3 44r@c t|}t|\}}t||\}}|j\}}t |t |}|j |j j}t|d}t||||\}}tt||tt|}t||}ttdt |j j}|dk(r|}t#dt%|||z t't)|ddz} t+t-t/t1|drt2j4} |r| d|zkDr t7d| |S|| fS) 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.]]) )rrrCrDrr)axisr^z0funm result may be inaccurate, approximate err =)r?rrr:rrrKrLabsr!rr r rRrHrIrJr|maxrrrrrrr7rprint) r=funcrjTZrr3mindenrQerrs r>r*r*sD@ A 8DAq 1a=DAq 77DAq T$q']A A 4\FAq!V,IAv C1Iy1./AAqAs ,QWW\\: ;C } aS3v:tDAJ'::; Dc J#v r@c|turd}ntjdtdt |}d}t ||d\}}dt zdtzd t|jj}||kr|St|d }tj|}d |z }||tj|jdzz} |} t!d D]N} t#| } d | | zz} d t%| | | zz} t't%| | | z d}||ks| |k(rn|} P|rt)|r||k\r t+d|| S| |fS)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) .. deprecated:: 1.16.0 The `disp` argument is deprecated and will be removed in SciPy 1.18.0. The previously returned error estimate can be computed as ``norm(signm @ signm - signm, 1)``. 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]) Tr[r6r\ctj|}|jjdk(rdtzt |z}ndt zt |z}tt||kD|zS)Nr0rB) r7rOrKrLrHr rIr r )rrxcs r> rounded_signzsignm..rounded_signs[ WWQZ 88==C Da ACQAXb\A%+,,r@r)rjrBrCF) compute_uvrdrz1signm result may be inaccurate, approximate err =)rrdrerfr?r*rHrIrJrKrLrr7r identityr:rrrrrr)r=rjrresultrlrkvalsmax_svrS0 prev_errestr1iS0Pps r>r+r+}sbN x =(Q 8 A-!\2NFFTc#g &'7 8I8I'J KF   qU #D WWT]F F A Qr{{1771:& & &BK 3Z"g "s(^ #b"+b. !c"bk"na( F?kV3   6V#3 Ev N 6zr@)rr6)br6cvtj|}tj|}|jdk(r|jdk(s td|jd|jdk(s td|j dk(s|j dk(rG|jd|jdz}|jd}tj |||fS|dddtjddf|dtjddddfz}|jd |jddzS) 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]]) r6z(The both arrays should be 2-dimensional.rz6The number of columns for both arrays should be equal.r)r:.N)rp) r7r8rxr;r:rwr~newaxisreshape)rrrrrs r>r.r.s \ 1 A 1 A FFaKAFFaKCDD 771: #,- - vv{affk GGAJ # GGAJ}}Qq!f-- #q"**a  1S"**a%:#;;A 99UQWWQR[( ))r@)N)T)Brd itertoolsrnumpyr7rrrrrr r r r r rrscipy._lib._utilrscipy._lib.deprecationr scipy.linalgrrr_miscr_basicrr _decomp_svdr _decomp_schurrr _expm_frechetrr_matfuncs_schur_sqrtmr_matfuncs_expmrr _linalg_pythranr!__all__finforIrHrJr?rRr-r)r"rr,r#r$r%r&r'r(r*r+r.rcr@r>rsDDDD/+?>)28=( &bhhsmrxx}C 0 L8+I+I\8QQhdNhOd8%%P8%%P8$3$3N8$6$6N8$6$6N8$5$5N8[[|8YYx8X&?*'?*r@