
    5[g(0                     h    d Z ddlZddlZddgZddZd Z	 dZd Z	d	 Z
d
 Zd Zd Zd ZddZddZy)z-Frechet derivative of the matrix exponential.    Nexpm_frechet	expm_condc                 d   |r+t        j                  |       } t        j                  |      }n*t        j                  |       } t        j                  |      }| j                  dk7  s| j                  d   | j                  d   k7  rt        d      |j                  dk7  s|j                  d   |j                  d   k7  rt        d      | j                  |j                  k7  rt        d      |d}|dk(  rt        | |      \  }}n#|dk(  rt        | |      \  }}nt        d	|z        |r||fS |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

       r      z expected A to be a square matrixz expected E to be a square matrixz%expected A and E to be the same shapeSPSblockEnlargezUnknown implementation %s)npasarray_chkfiniteasarrayndimshape
ValueErrorexpm_frechet_algo_64expm_frechet_block_enlarge)AEmethodcompute_expmcheck_finiteexpm_Aexpm_frechet_AEs          U/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/linalg/_expm_frechet.pyr   r      s   h   #  #JJqMJJqMvv{aggajAGGAJ.;<<vv{aggajAGGAJ.;<<ww!''@AA~"6q!"<	>	!"<Q"B4v=>>&&    c                 2   | j                   d   }t        j                  t        j                  | |g      t        j                  t        j                  |       | g      g      }t
        j                  j                  |      }|d|d|f   |d||df   fS )zh
    This is a helper function, mostly for testing and profiling.
    Return expm(A), frechet(A, E)
    r   N)r   r
   vstackhstack
zeros_likescipylinalgexpm)r   r   nMexpm_Ms        r   r   r   v   s    
 	

A
		
		1a&
		2==#Q'(* 	+A \\q!F"1"bqb&>6"1"ab&>))r   )NgaьV>gX|[T7?g_vO?gQI?g?g+?guV?gGz?g{Gz?g\(\@g
ףp=
	@g333333@g(\@gQ@g=
ףp=@gGz@g(\!@gQ#@g333333%@gffffff'@c                 p   d}| j                  |       }t        j                   | |      t        j                   ||       z   }| j                  |d   |z  |d   |z  z         }|d   |z  |d   |z  z   }| j                  |d   |z        |j                  |d   |z  |d   |z  z         z   }|d   |z  }	||||	fS )N)g      ^@g      N@g      (@      ?   r   r   r   dotr
   )
r   r   identbA2M2UVLuLvs
             r   _diff_pade3r2      s    A	
qB	1q!	$B	ad2g!U
"#A	!R!A$u*A	
qtBw!%%!R!A$u* 45	5B	
1bBaR<r   c                 P   d}| j                  |       }t        j                   | |      t        j                   ||       z   }t        j                   ||      }t        j                   ||      t        j                   ||      z   }| j                  |d   |z  |d   |z  z   |d   |z  z         }|d   |z  |d   |z  z   |d   |z  z   }	| j                  |d   |z  |d   |z  z         |j                  |d   |z  |d   |z  z   |d   |z  z         z   }
|d   |z  |d   |z  z   }||	|
|fS )N)g     @g     @g     @@g     @z@g      >@r&      r'   r      r   r   r(   )r   r   r*   r+   r,   r-   A4M4r.   r/   r0   r1   s               r   _diff_pade5r8      s4   .A	
qB	1q!	$B	BB	B"&&R.	(B	ad2g!R!A$u*,-A	!R!A$r'AaDJ&A
%%!R!A$r'!
"EE!A$r'AaDG#ad5j012B	
1b1Q47	BaR<r   c                 0   d}| j                  |       }t        j                   | |      t        j                   ||       z   }t        j                   ||      }t        j                   ||      t        j                   ||      z   }t        j                   ||      }t        j                   ||      t        j                   ||      z   }	| j                  |d   |z  |d   |z  z   |d   |z  z   |d   |z  z         }
|d   |z  |d   |z  z   |d   |z  z   |d	   |z  z   }| j                  |d   |	z  |d   |z  z   |d   |z  z         |j                  |d   |z  |d   |z  z   |d   |z  z   |d   |z  z         z   }|d   |	z  |d   |z  z   |d   |z  z   }|
|||fS )
N)g    ~pAg    ~`Ag    @t>Ag    @Ag     @g     @g      L@r&      r4   r'   r      r5   r   r   r(   )r   r   r*   r+   r,   r-   r6   r7   A6M6r.   r/   r0   r1   s                 r   _diff_pade7r>      s   HA	
qB	1q!	$B	BB	B"&&R.	(B	BB	B"&&R.	(B	ad2g!R!A$r')AaDJ67A	!R!A$r'AaDG#ad5j0A
%%!R!A$r'!AaDG+
,EE!A$r'AaDG#ad2g-!U
:;<B	
1b1Q47	QqT"W	$BaR<r   c                    d}| j                  |       }t        j                   | |      t        j                   ||       z   }t        j                   ||      }t        j                   ||      t        j                   ||      z   }t        j                   ||      }t        j                   ||      t        j                   ||      z   }	t        j                   ||      }
t        j                   ||      t        j                   ||      z   }| j                  |d   |
z  |d   |z  z   |d   |z  z   |d   |z  z   |d   |z  z         }|d   |
z  |d   |z  z   |d	   |z  z   |d
   |z  z   |d   |z  z   }| j                  |d   |z  |d   |	z  z   |d   |z  z   |d   |z  z         |j                  |d   |
z  |d   |z  z   |d   |z  z   |d   |z  z   |d   |z  z         z   }|d   |z  |d   |	z  z   |d	   |z  z   |d
   |z  z   }||||fS )N)
g   ynBg   yn Bg    Ag   @
Ag    2|Ag    ~@Ag     @g     @g     V@r&   	   r:   r4   r'   r      r;   r5   r   r   r(   )r   r   r*   r+   r,   r-   r6   r7   r<   r=   A8M8r.   r/   r0   r1   s                   r   _diff_pade9rD      s   	/A	
qB	1q!	$B	BB	B"&&R.	(B	BB	B"&&R.	(B	BB	B"&&R.	(B	ad2g!R!A$r')AaDG3ad5j@AA	!R!A$r'AaDG#ad2g-!U
:A
%%!R!A$r'!AaDG+ad2g5
6EE!A$r'AaDG#ad2g-!R7!A$u*DEFB	
1b1Q47	QqT"W	$qtBw	.BaR<r   c                    | j                   d   }d }t        j                  |      }t        j                  j                  | d      }dt        fdt        fdt        fdt        ff}|D ]$  \  }}|t        |   k  s || ||      \  }	}
}}d} n |t        dt        t        j                  t        j                  |t        d   z                          }| d| z  z  } |d| z  z  }t        j                  | |       }t        j                  | |      t        j                  ||       z   }t        j                  ||      }t        j                  ||      t        j                  ||      z   }t        j                  ||      }t        j                  ||      t        j                  ||      z   }d	}|d   |z  |d
   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   |d   |z  z   }t        j                  ||      |z   }t        j                  | |      }	t        j                  ||      |z   }
|d   |z  |d
   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   }|d   |z  |d   |z  z   |d   |z  z   }t        j                  ||      t        j                  ||      z   |z   }t        j                  | |      t        j                  ||      z   }t        j                  ||      t        j                  ||      z   |z   }t        j                  j!                  	 
z         }t        j                  j#                  ||	|
z         }t        j                  j#                  |z   t        j                  ||z
  |      z         } t%        |      D ]E  }!t        j                  ||       t        j                  | |      z   } t        j                  ||      }G || fS )Nr   r   r'   r4   r:   r@      g       @)g D`lCg D`\Cg `=Hb;Cg 	eCg JXBg  "5Bg  /cBg   \L8Bg   pķAg    syAg    S-Ag     @g     f@r&         
   rA   r;   r5   r   )r   r
   identityr   r    normr2   r8   r>   rD   ell_table_61maxintceillog2r)   	lu_factorlu_solverange)"r   r   r"   sr*   A_norm_1m_pade_pairsmpader.   r/   r0   r1   r,   r-   r6   r7   r<   r=   r+   W1W2Z1Z2WLw1Lw2Lz1Lz2Lwlu_pivRLks"                                     r   r   r      s   	
