
    (phm                        S r SSKrSSKrSSKJrJr  SSKJrJ	r	  SSK
Jr  SSKJrJr  SSKJr  SSKJr  SSKr " S	 S
\5      r " S S\5      r " S S\5      r " S S\R2                  R4                  5      r " S S\R2                  R4                  5      r " S S\5      r S#S jrS rS r S r!S r"S r#S r$S r%S r&S r'S r(S  r)S$S! jr*S" r+g)%zR
Matrix functions that use Pade approximation with inverse scaling and squaring.

    N)
SqrtmError_sqrtm_triu)schurrsf2csf)funm)svdvalssolve_triangular)LinearOperator)
onenormestc                       \ rS rSrSrg)LogmRankWarning    N__name__
__module____qualname____firstlineno____static_attributes__r       Q/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_matfuncs_inv_ssq.pyr   r          r   r   c                       \ rS rSrSrg)LogmExactlySingularWarning   r   Nr   r   r   r   r   r      r   r   r   c                       \ rS rSrSrg)LogmNearlySingularWarning   r   Nr   r   r   r   r   r      r   r   r   c                       \ rS rSrSrg)	LogmError   r   Nr   r   r   r   r    r       r   r   r    c                       \ rS rSrSrg)FractionalMatrixPowerError"   r   Nr   r   r   r   r#   r#   "   r   r   r#   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_MatrixM1PowerOperator'   z4
A representation of the linear operator (A - I)^p.
c                    UR                   S:w  d   UR                  S   UR                  S   :w  a  [        S5      eUS:  d  U[        U5      :w  a  [        S5      eXl        X l        UR                   U l         UR                  U l        g )N   r      z%expected A to be like a square matrixz'expected p to be a non-negative integer)ndimshape
ValueErrorint_A_p)selfAps      r   __init___MatrixM1PowerOperator.__init__,   sm    66Q;!''!*
2DEEq5AQKFGGFF	WW
r   c                 z    [        U R                  5       H!  nU R                  R                  U5      U-
  nM#     U$ Nranger0   r/   dotr1   xis      r   _matvec_MatrixM1PowerOperator._matvec6   /    twwAA"A  r   c                 z    [        U R                  5       H!  nUR                  U R                  5      U-
  nM#     U$ r7   )r9   r0   r:   r/   r;   s      r   _rmatvec_MatrixM1PowerOperator._rmatvec;   s/    twwAdgg"A  r   c                 z    [        U R                  5       H!  nU R                  R                  U5      U-
  nM#     U$ r7   r8   )r1   Xr=   s      r   _matmat_MatrixM1PowerOperator._matmat@   r@   r   c                 V    [        U R                  R                  U R                  5      $ r7   )r&   r/   Tr0   )r1   s    r   _adjoint_MatrixM1PowerOperator._adjointE   s    %dggii99r   )r/   r0   r+   r,   N)r   r   r   r   __doc__r4   r>   rB   rF   rJ   r   r   r   r   r&   r&   '   s     


:r   r&   c                 *    [        [        X5      X#XES9$ )aM  
Efficiently estimate the 1-norm of (A - I)^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 matrix.
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.

)titmax	compute_v	compute_w)r   r&   )r2   r3   rN   rO   rP   rQ   s         r   _onenormest_m1_powerrR   J   s!    J ,Q2	H Hr   c                     [        [        R                  " U R                  [        R                  -
  S[        R                  -  -  5      5      $ )a0  
Compute the scalar unwinding number.

Uses Eq. (5.3) in [1]_, and should be equal to (z - log(exp(z)) / (2 pi i).
Note that this definition differs in sign from the original definition
in equations (5, 6) in [2]_.  The sign convention is justified in [3]_.

Parameters
----------
z : complex
    A complex number.

Returns
-------
unwinding_number : integer
    The scalar unwinding number of z.

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

.. [2] Robert M. Corless and David J. Jeffrey,
       "The unwinding number." Newsletter ACM SIGSAM Bulletin
       Volume 30, Issue 2, June 1996, Pages 28-35.

.. [3] Russell Bradford and Robert M. Corless and James H. Davenport and
       David J. Jeffrey and Stephen M. Watt,
       "Reasoning about the elementary functions of complex analysis"
       Annals of Mathematics and Artificial Intelligence,
       36: 303-318, 2002.

r)   )r.   npceilimagpi)zs    r   _unwindkrY   s   s2    H rww1RUU73455r   c                    US:  d  [        U5      U:w  a  [        S5      eUS:X  a  U S-
  $ US:X  a  [        R                  " U 5      S-
  $ Un[        R                  " U 5      [        R
                  S-  :  a  [        R                  " U 5      n US-
  nU S-
  n[        R                  " U 5      n SU -   n[        SU5       H!  n[        R                  " U 5      n USU -   -  nM#     X4-  nU$ )a+  
Computes r = a^(1 / (2^k)) - 1.

This is algorithm (2) of [1]_.
The purpose is to avoid a danger of subtractive cancellation.
For more computational efficiency it should probably be cythonized.

Parameters
----------
a : complex
    A complex number.
k : integer
    A nonnegative integer.

Returns
-------
r : complex
    The value r = a^(1 / (2^k)) - 1 computed with less cancellation.

Notes
-----
The algorithm as formulated in the reference does not handle k=0 or k=1
correctly, so these are special-cased in this implementation.
This function is intended to not allow `a` to belong to the closed
negative real axis, but this constraint is relaxed.

References
----------
.. [1] Awad H. Al-Mohy (2012)
       "A more accurate Briggs method for the logarithm",
       Numerical Algorithms, 59 : 393--402.

r   z expected a nonnegative integer kr*   r)   )r.   r-   rT   sqrtanglerW   r9   )akk_hatz0rjs         r   _briggs_helper_functionrc      s    D 	1uA!;<<Av1u	
awwqzA~88A;"%%!)#
AEEUGGAJEq%A
AQUA ! Fr   c                    X:X  a  X#-  XS-
  -  -  nU$ [        X-
  5      [        X-   5      S-  :  a  X!U-  X-  -
  -  X-
  -  nU$ X-
  X-   -  n[        R                  " U 5      n[        R                  " U5      n[        R                  " U5      nU[        R                  " US-  Xv-   -  5      -  n	[        Xv-
  5      n
U
(       a  X8[        R                  S-  U
-  -   -  nOX8-  nS[        R                  " U5      -  X-
  -  nX-  nU$ )a  
Compute a superdiagonal entry of a fractional matrix power.

This is Eq. (5.6) in [1]_.

Parameters
----------
l1 : complex
    A diagonal entry of the matrix.
l2 : complex
    A diagonal entry of the matrix.
t12 : complex
    A superdiagonal entry of the matrix.
p : float
    A fractional power.

Returns
-------
f12 : complex
    A superdiagonal entry of the fractional matrix power.

Notes
-----
Care has been taken to return a real number if possible when
all of the inputs are real numbers.

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

r*   r)                 ?)absrT   logarctanhexprY   rW   sinh)l1l2t12r3   f12rX   log_l1log_l2	arctanh_ztmp_atmp_utmp_btmp_cs                r   !_fractional_power_superdiag_entryrv      s	   F 
xgqS	!" J! 
RWBGq(	(!e&'273 J W!JJqM	bffacFO455)RUURZ%%778EMEBGGEN"bg.mJr   c                 
   X:X  a  X -  nU$ [        X-
  5      [        X-   5      S-  :  a7  U[        R                  " U5      [        R                  " U 5      -
  -  X-
  -  nU$ X-
  X-   -  n[        [        R                  " U5      [        R                  " U 5      -
  5      nU(       a:  US-  [        R                  " U5      [        R
                  S-  U-  -   -  X-
  -  nU$ US-  [        R                  " U5      -  X-
  -  nU$ )a  
Compute a superdiagonal entry of a matrix logarithm.

This is like Eq. (11.28) in [1]_, except the determination of whether
l1 and l2 are sufficiently far apart has been modified.

Parameters
----------
l1 : complex
    A diagonal entry of the matrix.
l2 : complex
    A diagonal entry of the matrix.
t12 : complex
    A superdiagonal entry of the matrix.

Returns
-------
f12 : complex
    A superdiagonal entry of the matrix logarithm.

Notes
-----
Care has been taken to return a real number if possible when
all of the inputs are real numbers.

References
----------
.. [1] Nicholas J. Higham (2008)
       "Functions of Matrices: Theory and Computation"
       ISBN 978-0-898716-46-7

r)   re   )rf   rT   rg   rY   rh   rW   )rk   rl   rm   rn   rX   us         r   _logm_superdiag_entryry   	  s    B 
xh J 
RWBGq(	(RVVBZ"&&*,-9 J W!RVVBZ"&&*,-'RZZ]RUU2XaZ78BGDC J 'BJJqM)RW5CJr   c                   ^^ [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [        S5      eU R                  u    nU nSn[        R                  " U5      n[        R
                  " U5      U:w  a  [        S5      e[        R                  " [        R                  " US-
  5      SS9TS   :  aP  [        R                  " U5      nUS-  n[        R                  " [        R                  " US-
  5      SS9TS   :  a  MP  [        U5       H  n[        U5      nM     UnSn[        US5      S	-  n	[        US
5      S-  n
[        X5      nSnS H  nUTU   ::  d  M  Un  O   Uc  Xt:  a  [        US
5      S-  n
[        US5      S-  n[        X5      mTTS   ::  aH  [        UU4S jS 5       5      nUS::  a  UnO~TS-  TS   ::  a  US:  a  US-  n[        U5      nUS-  nM  [        US5      S-  n[        X5      n[        TU5      nS H  nUTU   ::  d  M  Un  O   Ub  O[        U5      nUS-  nUc  M  U[        R                  " U5      -
  n[        S [        R                  " U 5       5       5      nU(       a  [        U5       H  nU UU4   n[!        UU5      nUUUU4'   M     [        R"                  " U* 5      n[        US-
  5       H9  nU UU4   nU US-   US-   4   nU UUS-   4   n[%        UUUU5      nUUUUS-   4'   M;     [        R&                  " U[        R(                  " U5      5      (       d  [        S5      eUX|4$ )a  
A helper function for inverse scaling and squaring for Pade approximation.

Parameters
----------
T0 : (N, N) array_like upper triangular
    Matrix involved in inverse scaling and squaring.
theta : indexable
    The values theta[1] .. theta[7] must be available.
    They represent bounds related to Pade approximation, and they depend
    on the matrix function which is being computed.
    For example, different values of theta are required for
    matrix logarithm than for fractional matrix power.

Returns
-------
R : (N, N) array_like upper triangular
    Composition of zero or more matrix square roots of T0, minus I.
s : non-negative integer
    Number of square roots taken.
m : positive integer
    The degree of the Pade approximation.

Notes
-----
This subroutine appears as a chunk of lines within
a couple of published algorithms; for example it appears
as lines 4--35 in algorithm (3.1) of [1]_, and
as lines 3--34 in algorithm (4.1) of [2]_.
The instances of 'goto line 38' in algorithm (3.1) of [1]_
probably mean 'goto line 36' and have been interpreted accordingly.

References
----------
.. [1] Nicholas J. Higham and Lijing Lin (2013)
       "An Improved Schur-Pade Algorithm for Fractional Powers
       of a Matrix and their Frechet Derivatives."

.. [2] 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

r)   r   r*   *expected an upper triangular square matrixz%Diagonal entries of T must be nonzero        initial         ?   gUUUUUU?N)r*   r)      g      ?c              3   >   >#    U  H  nTTU   ::  d  M  Uv   M     g 7fr7   r   ).0r=   a3thetas     r   	<genexpr>+_inverse_squaring_helper.<locals>.<genexpr>  s     B12q>QQs   	)r   r         r   r   r   皙?)r   r   c              3   j   #    U  H)  oR                   S :  =(       d    UR                  S :g  v   M+     g7fr   NrealrV   r   r<   s     r   r   r     &     N+Qvvz8QVVq[8+   13zR is not upper triangular)lenr,   r-   rT   diagcount_nonzero	Exceptionmaxabsoluter[   r9   r   rR   minidentityallrc   exp2rv   array_equaltriu)T0r   nrI   s0tmp_diagr=   sr^   d2d3a2md4j1d5a4etaRhas_principal_branchrb   r]   ra   r3   rk   rl   rm   rn   r   s    `                          @r   _inverse_squaring_helperr   8  sX   \ 288}RXXa[BHHQK7EFF88DAq
A 
BwwqzH	!Q&?@@
&&X\*B
7%(
B778$
a &&X\*B
7%(
B
 2YN  	A	A	a	#	,B	a	#	,B	RBAq>A  )6%a+4B!!Q'C0[q>BBBBQwa58#AQNQ!!Q'C0["bkAeAh  =N	Q3 )< 	
BKKNA N"''"+NNqA1a4A'1-AAadG  GGQBKqsAAqDBAaC1HBQ!V*C3BCCCAa1fI  >>!RWWQZ((344a7Nr   c                    U S:  a  [        S5      eSUs=:  a  S:  d  O  [        S5      eU S:X  a  U* $ U S-  S:X  a  U S-  nU* U-   SSU-  S-
  -  -  $ U S-  S:X  a  U S-
  S-  nU* U-
  SSU-  S-   -  -  $ [        SU  35      e)Nr*   zexpected a positive integer iexpected -1 < t < 1r)   r   zunnexpected value of i, i = )r-   r   )r=   rN   rb   s      r   _fractional_power_pade_constantr     s    1u899JQJ.//Avr		
Q!FQ1!a=))	
Q!UqLQ1!a=))6qc:;;r   c                    US:  d  [        U5      U:w  a  [        S5      eSUs=:  a  S:  d  O  [        S5      e[        R                  " U 5      n [	        U R
                  5      S:w  d   U R
                  S   U R
                  S   :w  a  [        S5      eU R
                  u    n[        R                  " U5      nU [        SU-  U5      -  n[        SU-  S-
  SS5       H  nU [        Xa5      -  n[        XE-   U5      nM!     XE-   n[        R                  " U[        R                  " U5      5      (       d  [        S5      eU$ )	aE  
Evaluate the Pade approximation of a fractional matrix power.

Evaluate the degree-m Pade approximation of R
to the fractional matrix power t using the continued fraction
in bottom-up fashion using algorithm (4.1) in [1]_.

Parameters
----------
R : (N, N) array_like
    Upper triangular matrix whose fractional power to evaluate.
t : float
    Fractional power between -1 and 1 exclusive.
m : positive integer
    Degree of Pade approximation.

Returns
-------
U : (N, N) array_like
    The degree-m Pade approximation of R to the fractional power t.
    This matrix will be upper triangular.

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

r*   zexpected a positive integer mr   r   r)   r   r{   U is not upper triangular)r.   r-   rT   asarrayr   r,   r   r   r9   r	   r   r   r   )	r   rN   r   r   identYrb   rhsUs	            r   _fractional_power_pader     s   > 	1uA!899JQJ.//


1A
177|qAGGAJ!''!*4EFF77DAqKKNE	+AaC33A1Q37Ar"1!77UY, # 		A>>!RWWQZ((344Hr   c                 X   SSSSSSSS.nU R                   u    nU n[        R                  " U5      n[        R                  " U[        R                  " U5      5      (       a  [        R                  " XQ-  5      nO[	        XB5      u  pxn	[        U* X5      n[        R                  " U5      n
[        S	 U
 5       5      n[        US
S
5       H  nX:  a  UR                  U5      nM  U(       d  M$  U[        R                  " U* 5      -  nX]-  U[        R                  " U5      '   [        US-
  5       H5  nXNU4   nXNS-   US-   4   nXNUS-   4   n[        UUUU5      nUXnUS-   4'   M7     M     [        R                  " U[        R                  " U5      5      (       d  [        S5      eU$ )ab  
Compute a fractional power of an upper triangular matrix.

The fractional power is restricted to fractions -1 < t < 1.
This uses algorithm (3.1) of [1]_.
The Pade approximation itself uses algorithm (4.1) of [2]_.

Parameters
----------
T : (N, N) array_like
    Upper triangular matrix whose fractional power to evaluate.
t : float
    Fractional power between -1 and 1 exclusive.

Returns
-------
X : (N, N) array_like
    The fractional power of the matrix.

References
----------
.. [1] Nicholas J. Higham and Lijing Lin (2013)
       "An Improved Schur-Pade Algorithm for Fractional Powers
       of a Matrix and their Frechet Derivatives."

.. [2] 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

gǖ2>gמYb?gUN@?g?W[?gX9v?r   gB`"?)r*   r)   r   r   r   r   r   c              3   j   #    U  H)  oR                   S :  =(       d    UR                  S :g  v   M+     g7fr   r   r   s     r   r   /_remainder_matrix_power_triu.<locals>.<genexpr>A  s&     "Mf66A:#<1#<fr   r   r*   r   )r,   rT   r   r   r   r   r   r9   r:   r   diag_indicesrv   r   r   )rI   rN   
m_to_thetar   r   T0_diagr   r   r   r   eivalsr   r=   r3   rb   rk   rl   rm   rn   s                      r   _remainder_matrix_power_triur     s   B J 77DAq	
BggbkG	~~b"'''*++GGGL!*2:a
 #A2q, ""Mf"MMq"b!AuEE!H''BGGQBKA,3LAbooa()"1Q3Z1X!QqS\ AaCj?BQO$'QqS&	 ( " >>!RWWQZ((344Hr   c                    [         R                  " U 5      n [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [	        S5      eU R                  u    n[         R
                  " U [         R                  " U 5      5      (       a  SnU nOr[         R                  " U 5      (       aK  [        U 5      u  pC[         R
                  " U[         R                  " U5      5      (       d  [        XC5      u  pCO[        U SS9u  pC[         R                  " U5      n[         R                  " U5      U:w  a  [        S5      e[         R                  " U5      (       a/  [         R                  " U5      S:  a  UR                  [        5      n[!        XA5      nUb@  [         R"                  " U5      R$                  nUR'                  U5      R'                  U5      $ U$ )	a  
Compute the fractional power of a matrix, for fractions -1 < t < 1.

This uses algorithm (3.1) of [1]_.
The Pade approximation itself uses algorithm (4.1) of [2]_.

Parameters
----------
A : (N, N) array_like
    Matrix whose fractional power to evaluate.
t : float
    Fractional power between -1 and 1 exclusive.

Returns
-------
X : (N, N) array_like
    The fractional power of the matrix.

References
----------
.. [1] Nicholas J. Higham and Lijing Lin (2013)
       "An Improved Schur-Pade Algorithm for Fractional Powers
       of a Matrix and their Frechet Derivatives."

.. [2] 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

r)   r   r*   zinput must be a square arrayNcomplexoutputz`cannot use inverse scaling and squaring to find the fractional matrix power of a singular matrix)rT   r   r   r,   r-   r   r   	isrealobjr   r   r   r   r#   r   astyper   r   	conjugaterI   r:   )r2   rN   r   ZrI   T_diagr   ZHs           r   _remainder_matrix_powerr   T  sg   @ 	

1A
177|qAGGAJ!''!*4788 77DAq 
~~a$$<<??8DA>>!RWWQZ00q}9-DA WWQZF	1$(CD 	D 
||A266&>A-HHW 	%Q*A}\\!_uuQx||Br   c                   ^ [         R                  " U 5      n [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [	        S5      eU[        U5      :X  a)  [         R                  R                  U [        U5      5      $ [        U 5      nUS   (       a  US   US   -  nU[         R                  " U5      -
  nU[         R                  " U5      -
  nXCSU-
  -  -  U* U-  ::  a"  [        [         R                  " U5      5      nUmO![        [         R                  " U5      5      nUm [        U T5      n[         R                  R                  X5      nUR                  U5      $ US:  a7  [         R                  " U 5      n	U	R                  [         R                   5        U	$ U[         R                  " U5      -
  n[        [         R                  " U5      5      nUm[#        U U4S jSS9u  pz[         R                  R                  X5      nUR                  U5      $ ! [         R                  R                   a     Nf = f)	zu
Compute the fractional power of a matrix.

See the fractional_matrix_power docstring in matfuncs.py for more info.

r)   r   r*   expected a square matrixr   c                    > [        U T5      $ r7   )pow)r<   bs    r   <lambda>*_fractional_matrix_power.<locals>.<lambda>  s    C1Ir   F)disp)rT   r   r   r,   r-   r.   linalgmatrix_powerr   floorrU   r   r:   LinAlgError
empty_likefillnanr   )r2   r3   r   k2p1p2r]   r   QrE   infor   s              @r   _fractional_matrix_powerr     s    	

1A
177|qAGGAJ!''!*4344CF{yy%%aQ00
A 	u qTAbE\!_^q2v2#(*BHHQK AABGGAJAA	'1-A		&&q,A558O
 	1uMM!	rvv!_q-E:II""1(uuQx yy$$ 		s   <;H0 0IIc                 Z   [         R                  " U 5      n [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [	        S5      eU R                  u    n[         R
                  " U 5      n[         R                  " U 5      =(       a    [         R                  " USS9S:  nU(       a  U nOU R                  [        5      nSn[        XE5      u  pgn[        R                  R                  U5      u  pU	R                  n	U	R                  U4:w  d  U
R                  U4:w  a  [        S5      eS	S	U	-  -   n	S	U
-  n
[         R                   " U5      n[         R"                  " U5      n[%        X5       H  u  pU['        XU-  -   X-  5      -  nM     U[         R(                  " U5      -  n[+        S
 [         R
                  " U5       5       5      nU(       a  [         R,                  " [         R
                  " U5      5      U[         R.                  " U5      '   [1        US-
  5       H6  nUUU4   nUUS-   US-   4   nUUUS-   4   n[3        UUU5      UUUS-   4'   M8     [         R4                  " U[         R6                  " U5      5      (       d  [        S5      eU$ )au  
Compute matrix logarithm of an upper triangular matrix.

The matrix logarithm is the inverse of
expm: expm(logm(`T`)) == `T`

Parameters
----------
T : (N, N) array_like
    Upper triangular matrix whose logarithm to evaluate

Returns
-------
logm : (N, N) ndarray
    Matrix logarithm of `T`

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

r)   r   r*   r{   r|   r}   )Ng0>g3kb?gj+ݓ?gz):˯?gMb?g|?5^?g;On?grh|?gjt?gjt?gQ?grh|?gw/?g?5^I?gOn?g+?zinternal errorr   c              3   j   #    U  H)  oR                   S :  =(       d    UR                  S :g  v   M+     g7fr   r   r   s     r   r   _logm_triu.<locals>.<genexpr>  r   r   r   )rT   r   r   r,   r-   r   r   r   r   r   r   scipyspecialp_rootsr   r   r   
zeros_likezipr	   r   r   rg   r   r9   ry   r   r   )rI   r   r   keep_it_realr   r   r   r   r   nodesweightsr   r   alphabetar   r=   rk   rl   rm   s                       r   
_logm_triur     s7   F 	

1A
177|qAGGAJ!''!*4EFF77DAq WWQZF<<?Frvvfb'AQ'FLXXg0E 'r1GA! ]]**1-NEJJE{{qdgmmt3())#+EGmGKKNE
aA7*	e1fneg66 +OA
 N"''"+NN !#rwwr{ 3"//!

 qsAAqDBAaC1HBQ!V*C-b"c:Aa1fI	  >>!RWWQZ((344Hr   c                    Sn[         R                  " [         R                  " U 5      5      n[         R                  " US:H  5      (       ah  Sn[        R
                  " U[        SS9  U(       d  U R                  5       n U R                  S   n[        U5       H  nXU4   (       a  M  X Xf4'   M     U $ [         R                  " X2:  5      (       a  Sn[        R
                  " U[        SS9  U $ )Ng#B;r   z*The logm input matrix is exactly singular.r   )
stacklevelz-The logm input matrix may be nearly singular.)rT   r   r   anywarningswarnr   copyr,   r9   r   )rI   inplacetri_epsabs_diagexact_singularity_msgr   r=   near_singularity_msgs           r   )_logm_force_nonsingular_triangular_matrixr   4  s     G{{2771:&H	vvh!m L+-GTUVAGGAJqAT77!!$  H 
"	#	#N*,ERSTHr   c                    [         R                  " U 5      n [        U R                  5      S:w  d   U R                  S   U R                  S   :w  a  [	        S5      e[        U R                  R                  [         R                  5      (       a  [         R                  " U [        S9n [         R                  " U 5      n [         R                  " U [         R                  " U 5      5      (       aX  [        U 5      n [         R                  " [         R                  " U 5      SS9S:  a  U R!                  ["        5      n [%        U 5      $ U(       aK  ['        U 5      u  p#[         R                  " U[         R                  " U5      5      (       d  [)        X#5      u  p#O['        U SS	9u  p#[        US
S9n[%        U5      n[         R*                  " U5      R,                  nUR/                  U5      R/                  U5      $ ! [0        [2        4 a:    [         R4                  " U 5      nUR7                  [         R8                  5        Us $ f = f)a.  
Compute the matrix logarithm.

See the logm docstring in matfuncs.py for more info.

Notes
-----
In this function we look at triangular matrices that are similar
to the input matrix. If any diagonal entry of such a triangular matrix
is exactly zero then the original matrix is singular.
The matrix logarithm does not exist for such matrices,
but in such cases we will pretend that the diagonal entries that are zero
are actually slightly positive by an ad-hoc amount, in the interest
of returning something more useful than NaN. This will cause a warning.

r)   r   r*   r   )dtyper|   r}   r   r   T)r   )rT   r   r   r,   r-   
issubclassr   typeintegerfloatr   r   r   r   r   r   r   r   r   r   r   r   rI   r:   r   r    r   r   r   )r2   r   rI   r   r   r   rE   s          r   _logmr  H  s   " 	

1A
177|qAGGAJ!''!*4344 !'',,

++JJq&<<?L>>!RWWQZ((9!<Avvbggaj"-1HHW%a= Qx~~a44"1=DAQy19!TJA1Aa""B558<<##	" MM!	rvvs   >BG9 B2G9 9AII)r)   r   FF)F),rL   r   numpyrT   scipy.linalg._matfuncs_sqrtmr   r   scipy.linalg._decomp_schurr   r   scipy.linalg._matfuncsr   scipy.linalgr   r	   scipy.sparse.linalg._interfacer
   scipy.sparse.linalgr   scipy.specialr   UserWarningr   r   r   r   r   r    r#   r&   rR   rY   rc   rv   ry   r   r   r   r   r   r   r   r   r  r   r   r   <module>r     s      @ 5 ' 2 9 * 	k 		 		 				%% 		!6!6 	
:^ :H 27&HR$6N4n5p,^FR<$/dL^HV/d`F(.r   