def expm(A):
<EOL>if isinstance(A, (list, tuple)):<EOL><INDENT>A = np.asarray(A)<EOL><DEDENT>if len(A.shape) != <NUM_LIT:2> or A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>structure = UPPER_TRIANGULAR if _is_upper_triangular(A) else None<EOL>use_exact_onenorm = A.shape[<NUM_LIT:0>] < <NUM_LIT:200><EOL>h = _ExpmPadeHelper(<EOL>A, structure=structure, use_exact_onenorm=use_exact_onenorm)<EOL>eta_1 = max(h.d4_loose, h.d6_loose)<EOL>if eta_1 < <NUM_LIT> and _ell(h.A, <NUM_LIT:3>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade3()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_2 = max(h.d4_tight, h.d6_loose)<EOL>if eta_2 < <NUM_LIT> and _ell(h.A, <NUM_LIT:5>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade5()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_3 = max(h.d6_tight, h.d8_loose)<EOL>if eta_3 < <NUM_LIT> and _ell(h.A, <NUM_LIT:7>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade7()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>if eta_3 < <NUM_LIT> and _ell(h.A, <NUM_LIT:9>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade9()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_4 = max(h.d8_loose, h.d10_loose)<EOL>eta_5 = min(eta_3, eta_4)<EOL>theta_13 = <NUM_LIT><EOL>s = max(int(np.ceil(np.log2(eta_5 / theta_13))), <NUM_LIT:0>)<EOL>s = s + _ell(<NUM_LIT:2>**-s * h.A, <NUM_LIT>)<EOL>U, V = h.pade13_scaled(s)<EOL>X = _solve_P_Q(U, V, structure=structure)<EOL>if structure == UPPER_TRIANGULAR:<EOL><INDENT>X = _fragment_2_1(X, h.A, s)<EOL><DEDENT>else:<EOL><INDENT>for i in range(s):<EOL><INDENT>X = X.dot(X)<EOL><DEDENT><DEDENT>return X<EOL>
Compute the matrix exponential using Pade approximation.

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

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

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

.. versionadded:: 0.12.0

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