AAKKNE||  A&H	L
  4|A&1e,LAq"bA	  
 	y3rwwrwwx,r2B'BCDEFaRKaRKVVAq\VVAq\BFF1aL(VVB^VVB^bffRn,VVB^VVB^bffRn," rU2X"b 1Q47*qT"WqtBw1b(1Q4:5rU2X"b 1Q47*qT"WqtBw1b(1Q4:5FF2rNRFF1aLFF2rNReBh2r!AaDG+d2g!R!A$r')eBh2r!AaDG+d2g!R!A$r')VVB_rvvb"~-3VVAr]RVVAq\)VVB_rvvb"~-3\\##QBF+Ffa!e,Afb2gR!0D&DEA1XFF1aL266!Q<'FF1aL  a4Kr   c                 6    | j                   j                         S )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

    )Travel)r#   s    r   vecrj     s    " 3399;r   c           	      6   |rt        j                  |       } nt        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt        d      | j                  d   }t        j                  |      }g }t        |      D ]X  }t        |      D ]H  }t        j                  ||   ||         }t        | ||dd      }	|j                  t        |	             J Z t        j                  |      j                  S )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.

    r   r   r   expected a square matrixF)r   r   r   )r
   r   r   lenr   r   rJ   rS   outerr   appendrj   r   rh   )
r   r   r   r"   r*   colsijr   Fs
             r   expm_frechet_kronformrt   .  s    B   #JJqM
177|qAGGAJ!''!*4344	
AKKNED1XqAq58,AQ!EKAKKA	   99T?r   c                    |rt        j                  |       } nt        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt        d      t        j                  j                  |       }t        | d      }t        j                  j                  | d      }t        j                  j                  |d      }t        j                  j                  |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

    r   r   r   rl   F)r   fro)r
   r   r   rm   r   r   r   r    r!   rt   rK   )r   r   XKA_normX_normK_normkappas           r   r   r   b  s    R   #JJqM
177|qAGGAJ!''!*4344!Aae4A
 \\q%(F\\q%(F\\q!$Ff_&ELr   )NTT)NT)T)__doc__numpyr
   scipy.linalgr   __all__r   r   rL   r2   r8   r>   rD   r   rj   rt   r    r   r   <module>r      sZ    3  ;
'k\
*
> &5p(1h;r   