
    (phr                     4   S r / SQrSSKrSSKJrJr  SSKJr  SSK	J
r
  SSKJrJr  SSKrSSK	rSSKJr  SS	KJr  S
SKJrJr  SrS rS rS rS!S jr " S S\5      r " S S\5      r S"S jr S"S jr  " S S5      r!S r"S r#S#S jr$S r%S r&S r'S r(S  r)g)$z
Sparse matrix functions
)expminvmatrix_power    N)solvesolve_triangular)issparse)spsolve)is_pydata_spmatrix	isintlike)LinearOperator)	eye_array   )_ident_like_exact_1_normupper_triangularc                     [        U 5      (       d  [        U 5      (       d  [        S5      e[        U 5      n[	        X5      nU$ )aO  
Compute the inverse of a sparse arrays

Parameters
----------
A : (M, M) sparse arrays
    square matrix to be inverted

Returns
-------
Ainv : (M, M) sparse arrays
    inverse of `A`

Notes
-----
This computes the sparse inverse of `A`. If the inverse of `A` is expected
to be non-sparse, it will likely be faster to convert `A` to dense and use
`scipy.linalg.inv`.

Examples
--------
>>> from scipy.sparse import csc_array
>>> from scipy.sparse.linalg import inv
>>> A = csc_array([[1., 0.], [1., 2.]])
>>> Ainv = inv(A)
>>> Ainv
<Compressed Sparse Column sparse array of dtype 'float64'
    with 3 stored elements and shape (2, 2)>
>>> A.dot(Ainv)
<Compressed Sparse Column sparse array of dtype 'float64'
    with 2 stored elements and shape (2, 2)>
>>> A.dot(Ainv).toarray()
array([[ 1.,  0.],
       [ 0.,  1.]])

.. versionadded:: 0.12.0

zInput must be a sparse arrays)r   r
   	TypeErrorr   r	   )AIAinvs      P/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_matfuncs.pyr   r      s>    P QKK-a00788 	AA1=DK    c                    [        U5      U:w  d  US:  a  [        S5      e[        U5      n[        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [        S5      e[        R
                  " U R                  S   S4[        S9nU R                  n[        U5       H  nUR                  U5      nM     [        R                  " U5      $ )aV  
Compute the 1-norm of a non-negative integer power of a non-negative matrix.

Parameters
----------
A : a square ndarray or matrix or sparse arrays
    Input matrix with non-negative entries.
p : non-negative integer
    The power to which the matrix is to be raised.

Returns
-------
out : float
    The 1-norm of the matrix power p of A.

r   zexpected non-negative integer p   r   %expected A to be like a square matrixdtype)int
ValueErrorlenshapenponesfloatTrangedotmax)r   pvMis        r   _onenorm_matrix_power_nnmr-   P   s    $ 1v{a!e:;;AA
177|qAGGAJ!''!*4@AA 	Qu-A	A1XEE!H 66!9r   c                 r   [        U 5      (       aI  [        R                  R                  U S5      nUR                  S:H  =(       d    UR                  5       S:H  $ [        U 5      (       a%  SS KnUR                  U S5      nUR                  S:H  $ [        R                  " U S5      R                  5       (       + $ )Nr   )	r   scipysparsetrilnnzcount_nonzeror
   r"   any)r   
lower_partr1   s      r   _is_upper_triangularr7   q   s    {{\\&&q"-
 ~~"Ej&>&>&@A&EE	A		[[B'
~~""771b>%%'''r   c                    [        U R                  5      S:w  a  [        S5      e[        UR                  5      S:w  a  [        S5      eSnU[        :X  ac  [	        U 5      (       dS  [	        U5      (       dC  [        U 5      (       d3  [        U5      (       d#  [        R                  R                  SX45      u  nUb  Uc  SnU" X U5      nU$ Uc  U R                  U5      nU$ X R                  U5      -  nU$ )a  
A matrix product that knows about sparse and structured matrices.

Parameters
----------
A : 2d ndarray
    First matrix.
B : 2d ndarray
    Second matrix.
alpha : float
    The matrix product will be scaled by this constant.
structure : str, optional
    A string describing the structure of both matrices `A` and `B`.
    Only `upper_triangular` is currently supported.

Returns
-------
M : 2d ndarray
    Matrix product of A and B.

r   z%expected A to be a rectangular matrixz%expected B to be a rectangular matrixN)trmm      ?)
r    r!   r   UPPER_TRIANGULARr   r
   r0   linalgget_blas_funcsr'   )r   Balpha	structurefouts         r   _smart_matrix_productrC      s    , 177|q@AA
177|q@AAA$$HQKK*1--6H6K6K,,Y?BA}=E!n J	 =%%(C J %%("CJr   c                   @    \ rS rSrS	S jrS rS rS r\S 5       r	Sr
g)
MatrixPowerOperator   Nc                 &   UR                   S:w  d   UR                  S   UR                  S   :w  a  [        S5      eUS:  a  [        S5      eXl        X l        X0l        UR                  U l        UR                   U l         UR                  U l        g )Nr   r   r   r   z'expected p to be a non-negative integer)ndimr!   r   _A_p
_structurer   )selfr   r)   r@   s       r   __init__MatrixPowerOperator.__init__   ss    66Q;!''!*
2DEEq5FGG#WW
FF	WW
r   c                 t    [        U R                  5       H  nU R                  R                  U5      nM      U$ N)r&   rJ   rI   r'   )rL   xr,   s      r   _matvecMatrixPowerOperator._matvec   s*    twwAAA  r   c                     U R                   R                  nUR                  5       n[        U R                  5       H  nUR                  U5      nM     U$ rP   )rI   r%   ravelr&   rJ   r'   )rL   rQ   A_Tr,   s       r   _rmatvecMatrixPowerOperator._rmatvec   s>    ggiiGGItwwA
A  r   c                 z    [        U R                  5       H!  n[        U R                  XR                  S9nM#     U$ Nr@   )r&   rJ   rC   rI   rK   )rL   Xr,   s      r   _matmatMatrixPowerOperator._matmat   s-    twwA%dggqOOLA  r   c                 V    [        U R                  R                  U R                  5      $ rP   )rE   rI   r%   rJ   rL   s    r   r%   MatrixPowerOperator.T   s    "47799dgg66r   )rI   rJ   rK   r   rH   r!   rP   )__name__
__module____qualname____firstlineno__rM   rR   rW   r]   propertyr%   __static_attributes__ r   r   rE   rE      s*    


 7 7r   rE   c                   @    \ rS rSrSrS rS rS rS r\	S 5       r
Srg	)
ProductOperator   zC
For now, this is limited to products of multiple square matrices.
c                    UR                  SS 5      U l        U HG  n[        UR                  5      S:w  d"  UR                  S   UR                  S   :w  d  M>  [	        S5      e   U(       a`  US   R                  S   nU H&  nUR                   H  nXT:w  d  M
  [	        S5      e   M(     XD4U l        [        U R                  5      U l        [        R                  " U Vs/ s H  ofR                  PM     sn6 U l        Xl	        g s  snf )Nr@   r   r   r   zbFor now, the ProductOperator implementation is limited to the product of multiple square matrices.zHThe square matrices of the ProductOperator must all have the same shape.)
getrK   r    r!   r   rH   r"   result_typer   _operator_sequence)rL   argskwargsr   ndrQ   s          r   rM   ProductOperator.__init__   s     **[$7A177|q AGGAJ!''!*$< NO O 
 Qa AAv(!@A A !  DJDJJDI^^t%<t!ggt%<=
"& &=s    Dc                 `    [        U R                  5       H  nUR                  U5      nM     U$ rP   )reversedro   r'   rL   rQ   r   s      r   rR   ProductOperator._matvec   s)    $112AaA 3r   c                     UR                  5       nU R                   H  nUR                  R                  U5      nM      U$ rP   )rU   ro   r%   r'   rw   s      r   rW   ProductOperator._rmatvec   s3    GGI((A
A )r   c                 f    [        U R                  5       H  n[        X!U R                  S9nM     U$ rZ   )rv   ro   rC   rK   )rL   r\   r   s      r   r]   ProductOperator._matmat   s,    $112A%adooFA 3r   c                 x    [        U R                  5       Vs/ s H  oR                  PM     nn[        U6 $ s  snf rP   )rv   ro   r%   rj   )rL   r   T_argss      r   r%   ProductOperator.T   s6    '(?(?@A@!##@A'' Bs   7)ro   rK   r   rH   r!   N)rb   rc   rd   re   __doc__rM   rR   rW   r]   rf   r%   rg   rh   r   r   rj   rj      s/    '(

 ( (r   rj   c                 d    [         R                  R                  R                  [	        XUS95      $ )aG  
Efficiently estimate the 1-norm of A^p.

Parameters
----------
A : ndarray
    Matrix whose 1-norm of a power is to be computed.
p : int
    Non-negative integer power.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
    Larger values take longer and use more memory
    but give more accurate output.
itmax : int, optional
    Use at most this many iterations.
compute_v : bool, optional
    Request a norm-maximizing linear operator input vector if True.
compute_w : bool, optional
    Request a norm-maximizing linear operator output vector if True.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse arrays.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.

r[   )r0   r1   r<   
onenormestrE   )r   r)   titmax	compute_v	compute_wr@   s          r   _onenormest_matrix_powerr      s,    J <<))	:< <r   c                 f    [         R                  R                  R                  [	        U SU065      $ )a  
Efficiently estimate the 1-norm of the matrix product of the args.

Parameters
----------
operator_seq : linear operator sequence
    Matrices whose 1-norm of product is to be computed.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
    Larger values take longer and use more memory
    but give more accurate output.
itmax : int, optional
    Use at most this many iterations.
compute_v : bool, optional
    Request a norm-maximizing linear operator input vector if True.
compute_w : bool, optional
    Request a norm-maximizing linear operator output vector if True.
structure : str, optional
    A string describing the structure of all operators.
    Only `upper_triangular` is currently supported.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse arrays.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.

r@   )r0   r1   r<   r   rj   )operator_seqr   r   r   r   r@   s         r   _onenormest_productr   '  s1    L <<))\?Y?A Ar   c                      \ rS rSrSrSS jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rS rS rS rS rSrg)_ExpmPadeHelperiQ  z
Help lazily evaluate a matrix exponential.

The idea is to not do more work than we need for high expm precision,
so we lazily compute matrix powers and store or precompute
other properties of the matrix.

Nc                     Xl         SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        [        U5      U l        X l        X0l        g)a  
Initialize the object.

Parameters
----------
A : a dense or sparse square numpy matrix or ndarray
    The matrix to be exponentiated.
structure : str, optional
    A string describing the structure of matrix `A`.
    Only `upper_triangular` is currently supported.
use_exact_onenorm : bool, optional
    If True then only the exact one-norm of matrix powers and products
    will be used. Otherwise, the one-norm of powers and products
    may initially be estimated.
N)r   _A2_A4_A6_A8_A10	_d4_exact	_d6_exact	_d8_exact
_d10_exact
_d4_approx
_d6_approx
_d8_approx_d10_approxr   identr@   use_exact_onenorm)rL   r   r@   r   s       r   rM   _ExpmPadeHelper.__init__[  s{      	 ^
"!2r   c                     U R                   c.  [        U R                  U R                  U R                  S9U l         U R                   $ rZ   )r   rC   r   r@   r`   s    r   A2_ExpmPadeHelper.A2}  s4    88,FFDFFdnn>DHxxr   c                     U R                   c.  [        U R                  U R                  U R                  S9U l         U R                   $ rZ   )r   rC   r   r@   r`   s    r   A4_ExpmPadeHelper.A4  5    88,GGTWW@DHxxr   c                     U R                   c.  [        U R                  U R                  U R                  S9U l         U R                   $ rZ   )r   rC   r   r   r@   r`   s    r   A6_ExpmPadeHelper.A6  r   r   c                     U R                   c.  [        U R                  U R                  U R                  S9U l         U R                   $ rZ   )r   rC   r   r   r@   r`   s    r   A8_ExpmPadeHelper.A8  r   r   c                     U R                   c.  [        U R                  U R                  U R                  S9U l         U R                   $ rZ   )r   rC   r   r   r@   r`   s    r   A10_ExpmPadeHelper.A10  s6    99-GGTWW@DIyyr   c                 n    U R                   c  [        U R                  5      S-  U l         U R                   $ )N      ?)r   _onenormr   r`   s    r   d4_tight_ExpmPadeHelper.d4_tight  +    >>!%dgg.6DN~~r   c                 n    U R                   c  [        U R                  5      S-  U l         U R                   $ )NUUUUUU?)r   r   r   r`   s    r   d6_tight_ExpmPadeHelper.d6_tight  r   r   c                 n    U R                   c  [        U R                  5      S-  U l         U R                   $ )N      ?)r   r   r   r`   s    r   d8_tight_ExpmPadeHelper.d8_tight  r   r   c                 n    U R                   c  [        U R                  5      S-  U l         U R                   $ )N皙?)r   r   r   r`   s    r   	d10_tight_ExpmPadeHelper.d10_tight  s+    ??"&txx059DOr   c                     U R                   (       a  U R                  $ U R                  b  U R                  $ U R                  c'  [	        U R
                  SU R                  S9S-  U l        U R                  $ )Nr   r[   r   )r   r   r   r   r   r   r@   r`   s    r   d4_loose_ExpmPadeHelper.d4_loose  `    !!== >>%>>!&":477A"&..#248#:??"r   c                     U R                   (       a  U R                  $ U R                  b  U R                  $ U R                  c'  [	        U R
                  SU R                  S9S-  U l        U R                  $ )N   r[   r   )r   r   r   r   r   r   r@   r`   s    r   d6_loose_ExpmPadeHelper.d6_loose  r   r   c                     U R                   (       a  U R                  $ U R                  b  U R                  $ U R                  c'  [	        U R
                  SU R                  S9S-  U l        U R                  $ )Nr   r[   r   )r   r   r   r   r   r   r@   r`   s    r   d8_loose_ExpmPadeHelper.d8_loose  r   r   c                    U R                   (       a  U R                  $ U R                  b  U R                  $ U R                  c2  [	        U R
                  U R                  4U R                  S9S-  U l        U R                  $ )Nr[   r   )r   r   r   r   r   r   r   r@   r`   s    r   	d10_loose_ExpmPadeHelper.d10_loose  sl    !!>>!??&??"'#67I"&..$249$; ###r   c                     Sn[        U R                  US   U R                  -  US   U R                  -  -   U R                  S9nUS   U R                  -  US   U R                  -  -   nX#4$ )N)g      ^@g      N@g      (@r:   r   r   r[   r   r   )rC   r   r   r   r@   rL   bUVs       r   pade3_ExpmPadeHelper.pade3  si     !$&&!TWWqtDJJ...* aDL1Q4

?*tr   c                 &   Sn[        U R                  US   U R                  -  US   U R                  -  -   US   U R                  -  -   U R
                  S9nUS   U R                  -  US   U R                  -  -   US   U R                  -  -   nX#4$ )	N)g     @g     @g     @@g     @z@g      >@r:      r   r   r[      r   r   )rC   r   r   r   r   r@   r   s       r   pade5_ExpmPadeHelper.pade5  s    2!$&&!TWWqtDGG|+ad4::o=..* aDL1Q4<'!A$tzz/9tr   c                 r   Sn[        U R                  US   U R                  -  US   U R                  -  -   US   U R                  -  -   US   U R
                  -  -   U R                  S9nUS   U R                  -  US   U R                  -  -   US	   U R                  -  -   US
   U R
                  -  -   nX#4$ )N)g    ~pAg    ~`Ag    @t>Ag    @Ag     @g     @g      L@r:      r   r   r   r[      r   r   r   )rC   r   r   r   r   r   r@   r   s       r   pade7_ExpmPadeHelper.pade7  s    L!$&&!TWWqtDGG|+ad477l:QqT$**_L..* aDL1Q4<'!A$tww,61djjHtr   c                    Sn[        U R                  US   U R                  -  US   U R                  -  -   US   U R                  -  -   US   U R
                  -  -   US   U R                  -  -   U R                  S9nUS   U R                  -  US	   U R                  -  -   US
   U R                  -  -   US   U R
                  -  -   US   U R                  -  -   nX#4$ )N)
g   ynBg   yn Bg    Ag   @
Ag    2|Ag    ~@Ag     @g     @g     V@r:   	   r   r   r   r   r[      r   r   r   r   )rC   r   r   r   r   r   r   r@   r   s       r   pade9_ExpmPadeHelper.pade9   s    3!$&&1dgg!TWW,qtDGG|;aDL!#$Q4

?3..* qT$''\AaDL(1Q4<7!TWW tDJJ/tr   c                 F   SnU R                   SU* -  -  nU R                  SSU-  -  -  nU R                  SSU-  -  -  nU R                  SSU-  -  -  n[	        UUS   U-  US   U-  -   US   U-  -   U R
                  S	9n[	        UXrS
   U-  -   US   U-  -   US   U-  -   US   U R                  -  -   U R
                  S	9n[	        UUS   U-  US   U-  -   US   U-  -   U R
                  S	9n	XS   U-  -   US   U-  -   US   U-  -   US   U R                  -  -   n
X4$ )N)g D`lCg D`\Cg `=Hb;Cg 	eCg JXBg  "5Bg  /cBg   \L8Bg   pķAg    syAg    S-Ag     @g     f@r:   r   i      r   r[   r   r   r   r      
   r   r   r   r   )r   r   r   r   rC   r@   r   )rL   sr   r>   B2B4B6U2r   V2r   s              r   pade13_scaled_ExpmPadeHelper.pade13_scaled  sh   " FFQUNWWq2a4y WWq2a4y WWq2a4y "2"b1R58#ad2g-..* "!d2g!R'aDGd4::o...* #2"b1R58#ad2g-..* 1bL1Q47"QqT"W,qtDJJ>tr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r@   r   )NF)rb   rc   rd   re   r   rM   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rg   rh   r   r   r   r   Q  s*    3D            
  
  
  
 	# 	# 	# 	# 	# 	# 	$ 	$	r   r   c                     [        U SS9$ )az  
Compute the matrix exponential using Pade approximation.

Parameters
----------
A : (M,M) array_like or sparse array
    2D Array or Matrix (sparse or dense) to be exponentiated

Returns
-------
expA : (M,M) ndarray
    Matrix exponential of `A`

Notes
-----
This is algorithm (6.1) which is a simplification of algorithm (5.1).

.. versionadded:: 0.12.0

References
----------
.. [1] Awad H. Al-Mohy and Nicholas J. Higham (2009)
       "A New Scaling and Squaring Algorithm for the Matrix Exponential."
       SIAM Journal on Matrix Analysis and Applications.
       31 (3). pp. 970-989. ISSN 1095-7162

Examples
--------
>>> from scipy.sparse import csc_array
>>> from scipy.sparse.linalg import expm
>>> A = csc_array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
>>> A.toarray()
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]], dtype=int64)
>>> Aexp = expm(A)
>>> Aexp
<Compressed Sparse Column sparse array of dtype 'float64'
    with 3 stored elements and shape (3, 3)>
>>> Aexp.toarray()
array([[  2.71828183,   0.        ,   0.        ],
       [  0.        ,   7.3890561 ,   0.        ],
       [  0.        ,   0.        ,  20.08553692]])
auto)r   )_expm)r   s    r   r   r   "  s    Z f--r   c           
         [        U [        [        [        R                  45      (       a  [        R
                  " U 5      n [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [        S5      eU R                  S:X  aT  [        R                  " SS/U R                  S9n[        U 5      (       d  [        U 5      (       a  U R                  U5      $ U$ U R                  S:X  ab  [        R                  " U S   5      //n[        U 5      (       d  [        U 5      (       a  U R                  U5      $ [        R                  " U5      $ [        U [        R                   5      (       d   [        U 5      (       d  [        U 5      (       aI  [        R"                  " U R                  [        R$                  5      (       d  U R'                  [(        5      n [+        U 5      (       a  [,        OS nUS:X  a  U R                  S   S	:  n[/        XUS
9n[1        UR2                  UR4                  5      nUS:  a6  [7        UR8                  S5      S:X  a  UR;                  5       u  pg[=        XgUS9$ [1        UR>                  UR4                  5      nUS:  a6  [7        UR8                  S5      S:X  a  URA                  5       u  pg[=        XgUS9$ [1        URB                  URD                  5      n	U	S:  a6  [7        UR8                  S5      S:X  a  URG                  5       u  pg[=        XgUS9$ U	S:  a6  [7        UR8                  S5      S:X  a  URI                  5       u  pg[=        XgUS9$ [1        URD                  URJ                  5      n
[M        X5      nSnUS:X  a  SnO?[1        [O        [        RP                  " [        RR                  " X-  5      5      5      S5      nU[7        SU* -  UR8                  -  S5      -   nURU                  U5      u  pg[=        XgUS9nU[,        :X  a  [W        XR8                  U5      nU$ [Y        U5       H  nUR[                  U5      nM     U$ )Nr   r   r   zexpected a square matrix)r   r   r   )r   r   r      )r@   r   g ,?r   r[   g|zی@?r   gQi?r   gd @r   g      @r   ).
isinstancelisttupler"   matrixasarrayr    r!   r   zerosr   r   r
   	__class__exparrayndarray
issubdtypeinexactastyper$   r7   r;   r   r(   r   r   _ellr   r   
_solve_P_Qr   r   r   r   r   r   r   minr   ceillog2r   _fragment_2_1r&   r'   )r   r   rB   r@   heta_1r   r   eta_2eta_3eta_4eta_5theta_13r   r\   r,   s                   r   r   r   R  sx   
 !dE299-..JJqM
177|qAGGAJ!''!*4344 	ww&hh1vQWW-A;;,Q//;;s##
 	ww&qw ! A;;,Q//;;s##xx} 
Arzz	"	"hqkk5G5J5JMM!''2::66HHUO %9$;$; IF"GGAJ, 	6G	IA 

AJJ'E%%$qssA,!*;wwy!)44 

AJJ'E%%$qssA,!*;wwy!)44 

AJJ'E%%$qssA,!*;wwy!)44%%$qssA,!*;wwy!)44 

AKK(EEH zBGGBGGE$4567;	DQBb!!A??1DA19-A$$!SS!$
 H qAaA Hr   c                     X-   nU * U-   n[        U 5      (       d  [        U 5      (       a  [        XC5      $ Uc  [        XC5      $ U[        :X  a  [        XC5      $ [        S[        U5      -   5      e)aq  
A helper function for expm_2009.

Parameters
----------
U : ndarray
    Pade numerator.
V : ndarray
    Pade denominator.
structure : str, optional
    A string describing the structure of both matrices `U` and `V`.
    Only `upper_triangular` is currently supported.

Notes
-----
The `structure` argument is inspired by similar args
for theano and cvxopt functions.

zunsupported matrix structure: )r   r
   r	   r   r;   r   r   str)r   r   r@   PQs        r   r  r    sm    ( 	
A	
QA{{(++q}		Q{	&	&%%9C	NJKKr   c                     [        U5      S:  a5  X-  n[        R                  " U 5      SUS-  SUS-  SUS-  -   -  -   -  -   -  $ [        R                  " X-   5      [        R                  " X-
  5      -
  SU-  -  $ )a  
Stably evaluate exp(a)*sinh(x)/x

Notes
-----
The strategy of falling back to a sixth order Taylor expansion
was suggested by the Spallation Neutron Source docs
which was found on the internet by google search.
http://www.ornl.gov/~t6p/resources/xal/javadoc/gov/sns/tools/math/ElementaryFunction.html
The details of the cutoff point and the Horner-like evaluation
was picked without reference to anything in particular.

Note that sinch is not currently implemented in scipy.special,
whereas the "engineer's" definition of sinc is implemented.
The implementation of sinc involves a scaling factor of pi
that distinguishes it from the "mathematician's" version of sinc.

gS㥋?r   g      @g      4@g      E@r   )absr"   r   )arQ   x2s      r   
_exp_sinchr    su    0 1vSvvayABbfqBsF|-D)D EEFFququ-!A#66r   c                 :    SX-   -  nSX-
  -  nU[        X45      -  $ )a  
Equation (10.42) of Functions of Matrices: Theory and Computation.

Notes
-----
This is a helper function for _fragment_2_1 of expm_2009.
Equation (10.42) is on page 251 in the section on Schur algorithms.
In particular, section 10.4.3 explains the Schur-Parlett algorithm.
expm([[lam_1, t_12], [0, lam_1])
=
[[exp(lam_1), t_12*exp((lam_1 + lam_2)/2)*sinch((lam_1 - lam_2)/2)],
[0, exp(lam_2)]
g      ?)r  )lam_1lam_2t_12r  r   s        r   	_eq_10_42r    s,    & 	u}Au}A*Q"""r   c                 B   U R                   S   n[        R                  " UR                  5       R	                  5       5      nSU* -  n[        R
                  " XT-  5      n[        U5       H  nXg   XU4'   M     [        US-
  SS5       H  nU R                  U 5      n SU* -  n[        R
                  " XT-  5      n[        U5       H  nXg   XU4'   M     [        US-
  5       H3  nXTU   -  n	XTUS-      -  n
XQXwS-   4   -  n[        XU5      nXXwS-   4'   M5     M     U $ )a  
A helper function for expm_2009.

Notes
-----
The argument X is modified in-place, but this modification is not the same
as the returned value of the function.
This function also takes pains to do things in ways that are compatible
with sparse arrays, for example by avoiding fancy indexing
and by using methods of the matrices whenever possible instead of
using functions of the numpy or scipy libraries themselves.

r   r   r   r/   )	r!   r"   rU   diagonalcopyr   r&   r'   r  )r\   r%   r   rr   diag_Tscaleexp_diagkr,   r  r  r  values                r   r
  r
    s&     	

AXXajjl'')*F !GEvven%H1X+Q$  1Q3BEE!H aR66%.)qAkAdG  qsA1I%E1Q3K'EQ!V9$DeD1Ea1fI   * Hr   c                    [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [        S5      eSSSSS	S
.nX!   nSn[        [	        U 5      SU-  S-   5      nU(       d  gU[        U 5      U-  -  n[        R                  " Xd-  5      n[        [        R                  " USU-  -  5      5      n[        US5      $ )z
A helper function for expm_2009.

Parameters
----------
A : linear operator
    A linear operator whose norm of power we care about.
m : int
    The power of the linear operator

Returns
-------
value : int
    A value related to a bound.

r   r   r   r   g     @g   `Bg //Cgu;	tDg5G)r   r   r   r   r   g      <)r    r!   r   r-   r  r   r"   r	  r   r  r(   )	r   mc_iabs_c_recipuA_abs_onenormr?   log2_alpha_div_ur'  s	            r   r  r  4  s    " 177|qAGGAJ!''!*4@AA %4	C &K 	A .c!facAg>M Xa[;67Ewwuw'(AE234Eua=r   c                 P   U R                   u  p#X#:w  a  [        S5      e[        U5      (       an  [        U5      nUS:  a  [	        S5      eUS:X  a  [        X R                  S9$ US:X  a  U R                  5       $ [        XS-  5      nUS-  (       a  X-  U-  $ XD-  $ [	        S5      e)aB  
Raise a square matrix to the integer power, `power`.

For non-negative integers, ``A**power`` is computed using repeated
matrix multiplications. Negative integers are not supported. 

Parameters
----------
A : (M, M) square sparse array or matrix
    sparse array that will be raised to power `power`
power : int
    Exponent used to raise sparse array `A`

Returns
-------
A**power : (M, M) sparse array or matrix
    The output matrix will be the same shape as A, and will preserve
    the class of A, but the format of the output may be changed.

Notes
-----
This uses a recursive implementation of the matrix power. For computing
the matrix power using a reasonably large `power`, this may be less efficient
than computing the product directly, using A @ A @ ... @ A.
This is contingent upon the number of nonzero entries in the matrix. 

.. versionadded:: 1.12.0

Examples
--------
>>> from scipy import sparse
>>> A = sparse.csc_array([[0,1,0],[1,0,1],[0,1,0]])
>>> A.todense()
array([[0, 1, 0],
       [1, 0, 1],
       [0, 1, 0]])
>>> (A @ A).todense()
array([[1, 0, 1],
       [0, 2, 0],
       [1, 0, 1]])
>>> A2 = sparse.linalg.matrix_power(A, 2)
>>> A2.todense()
array([[1, 0, 1],
       [0, 2, 0],
       [1, 0, 1]])
>>> A4 = sparse.linalg.matrix_power(A, 4)
>>> A4.todense()
array([[2, 0, 2],
       [0, 4, 0],
       [2, 0, 2]])

zsparse matrix is not squarer   zexponent must be >= 0r   r   r   zexponent must be an integer)	r!   r   r   r   r   r   r   r"  r   )r   powerr+   Ntmps        r   r   r   b  s    j 77DAv566E
19455A:Qgg..A:668O1qj)197S= 9677r   )NN)r   r   FFNrP   )*r   __all__numpyr"   scipy.linalg._basicr   r   scipy.sparse._baser   scipy.sparse.linalgr	   scipy.sparse._sputilsr
   r   scipy.sparser0   scipy.sparse.linalg._interfacer   scipy.sparse._constructr   _expm_multiplyr   r   r   r;   r   r-   r7   rC   rE   rj   r   r   r   r   r   r  r  r  r
  r  r   rh   r   r   <module>r=     s    *  7 ' ' ?   9 - B & .bB((V!7. !7H,(n ,(` CG&<T CG'ATN Nb-.`WtL@7>#0.b,\J8r   