
    5[gm                        d Z ddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ ddlmZ ddlZ G d	 d
e      Z G d de      Z G d de      Z G d dej2                  j4                        Z G d dej2                  j4                        Z G d de      Z	 d#dZd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d  Z)d$d!Z*d" Z+y)%zR
Matrix functions that use Pade approximation with inverse scaling and squaring.

    N)
SqrtmError_sqrtm_triu)schurrsf2csf)funm)svdvalssolve_triangular)LinearOperator)
onenormestc                       e Zd Zy)LogmRankWarningN__name__
__module____qualname__     Y/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/linalg/_matfuncs_inv_ssq.pyr   r          r   r   c                       e Zd Zy)LogmExactlySingularWarningNr   r   r   r   r   r      r   r   r   c                       e Zd Zy)LogmNearlySingularWarningNr   r   r   r   r   r      r   r   r   c                       e Zd Zy)	LogmErrorNr   r   r   r   r   r      r   r   r   c                       e Zd Zy)FractionalMatrixPowerErrorNr   r   r   r   r   r   "   r   r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_MatrixM1PowerOperatorz<
    A representation of the linear operator (A - I)^p.
    c                    |j                   dk7  s|j                  d   |j                  d   k7  rt        d      |dk  s|t        |      k7  rt        d      || _        || _        |j                   | _         |j                  | _        y )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__z_MatrixM1PowerOperator.__init__,   sq    66Q;!''!*
2DEEq5AQKFGGFF	WW
r   c                 v    t        | j                        D ]   }| j                  j                  |      |z
  }" |S Nranger(   r'   dotr)   xis      r   _matvecz_MatrixM1PowerOperator._matvec6   /    twwAA"A  r   c                 v    t        | j                        D ]   }|j                  | j                        |z
  }" |S r.   )r0   r(   r1   r'   r2   s      r   _rmatvecz_MatrixM1PowerOperator._rmatvec;   s/    twwAdgg"A  r   c                 v    t        | j                        D ]   }| j                  j                  |      |z
  }" |S r.   r/   )r)   Xr4   s      r   _matmatz_MatrixM1PowerOperator._matmat@   r6   r   c                 V    t        | j                  j                  | j                        S r.   )r   r'   Tr(   )r)   s    r   _adjointz_MatrixM1PowerOperator._adjointE   s    %dggii99r   N)	r   r   r   __doc__r,   r5   r8   r;   r>   r   r   r   r   r   '   s     


:r   r   c                 6    t        t        | |      ||||      S )a  
    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   )r*   r+   rA   rB   rC   rD   s         r   _onenormest_m1_powerrE   J   s'    J ,Q2u	YH Hr   c                     t        t        j                  | j                  t        j                  z
  dt        j                  z  z              S )a  
    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   _unwindkrL   s   s2    H rww1RUU73455r   c                    |dk  st        |      |k7  rt        d      |dk(  r| dz
  S |dk(  rt        j                  |       dz
  S |}t        j                  |       t        j
                  dz  k\  rt        j                  |       } |dz
  }| dz
  }t        j                  |       } d| z   }t        d|      D ]  }t        j                  |       } |d| z   z  }! ||z  }|S )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%   rG   sqrtanglerJ   r0   )akk_hatz0rjs         r   _briggs_helper_functionrV      s    D 	1uA!;<<Av1u	
awwqzA~88A;"%%!)#
AEEUGGAJEq%A
AQUA ! Fr   c                    | |k(  r||z  | |dz
  z  z  }|S t        || z
        t        | |z         dz  kD  r|||z  | |z  z
  z  || z
  z  }|S || z
  || z   z  }t        j                  |       }t        j                  |      }t        j                  |      }|t        j                  |dz  ||z   z        z  }	t        ||z
        }
|
r||t        j                  dz  |
z  z   z  }n||z  }dt        j                  |      z  || z
  z  }|	|z  }|S )aO  
    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!                 ?)absrG   logarctanhexprL   rJ   sinh)l1l2t12r+   f12rK   log_l1log_l2	arctanh_ztmp_atmp_utmp_btmp_cs                r   !_fractional_power_superdiag_entryri      s)   F 
