L i0dZddlZddlZddlmZddgZeddddZd Z d Z d Z d Z d Z dZdZdZddZedddZy)z-Frechet derivative of the matrix exponential.N)_apply_over_batch expm_frechet expm_cond)A)Ercd|r+tj|}tj|}n*tj|}tj|}|jdk7s|jd|jdk7r t d|jdk7s|jd|jdk7r t d|j|jk7r t d|d}|dk(rt ||\}}n#|dk(rt||\}}nt d ||r||fS|S) a Frechet derivative of the matrix exponential of A in the direction E. Parameters ---------- A : (N, N) array_like Matrix of which to take the matrix exponential. E : (N, N) array_like Matrix direction in which to take the Frechet derivative. method : str, optional Choice of algorithm. Should be one of - `SPS` (default) - `blockEnlarge` compute_expm : bool, optional Whether to compute also `expm_A` in addition to `expm_frechet_AE`. Default is True. check_finite : bool, optional Whether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Returns ------- expm_A : ndarray Matrix exponential of A. expm_frechet_AE : ndarray Frechet derivative of the matrix exponential of A in the direction E. For ``compute_expm = False``, only `expm_frechet_AE` is returned. See Also -------- expm : Compute the exponential of a matrix. Notes ----- This section describes the available implementations that can be selected by the `method` parameter. The default method is *SPS*. Method *blockEnlarge* is a naive algorithm. Method *SPS* is Scaling-Pade-Squaring [1]_. It is a sophisticated implementation which should take only about 3/8 as much time as the naive implementation. The asymptotics are the same. .. versionadded:: 0.13.0 References ---------- .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2009) Computing the Frechet Derivative of the Matrix Exponential, with an application to Condition Number Estimation. SIAM Journal On Matrix Analysis and Applications., 30 (4). pp. 1639-1657. ISSN 1095-7162 Examples -------- >>> import numpy as np >>> from scipy import linalg >>> rng = np.random.default_rng() >>> A = rng.standard_normal((3, 3)) >>> E = rng.standard_normal((3, 3)) >>> expm_A, expm_frechet_AE = linalg.expm_frechet(A, E) >>> expm_A.shape, expm_frechet_AE.shape ((3, 3), (3, 3)) Create a 6x6 matrix containing [[A, E], [0, A]]: >>> M = np.zeros((6, 6)) >>> M[:3, :3] = A >>> M[:3, 3:] = E >>> M[3:, 3:] = A >>> expm_M = linalg.expm(M) >>> np.allclose(expm_A, expm_M[:3, :3]) True >>> np.allclose(expm_frechet_AE, expm_M[:3, 3:]) True rrz expected A to be a square matrixz expected E to be a square matrixz%expected A and E to be the same shapeSPS blockEnlargezUnknown implementation )npasarray_chkfiniteasarrayndimshape ValueErrorexpm_frechet_algo_64expm_frechet_block_enlarge)rrmethod compute_expm check_finiteexpm_Aexpm_frechet_AEs `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/linalg/_expm_frechet.pyrr sj   #   # JJqM JJqMvv{aggajAGGAJ.;<<vv{aggajAGGAJ.;<<ww!''@AA ~ "6q!"< > !"6"1"ab&> ))r)NgaьV>gX|[T7?g_vO?gQI?g?g+?guV?gGz?g{Gz?g\(\@g ףp= @g333333@g(\@gQ@g= ףp=@gGz@g(\!@gQ#@g333333%@gffffff'@cpd}|j|}tj||tj||z}|j|d|z|d|zz}|d|z|d|zz}|j|d|z|j|d|z|d|zzz}|d|z} |||| fS)N)g^@gN@g(@?r rrdotr ) rridentbA2M2UVLuLvs r _diff_pade3r3sA qB 1q! $B ad2g!U "#A !R!A$u*A qtBw!%%!R!A$u* 45 5B 1bB aR<rcPd}|j|}tj||tj||z}tj||}tj||tj||z}|j|d|z|d|zz|d|zz}|d|z|d|zz|d|zz} |j|d|z|d|zz|j|d|z|d|zz|d|zzz} |d|z|d|zz} || | | fS)N)g@g@g@@g@z@g>@r'r(r rrr)) rrr+r,r-r.A4M4r/r0r1r2s r _diff_pade5r9s4.A qB 1q! $B BB B"&&R. (B ad2g!R!A$u*,-A !R!A$r'AaDJ&A %%!R!A$r'! " EE!A$r'AaDG#ad5j0 1 2B 1b1Q47 B aR<rc0d}|j|}tj||tj||z}tj||}tj||tj||z}tj||}tj||tj||z} |j|d|z|d|zz|d|zz|d|zz} |d|z|d|zz|d|zz|d |zz} |j|d| z|d|zz|d|zz|j|d|z|d|zz|d|zz|d|zzz} |d| z|d|zz|d|zz} | | | | fS) N)g~pAg~`Ag@t>Ag@Ag@g@gL@r'r5r(r r6rrr))rrr+r,r-r.r7r8A6M6r/r0r1r2s r _diff_pade7r?sHA qB 1q! $B BB B"&&R. (B BB B"&&R. (B ad2g!R!A$r')AaDJ67A !R!A$r'AaDG#ad5j0A %%!R!A$r'!AaDG+ , EE!A$r'AaDG#ad2g-!U : ; A8M8r/r0r1r2s r _diff_pade9rEs /A qB 1q! $B BB B"&&R. (B BB B"&&R. (B BB B"&&R. (B ad2g!R!A$r')AaDG3ad5j@AA !R!A$r'AaDG#ad2g-!U :A %%!R!A$r'!AaDG+ad2g5 6 EE!A$r'AaDG#ad2g-!R7!A$u*D E FB 1b1Q47 QqT"W $qtBw .B aR<rc |jd}d}tj|}tjj |d}dt fdtfdtfdtff}|D]$\}}|t|ks||||\} } } } d}n|tdttjtj|tdz }|d| zz}|d| zz}tj||} tj||tj||z}tj| | }tj| |tj|| z}tj| |}tj||tj|| z}d }|d|z|d |zz|d| zz}|d|z|d|zz|d| zz|d|zz}|d |z|d |zz|d | zz}|d|z|d|zz|d| zz|d|zz}tj|||z}tj||} tj|||z} |d|z|d |zz|d|zz}|d|z|d|zz|d|zz}|d |z|d |zz|d |zz}|d|z|d|zz|d|zz}tj||tj||z|z}tj||tj||z} tj||tj||z|z} tjj!  z}tjj#|| | z}tjj#|  ztj| | z |z} t%|D]E}!tj|| tj| |z} tj||}G|| fS)Nrr r(r5r;rA g@)gD`lCgD`\Cg`=Hb;Cg eCgJXBg"5Bg/cBg\L8BgpķAgsyAgS-Ag@gf@r' rBr<r6r)rr identityr r!normr3r9r?rE ell_table_61maxintceillog2r* lu_factorlu_solverange)"rrr#sr+A_norm_1 m_pade_pairsmpader/r0r1r2r-r.r7r8r=r>r,W1W2Z1Z2WLw1Lw2Lz1Lz2Lwlu_pivRLks" rrrs  A A KKNE||  A&H         L  4 |A &1e,LAq"bA    y 3rwwrwwx,r2B'BCDE F aRK aRK VVAq\ VVAq\BFF1aL ( VVB^ VVB^bffRn , VVB^ VVB^bffRn , "rU2X"b 1Q47 * qT"WqtBw 1b (1Q4: 5 rU2X"b 1Q47 * qT"WqtBw 1b (1Q4: 5 FF2rNR  FF1aL FF2rNR eBh2r!AaDG+d2g!R!A$r')eBh2r!AaDG+d2g!R!A$r') VVB_rvvb"~ - 3 VVAr]RVVAq\ ) VVB_rvvb"~ - 3 \\ # #QBF +F fa!e,A fb2gR!0D&DEA 1X FF1aL266!Q< ' FF1aL a4Krc6|jjS)a Stack columns of M to construct a single vector. This is somewhat standard notation in linear algebra. Parameters ---------- M : 2-D array_like Input matrix Returns ------- v : 1-D ndarray Output vector )Travel)r$s rvecrks" 3399;rc 6|rtj|}ntj|}t|jdk7s|jd|jdk7r t d|jd}tj |}g}t|D]X}t|D]H}tj||||}t|||dd} |jt| JZtj|jS)a  Construct the Kronecker form of the Frechet derivative of expm. Parameters ---------- A : array_like with shape (N, N) Matrix to be expm'd. method : str, optional Extra keyword to be passed to expm_frechet. check_finite : bool, optional Whether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Returns ------- K : 2-D ndarray with shape (N*N, N*N) Kronecker form of the Frechet derivative of the matrix exponential. Notes ----- This function is used to help compute the condition number of the matrix exponential. See Also -------- expm : Compute a matrix exponential. expm_frechet : Compute the Frechet derivative of the matrix exponential. expm_cond : Compute the relative condition number of the matrix exponential in the Frobenius norm. rrr expected a square matrixF)rrr)r rrlenrrrKrTouterrappendrkrri) rrrr#r+colsijrFs rexpm_frechet_kronformru1sB   # JJqM 177|qAGGAJ!''!*4344  A KKNE D 1X q Aq58,AQ!EKA KKA    99T?  rc |rtj|}ntj|}t|jdk7s|jd|jdk7r t dt jj|}t|d}t jj|d}t jj|d}t jj|d}||z|z }|S)ax Relative condition number of the matrix exponential in the Frobenius norm. Parameters ---------- A : 2-D array_like Square input matrix with shape (N, N). check_finite : bool, optional Whether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Returns ------- kappa : float The relative condition number of the matrix exponential in the Frobenius norm See Also -------- expm : Compute the exponential of a matrix. expm_frechet : Compute the Frechet derivative of the matrix exponential. Notes ----- A faster estimate for the condition number in the 1-norm has been published but is not yet implemented in SciPy. .. versionadded:: 0.14.0 Examples -------- >>> import numpy as np >>> from scipy.linalg import expm_cond >>> A = np.array([[-0.3, 0.2, 0.6], [0.6, 0.3, -0.1], [-0.7, 1.2, 0.9]]) >>> k = expm_cond(A) >>> k 1.7787805864469866 rrr rmF)rfro) r rrrnrrr r!r"rurL)rrXKA_normX_normK_normkappas rrresT   # JJqM 177|qAGGAJ!''!*4344 !Aae4A \\  q% (F \\  q% (F \\  q! $F f_ &E Lr)NTT)NT)T)__doc__numpyr scipy.linalgr scipy._lib._utilr__all__rrrMr3r9r?rErrkrurrrrs3. ; '8X&k'k\ * >   &5p(1h8;;r