+
    iH                     (    ^RI Ht  ! R R]4      tR# )   )xrangec                   X   a  ] tR t^t o R tRR ltR tR tR tRR lt	R t
R tR	tV tR
# )MatrixCalculusMethodsc                  a  V 3R lp^p^p V! V4      S P                   8  d   MV^,          pK$  W4,          p\        \        ^S P                  S P	                  VR4      4      4      4      pTpS P
                  pS ;P                  V^,           ,          un         V^V,          ,          pVP                  pS P                  V4      p	S P                  V4      p
S P                  V4      pS P                  ^4      p\        ^V^,           4       Fz  pVS P                  W=,
          ^,           4      ^V,          V,
          ^,           V,          ,          ,          pW,          pW,          pW,          p
V	RV,          V,          ,          p	K|  	  S P                  W4      p\        V4       F  pW,          pK  	  VS n        V^,          #   TS n        i ; i)z
Exponential of a matrix using Pade approximants.

See G. H. Golub, C. F. van Loan 'Matrix Computations',
third Ed., page 572

TODO:
 - find a good estimate for q
 - reduce the number of matrix multiplications to improve
   performance
c                    < SP                  ^4      ^^V ,          ,
          ,          SP                  V 4      ^,          ,          SP                  ^V ,          4      ^,          ^V ,          ^,           ,          ,          # r   )mpf	factorial)pctxs   &x/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/mpmath/matrices/calculus.pyeps_pade1MatrixCalculusMethods._exp_pade.<locals>.eps_pade   s_    771:!A#&a !#$%(]]1Q3%7%:acAg%FH H    inf)epsintmaxmagmnormprecdpsrowseyer	   rangelu_solve_mat)r   ar   qextraqjextrar   nadennumxckcxfs   f&              r   	_exp_padeMatrixCalculusMethods._exp_pade   s}   	H {SWW$FA	Aswwsyy51234xx519	!Q$AB''"+C''"+CA
A1ac]SWWQUQY'!A#'A+):;;CS	Qw|# #   *A1XC  CHs
 CHs   DG 	Gc                   VR8X  db   V P                   p V P                  V4      pV ;P                   ^VP                  ,          ,          un         V P                  V4      pW0n         V# V P                  V4      pV P                   p\	        \        ^V P                  V P                  VR4      4      4      4      pV\	        RVR,          ,          4      ,          p V ;P                   ^
^V,          ,           ,          un         V P                  5pV^V,          ,          pTpV^ ,          V,           p^p	 Wq^V P                  V	4      ,          ,          ,          pV P                  VR4      V8  d   MW,          pV	^,          p	KQ  \        V4       F  p	W,          pK  	  W0n         V^,          pV#   Y0n         i ; i  Y0n         i ; i)a~  
Computes the matrix exponential of a square matrix `A`, which is defined
by the power series

.. math ::

    \exp(A) = I + A + \frac{A^2}{2!} + \frac{A^3}{3!} + \ldots

With method='taylor', the matrix exponential is computed
using the Taylor series. With method='pade', Pade approximants
are used instead.

**Examples**

Basic examples::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> expm(zeros(3))
    [1.0  0.0  0.0]
    [0.0  1.0  0.0]
    [0.0  0.0  1.0]
    >>> expm(eye(3))
    [2.71828182845905               0.0               0.0]
    [             0.0  2.71828182845905               0.0]
    [             0.0               0.0  2.71828182845905]
    >>> expm([[1,1,0],[1,0,1],[0,1,0]])
    [ 3.86814500615414  2.26812870852145  0.841130841230196]
    [ 2.26812870852145  2.44114713886289   1.42699786729125]
    [0.841130841230196  1.42699786729125    1.6000162976327]
    >>> expm([[1,1,0],[1,0,1],[0,1,0]], method='pade')
    [ 3.86814500615414  2.26812870852145  0.841130841230196]
    [ 2.26812870852145  2.44114713886289   1.42699786729125]
    [0.841130841230196  1.42699786729125    1.6000162976327]
    >>> expm([[1+j, 0], [1+j,1]])
    [(1.46869393991589 + 2.28735528717884j)                        0.0]
    [  (1.03776739863568 + 3.536943175722j)  (2.71828182845905 + 0.0j)]

