docstr: 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
docstr_tok: ["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"]
code: def expm(ctx, A, method="taylor"):
      r"""
      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
  
      """
      if method == "pade":
          prec = ctx.prec
          try:
              A = ctx.matrix(A)
              ctx.prec += 2 * A.rows
              res = ctx._exp_pade(A)
          finally:
              ctx.prec = prec
          return res
      A = ctx.matrix(A)
      prec = ctx.prec
      j = int(max(1, ctx.mag(ctx.mnorm(A, "inf"))))
      j += int(0.5 * prec**0.5)
      try:
          ctx.prec += 10 + 2 * j
          tol = +ctx.eps
          A = A / 2**j
          T = A
          Y = A**0 + A
          k = 2
          while 1:
              T *= A * (1 / ctx.mpf(k))
              if ctx.mnorm(T, "inf") < tol:
                  break
              Y += T
              k += 1
          for k in xrange(j):
              Y = Y * Y
      finally:
          ctx.prec = prec
      Y *= 1
      return Y
  
file_path: catboost/contrib/python/mpmath/py3/mpmath/matrices/calculus.py
imports: from ..libmp.backend import xrange
label_desc: Use image node_id 2 for calling the MatrixCalculusMethods obj's underlying member method code with example usage: obj.expm(ctx, A, method) and returns: Y, res