RxAgQqS	!" J! 
R"WBGq(	(b!eA&'273 J "Wb!JJqM	bffacFVO455&)RUURZ%%778E	MEBGGEN"b2g.emJr   c                    | |k(  r|| z  }|S t        || z
        t        | |z         dz  kD  r6|t        j                  |      t        j                  |       z
  z  || z
  z  }|S || z
  || z   z  }t        t        j                  |      t        j                  |       z
        }|r:|dz  t        j                  |      t        j
                  dz  |z  z   z  || z
  z  }|S |dz  t        j                  |      z  || z
  z  }|S )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!   rX   )rY   rG   rZ   rL   r[   rJ   )r^   r_   r`   ra   rK   us         r   _logm_superdiag_entryrl   	  s    B 
RxBh J 
R"WBGq(	(RVVBZ"&&*,-b9 J "Wb!RVVBZ"&&*,-'RZZ]RUU2XaZ78BGDC J 'BJJqM)R"W5CJr   c                 x   t        | j                        dk7  s| j                  d   | j                  d   k7  rt        d      | j                  \  }}| }d}t        j                  |      }t        j
                  |      |k7  rt        d      t        j                  t        j                  |dz
        d      d   kD  rNt        j                  |      }|dz  }t        j                  t        j                  |dz
        d      d   kD  rNt        |      D ]  }t        |      } |}d}t        |d      d	z  }	t        |d
      dz  }
t        |	|
      }d}dD ]  }||   k  s|} n |||kD  rt        |d
      dz  }
t        |d      dz  }t        |
|      d   k  rCt        fddD              }|dk  r|}nvdz  d   k  r|dk  r|dz  }t        |      }|dz  }|t        |d      dz  }t        ||      }t        |      }dD ]  }||   k  s|} n |nt        |      }|dz  }||t        j                  |      z
  }t        d t        j                  |       D              }|rt        |      D ]  }| ||f   }t!        ||      }||||f<    t        j"                  |       }t        |dz
        D ]8  }| ||f   }| |dz   |dz   f   }| ||dz   f   }t%        ||||      }||||dz   f<   : t        j&                  |t        j(                  |            st        d      |||fS )az  
    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   4   K   | ]  }|   k  s|  y wr.   r   ).0r4   a3thetas     r   	<genexpr>z+_inverse_squaring_helper.<locals>.<genexpr>  s     B12q>Qs   )rt   ru         rr   r|   r{   皙?)r|   rr   c              3   \   K   | ]$  }|j                   d kD  xs |j                  d k7   & ywr   NrealrI   rw   r3   s     r   rz   z+_inverse_squaring_helper.<locals>.<genexpr>  )     N+Qqvvz8QVVq[8+   *,zR is not upper triangular)lenr$   r%   rG   diagcount_nonzero	ExceptionmaxabsoluterN   r0   r   rE   minidentityallrV   exp2ri   array_equaltriu)T0ry   nr=   s0tmp_diagr4   srQ   d2d3a2md4j1d5a4etaRhas_principal_branchrU   rP   rT   r+   r^   r_   r`   ra   rx   s    `                          @r   _inverse_squaring_helperr   8  s_   \ 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  )r6%a+4B!!Q'C0R[q>BBBBQwa58#AQNQ!!Q'C0R["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a7Nr   c                    | dk  rt        d      d|cxk  rdk  st        d       t        d      | dk(  r| S | dz  dk(  r| dz  }| |z   dd|z  dz
  z  z  S | dz  dk(  r| dz
  dz  }| |z
  dd|z  dz   z  z  S t        d|        )Nr"   zexpected a positive integer iexpected -1 < t < 1r!   r   zunnexpected value of i, i = )r%   r   )r4   rA   rU   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                 ~   |dk  st        |      |k7  rt        d      d|cxk  rdk  st        d       t        d      t        j                  |       } t	        | j
                        dk7  s| j
                  d   | j
                  d   k7  rt        d      | j
                  \  }}t        j                  |      }| t        d|z  |      z  }t        d|z  dz
  dd      D ]   }| t        ||      z  }t        ||z   |      }" ||z   }t        j                  |t        j                  |            st        d      |S )	a  
    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   rn   U is not upper triangular)r&   r%   rG   asarrayr   r$   r   r   r0   r	   r   r   r   )	r   rA   r   r   identYrU   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!Q77UQY, # 		A>>!RWWQZ(344Hr   c                 .   dddddddd}| j                   \  }}| }t        j                  |      }t        j                  |t        j                  |            rt        j                  ||z        }nt	        ||      \  }}}	t        | ||	      }t        j                  |      }
t        d	 |
D              }t        |d
d
      D ]  }||k  r|j                  |      }|s|t        j                  |       z  }||z  |t        j                  |      <   t        |dz
        D ]8  }|||f   }||dz   |dz   f   }|||dz   f   }t        ||||      }||||dz   f<   :  t        j                  |t        j                  |            st        d      |S )a  
    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!   rt   ru   r{   r|   rr   c              3   \   K   | ]$  }|j                   d kD  xs |j                  d k7   & ywr   r   r   s     r   rz   z/_remainder_matrix_power_triu.<locals>.<genexpr>A  s)     "Mf166A:#<1#<fr   r   r"   r   )r$   rG   r   r   r   r   r   r0   r1   r   diag_indicesri   r   r   )r=   rA   
m_to_thetar   r   T0_diagr   r   r   r   eivalsr   r4   r+   rU   r^   r_   r`   ra   s                      r   _remainder_matrix_power_triur     s   B J 77DAq	
BggbkG	~~b"'''*+GGGqL!*2z:1a
 #A2q!, ""Mf"MMq"b!A1uEE!H'BGGQBKA,3qLAbooa()"1Q3Z1X!QqS\ AaCj?BQO$'!QqS&	 ( " >>!RWWQZ(344Hr   c                    t        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt	        d      | j                  \  }}t        j
                  | t        j                  |             rd}| }nlt        j                  |       rGt        |       \  }}t        j
                  |t        j                  |            s t        ||      \  }}nt        | d      \  }}t        j                  |      }t        j                  |      |k7  rt        d      t        j                  |      r-t        j                  |      dk  r|j                  t              }t!        ||      }|?t        j"                  |      j$                  }|j'                  |      j'                  |      S |S )	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)rG   r   r   r$   r%   r   r   	isrealobjr   r   r   r   r   r   astyper   r   	conjugater=   r1   )r*   rA   r   Zr=   T_diagr   ZHs           r   _remainder_matrix_powerr   T  sf   @ 	