Matrices with large entries are allowed::

    >>> expm(matrix([[1,2],[2,3]])**25)
    [5.65024064048415e+2050488462815550  9.14228140091932e+2050488462815550]
    [9.14228140091932e+2050488462815550  1.47925220414035e+2050488462815551]

The identity `\exp(A+B) = \exp(A) \exp(B)` does not hold for
noncommuting matrices::

    >>> A = hilbert(3)
    >>> B = A + eye(3)
    >>> chop(mnorm(A*B - B*A))
    0.0
    >>> chop(mnorm(expm(A+B) - expm(A)*expm(B)))
    0.0
    >>> B = A + ones(3)
    >>> mnorm(A*B - B*A)
    1.8
    >>> mnorm(expm(A+B) - expm(A)*expm(B))
    42.0927851137247

pader         ?)r   matrixr   r+   r   r   r   r   r   r	   r   )
r   Amethodr   resr!   tolTYr(   s
   &&&       r   expmMatrixCalculusMethods.expm5   so   z V88D JJqMAaffH$mmA&JJJqMxxAswwsyy51234	ST3Y	HHQqS H77(C!Q$AA1qAA!CGGAJ,''99Q&,QAYC  H	Q1  , Hs   AF/ CF: /F7:Gc                T   RV P                  WP                  ,          4      V P                  WP                  ) ,          4      ,           ,          p\        VP                  V P                  4      P                  \
        4      4      '       g   VP                  V P                  4      pV# )aL  
Gives the cosine of a square matrix `A`, defined in analogy
with the matrix exponential.

Examples::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> X = eye(3)
    >>> cosm(X)
    [0.54030230586814               0.0               0.0]
    [             0.0  0.54030230586814               0.0]
    [             0.0               0.0  0.54030230586814]
    >>> X = hilbert(3)
    >>> cosm(X)
    [ 0.424403834569555  -0.316643413047167  -0.221474945949293]
    [-0.316643413047167   0.820646708837824  -0.127183694770039]
    [-0.221474945949293  -0.127183694770039   0.909236687217541]
    >>> X = matrix([[1+j,-2],[0,-j]])
    >>> cosm(X)
    [(0.833730025131149 - 0.988897705762865j)  (1.07485840848393 - 0.17192140544213j)]
    [                                     0.0               (1.54308063481524 + 0.0j)]
r/   r7   r!   sumapplyimabsrer   r1   Bs   && r   cosmMatrixCalculusMethods.cosm   sk    0 388AeeG$sxxEE6
';;<177366?((-..Ar   c                T   RV P                  WP                  ,          4      V P                  WP                  ) ,          4      ,
          ,          p\        VP                  V P                  4      P                  \
        4      4      '       g   VP                  V P                  4      pV# )aL  
Gives the sine of a square matrix `A`, defined in analogy
with the matrix exponential.

Examples::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> X = eye(3)
    >>> sinm(X)
    [0.841470984807897                0.0                0.0]
    [              0.0  0.841470984807897                0.0]
    [              0.0                0.0  0.841470984807897]
    >>> X = hilbert(3)
    >>> sinm(X)
    [0.711608512150994  0.339783913247439  0.220742837314741]
    [0.339783913247439  0.244113865695532  0.187231271174372]
    [0.220742837314741  0.187231271174372  0.155816730769635]
    >>> X = matrix([[1+j,-2],[0,-j]])
    >>> sinm(X)
    [(1.29845758141598 + 0.634963914784736j)  (-1.96751511930922 + 0.314700021761367j)]
    [                                    0.0                  (0.0 - 1.1752011936438j)]
y             r:   r@   s   && r   sinmMatrixCalculusMethods.sinm   sk    0 sxx%%(388AvJ+??@177366?((-..Ar   c                    V P                   R ,          pV P                  W1,          V4      V P                  V4      ,          # )g333333?)r!   sqrtmsqrt)r   r1   _may_rotateus   &&& r   
_sqrtm_rot MatrixCalculusMethods._sqrtm_rot   s1     EE3Jyyk*SXXa[88r   c                   V P                  V4      pV^ ,          V8X  d   V# V P                  pV'       dp   V P                  V4      p\        V P	                  V4      4      ^V P
                  ,          8  d/   V P                  V4      ^ 8  d   V P                  W^,
          4      #  V ;P                  ^
,          un        V P
                  ^,          pTpV^ ,          ;rx^ p	 Tp
 RW`P                  V4      ,           ,          RWpP                  V4      ,           ,          rvT P                  Yj,
          R4      pT P                  TR4      pYT,          8:  d   MaT'       d3   T	^8  d,   YR,          8  g   T P                  Y^,
          4      Y0n        # T	^,          p	YP                  8  g   K  T P                  hY0n        T^,          pT#   \         d&    T'       d   T P                  Y^,
          4      p K?  h i ; i  Y0n        i ; i)ag  
Computes a square root of the square matrix `A`, i.e. returns
a matrix `B = A^{1/2}` such that `B^2 = A`. The square root
of a matrix, if it exists, is not unique.

**Examples**

Square roots of some simple matrices::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> sqrtm([[1,0], [0,1]])
    [1.0  0.0]
    [0.0  1.0]
    >>> sqrtm([[0,0], [0,0]])
    [0.0  0.0]
    [0.0  0.0]
    >>> sqrtm([[2,0],[0,1]])
    [1.4142135623731  0.0]
    [            0.0  1.0]
    >>> sqrtm([[1,1],[1,0]])
    [ (0.920442065259926 - 0.21728689675164j)  (0.568864481005783 + 0.351577584254143j)]
    [(0.568864481005783 + 0.351577584254143j)  (0.351577584254143 - 0.568864481005783j)]
    >>> sqrtm([[1,0],[0,1]])
    [1.0  0.0]
    [0.0  1.0]
    >>> sqrtm([[-1,0],[0,1]])
    [(0.0 - 1.0j)           0.0]
    [         0.0  (1.0 + 0.0j)]
    >>> sqrtm([[j,0],[0,j]])
    [(0.707106781186547 + 0.707106781186547j)                                       0.0]
    [                                     0.0  (0.707106781186547 + 0.707106781186547j)]

A square root of a rotation matrix, giving the corresponding
half-angle rotation matrix::

    >>> t1 = 0.75
    >>> t2 = t1 * 0.5
    >>> A1 = matrix([[cos(t1), -sin(t1)], [sin(t1), cos(t1)]])
    >>> A2 = matrix([[cos(t2), -sin(t2)], [sin(t2), cos(t2)]])
    >>> sqrtm(A1)
    [0.930507621912314  -0.366272529086048]
    [0.366272529086048   0.930507621912314]
    >>> A2
    [0.930507621912314  -0.366272529086048]
    [0.366272529086048   0.930507621912314]

The identity `(A^2)^{1/2} = A` does not necessarily hold::

    >>> A = matrix([[4,1,4],[7,8,9],[10,2,11]])
    >>> sqrtm(A**2)
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]
    >>> sqrtm(A)**2
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]
    >>> A = matrix([[-4,1,4],[7,-8,9],[10,2,11]])
    >>> sqrtm(A**2)
    [  7.43715112194995  -0.324127569985474   1.8481718827526]
    [-0.251549715716942    9.32699765900402  2.48221180985147]
    [  4.11609388833616   0.775751877098258   13.017955697342]
    >>> chop(sqrtm(A)**2)
    [-4.0   1.0   4.0]
    [ 7.0  -8.0   9.0]
    [10.0   2.0  11.0]

For some matrices, a square root does not exist::

    >>> sqrtm([[0,1], [0,0]])
    Traceback (most recent call last):
      ...
    ZeroDivisionError: matrix is numerically singular

Two examples from the documentation for Matlab's ``sqrtm``::

    >>> mp.dps = 15; mp.pretty = True
    >>> sqrtm([[7,10],[15,22]])
    [1.56669890360128  1.74077655955698]
    [2.61116483933547  4.17786374293675]
    >>>
    >>> X = matrix(\
    ...   [[5,-4,1,0,0],
    ...   [-4,6,-4,1,0],
    ...   [1,-4,6,-4,1],
    ...   [0,1,-4,6,-4],
    ...   [0,0,1,-4,5]])
    >>> Y = matrix(\
    ...   [[2,-1,-0,-0,-0],
    ...   [-1,2,-1,0,-0],
    ...   [0,-1,2,-1,0],
    ...   [-0,0,-1,2,-1],
    ...   [-0,-0,-0,-1,2]])
    >>> mnorm(sqrtm(X) - Y)
    4.53155328326114e-19

r/   r   gMbP?)r0   r   detr>   r=   r   r?   rL   inverseZeroDivisionErrorr   NoConvergence)r   r1   rJ   r   dr4   r6   ZIr(   Yprevmag1mag2s   &&&          r   rH   MatrixCalculusMethods.sqrtm   s   F JJqMQ3!8Hxx
A366!9~377
*svvay1}~~aQ77	HHNH''C-CAqDLAA++a. 013++a.8H3Iq yy%0yyE*8#1q5u1D>>!];
 H	 Qxx<+++H	Q% ) "NN1!m<  HsH   &=G< $;G	 ?G< +G< G< ,G< 	G9G94G< 7G99G< <Hc                   V P                  V4      pV P                  p V ;P                  ^
,          un        V P                  ^,          pV^ ,          pTp^ p V P                  V4      pV^,          pV P	                  WT,
          R4      R8  g   K:   YT,
          ;rxT^ ,          p	^p
 T
^,          '       d   YT
,          ,          p	MYT
,          ,          p	Yx,          pT P	                  TR4      T8  d   M'T
^,          p
YP                  8  g   Kj  T P
                  hY n        T	^T,          ,          p	T	#   Y n        i ; i)a
  
Computes a logarithm of the square matrix `A`, i.e. returns
a matrix `B = \log(A)` such that `\exp(B) = A`. The logarithm
of a matrix, if it exists, is not unique.

**Examples**

Logarithms of some simple matrices::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> X = eye(3)
    >>> logm(X)
    [0.0  0.0  0.0]
    [0.0  0.0  0.0]
    [0.0  0.0  0.0]
    >>> logm(2*X)
    [0.693147180559945                0.0                0.0]
    [              0.0  0.693147180559945                0.0]
    [              0.0                0.0  0.693147180559945]
    >>> logm(expm(X))
    [1.0  0.0  0.0]
    [0.0  1.0  0.0]
    [0.0  0.0  1.0]

A logarithm of a complex matrix::

    >>> X = matrix([[2+j, 1, 3], [1-j, 1-2*j, 1], [-4, -5, j]])
    >>> B = logm(X)
    >>> nprint(B)
    [ (0.808757 + 0.107759j)    (2.20752 + 0.202762j)   (1.07376 - 0.773874j)]
    [ (0.905709 - 0.107795j)  (0.0287395 - 0.824993j)  (0.111619 + 0.514272j)]
    [(-0.930151 + 0.399512j)   (-2.06266 - 0.674397j)  (0.791552 + 0.519839j)]
    >>> chop(expm(B))
    [(2.0 + 1.0j)           1.0           3.0]
    [(1.0 - 1.0j)  (1.0 - 2.0j)           1.0]
    [        -4.0          -5.0  (0.0 + 1.0j)]

A matrix `X` close to the identity matrix, for which
`\log(\exp(X)) = \exp(\log(X)) = X` holds::

    >>> X = eye(3) + hilbert(3)/4
    >>> X
    [              1.25             0.125  0.0833333333333333]
    [             0.125  1.08333333333333              0.0625]
    [0.0833333333333333            0.0625                1.05]
    >>> logm(expm(X))
    [              1.25             0.125  0.0833333333333333]
    [             0.125  1.08333333333333              0.0625]
    [0.0833333333333333            0.0625                1.05]
    >>> expm(logm(X))
    [              1.25             0.125  0.0833333333333333]
    [             0.125  1.08333333333333              0.0625]
    [0.0833333333333333            0.0625                1.05]

A logarithm of a rotation matrix, giving back the angle of
the rotation::

    >>> t = 3.7
    >>> A = matrix([[cos(t),sin(t)],[-sin(t),cos(t)]])
    >>> chop(logm(A))
    [             0.0  -2.58318530717959]
    [2.58318530717959                0.0]
    >>> (2*pi-t)
    2.58318530717959

For some matrices, a logarithm does not exist::

    >>> logm([[1,0], [0,0]])
    Traceback (most recent call last):
      ...
    ZeroDivisionError: matrix is numerically singular

Logarithm of a matrix with large entries::

    >>> logm(hilbert(3) * 10**20).apply(re)
    [ 45.5597513593433  1.27721006042799  0.317662687717978]
    [ 1.27721006042799  42.5222778973542   2.24003708791604]
    [0.317662687717978  2.24003708791604    42.395212822267]

r   g      ?)r0   r   r   rH   r   rR   )r   r1   r   r4   rU   rA   nr5   XLr(   s   &&         r   logmMatrixCalculusMethods.logm^  s   d JJqMxx	HHNH''C-C1AAAIIaLQ99QS%(50CKA!AAq55QJAQJA99Q&,Qxx<+++H	QT	 Hs   A.D5 $D5 7AD5 D5 5D=c                   V P                  V4      pV P                  V4      pV P                  p V ;P                  ^
,          un        V P                  V4      '       d   V\	        V4      ,          pMoV P                  V^,          4      '       d,   \	        V^,          4      pV P                  V4      V,          pM&V P                  W P                  V4      ,          4      pW0n        V^,          pV#   Y0n        i ; i)a  
Computes `A^r = \exp(A \log r)` for a matrix `A` and complex
number `r`.

**Examples**

Powers and inverse powers of a matrix::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> A = matrix([[4,1,4],[7,8,9],[10,2,11]])
    >>> powm(A, 2)
    [ 63.0  20.0   69.0]
    [174.0  89.0  199.0]
    [164.0  48.0  179.0]
    >>> chop(powm(powm(A, 4), 1/4.))
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]
    >>> powm(extraprec(20)(powm)(A, -4), -1/4.)
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]
    >>> chop(powm(powm(A, 1+0.5j), 1/(1+0.5j)))
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]
    >>> powm(extraprec(5)(powm)(A, -1.5), -1/(1.5))
    [ 4.0  1.0   4.0]
    [ 7.0  8.0   9.0]
    [10.0  2.0  11.0]

A Fibonacci-generating matrix::

    >>> powm([[1,1],[1,0]], 10)
    [89.0  55.0]
    [55.0  34.0]
    >>> fib(10)
    55.0
    >>> powm([[1,1],[1,0]], 6.5)
    [(16.5166626964253 - 0.0121089837381789j)  (10.2078589271083 + 0.0195927472575932j)]
    [(10.2078589271083 + 0.0195927472575932j)  (6.30880376931698 - 0.0317017309957721j)]
    >>> (phi**6.5 - (1-phi)**6.5)/sqrt(5)
    (10.2078589271083 - 0.0195927472575932j)
    >>> powm([[1,1],[1,0]], 6.2)
    [ (14.3076953002666 - 0.008222855781077j)  (8.81733464837593 + 0.0133048601383712j)]
    [(8.81733464837593 + 0.0133048601383712j)  (5.49036065189071 - 0.0215277159194482j)]
    >>> (phi**6.2 - (1-phi)**6.2)/sqrt(5)
    (8.81733464837593 - 0.0133048601383712j)

)r0   convertr   isintr   rH   r7   r^   )r   r1   rr   vys   &&&   r   powmMatrixCalculusMethods.powm  s    h JJqMKKNxx
	HHNHyy||QK1Q3!HIIaLA%HHQxx{]+H	Q Hs   /C3  0C3 AC3 3C; N)taylorr   )__name__
__module____qualname____firstlineno__r+   r7   rB   rE   rL   rH   r^   rf   __static_attributes____classdictcell__)__classdict__s   @r   r   r      s;     ,\\|::9IVpdC Cr   r   N)libmp.backendr   objectr   rh   r   r   <module>rs      s    "NF Nr   