1A
177|qAGGAJ!''!*4788 77DAq 
~~a$<<?8DAq>>!RWWQZ0q!}19-DAq WWQZF	1$(CD 	D 
||A266&>A-HHW 	%Q*A}\\!_uuQx||Br   c                    t        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt	        d      |t        |      k(  r)t         j                  j                  | t        |            S t        |       }|d   r|d   |d   z  }|t        j                  |      z
  }|t        j                  |      z
  }||d|z
  z  z  | |z  k  r!t        t        j                  |            }|n t        t        j                  |            }|	 t        |       }t         j                  j                  | |      }|j                  |      S |dk  r6t        j                  |       }	|	j                  t         j                          |	S |t        j                  |      z
  }t        t        j                  |            }|t#        | fdd      \  }}
t         j                  j                  | |      }|j                  |      S # t         j                  j                  $ r Y w xY w)	z
    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                     t        |       S r.   )pow)r3   bs    r   <lambda>z*_fractional_matrix_power.<locals>.<lambda>  s    C1Ir   F)disp)rG   r   r   r$   r%   r&   linalgmatrix_powerr   floorrH   r   r1   LinAlgError
empty_likefillnanr   )r*   r+   r   k2p1p2rP   r   Qr:   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:4II""1a(uuQx yy$$ 		s   /<H$ $IIc                    t        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt	        d      | j                  \  }}t        j
                  |       }t        j                  |       xr t        j                  |d      dk\  }|r| }n| j                  t              }d}t        ||      \  }}}t        j                  j                  |      \  }	}
|	j                  }	|	j                  |fk7  s|
j                  |fk7  rt        d      d	d	|	z  z   }	d	|
z  }
t        j                   |      }t        j"                  |      }t%        |
|	      D ]  \  }}|t'        |||z  z   ||z        z  } |t        j(                  |      z  }t+        d
 t        j
                  |      D              }|rt        j,                  t        j
                  |            |t        j.                  |      <   t1        |dz
        D ]5  }|||f   }||dz   |dz   f   }|||dz   f   }t3        |||      |||dz   f<   7 t        j4                  |t        j6                  |            st        d      |S )a  
    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"   rn   ro   rp   )Ng0>g3kb?gj+ݓ?gz):˯?gMb?g|?5^?g;On?grh|?gjt?gjt?gQ?grh|?gw/?g?5^I?gOn?g+?zinternal errorrs   c              3   \   K   | ]$  }|j                   d kD  xs |j                  d k7   & ywr   r   r   s     r   rz   z_logm_triu.<locals>.<genexpr>  r   r   r   )rG   r   r   r$   r%   r   r   r   r   r   r   scipyspecialp_rootsr   r   r   
zeros_likezipr	   r   r   rZ   r   r0   rl   r   r   )r=   r   r   keep_it_realr   ry   r   r   r   nodesweightsr   r   alphabetar   r4   r^   r_   r`   s                       r   
_logm_triur     sB   F 	

1A
177|qAGGAJ!''!*4EFF77DAq WWQZF<<?Frvvfb'AQ'FLXXg0E 'r51GAq! ]]**1-NE7JJE{{qdgmmt3())#+EGmGKKNE
aA7E*t	ed1fneAg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                    d}t        j                  t        j                  |             }t        j                  |dk(        r`d}t	        j
                  |t        d       |s| j                         } | j                  d   }t        |      D ]  }| ||f   r|| ||f<    | S t        j                  ||k        rd}t	        j
                  |t        d       | S )Ng#B;r   z*The logm input matrix is exactly singular.rt   )
stacklevelz-The logm input matrix may be nearly singular.)rG   r   r   anywarningswarnr   copyr$   r0   r   )r=   inplacetri_epsabs_diagexact_singularity_msgr   r4   near_singularity_msgs           r   )_logm_force_nonsingular_triangular_matrixr   4  s     G{{2771:&H	vvh!m L+-GTUVAGGAJqAQT7!!Q$  H 
7"	#N*,ERSTHr   c                 f   t        j                  |       } t        | j                        dk7  s| j                  d   | j                  d   k7  rt	        d      t        | j                  j                  t         j                        rt        j                  | t              } t        j                  |       }	 t        j                  | t        j                  |             rXt        |       } t        j                  t        j                  |       d      dk  r| j!                  t"              } t%        |       S |rGt'        |       \  }}t        j                  |t        j                  |            s t)        ||      \  }}nt'        | d	      \  }}t        |d
      }t%        |      }t        j*                  |      j,                  }|j/                  |      j/                  |      S # t0        t2        f$ r9 t        j4                  |       }|j7                  t         j8                         |cY S w xY w)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   )dtypero   rp   r   r   T)r   )rG   r   r   r$   r%   
issubclassr   typeintegerfloatr   r   r   r   r   r   r   r   r   r   r   r   r=   r1   r   r   r   r   r   )r*   r   r=   r   r   r   r:   s          r   _logmr   H  s   " 	

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

+JJq&<<?L>>!RWWQZ(9!<Avvbggaj"-1HHW%a= Qx1~~a4"1a=DAqQy119!TJA1Aa""B558<<##	" MM!	rvvs   7B G( 8B/G( (AH0/H0)r!   r{   FF)F),r?   r   numpyrG   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   rE   rL   rV   ri   rl   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   