<commit_before>#
# Author: <NAME>, March 2002
#

__all__ = ['expm','expm2','expm3','cosm','sinm','tanm','coshm','sinhm',
           'tanhm','logm','funm','signm','sqrtm']

from numpy import asarray, Inf, dot, floor, eye, diag, exp, \
     product, logical_not, ravel, transpose, conjugate, \
     cast, log, ogrid, imag, real, absolute, amax, sign, \
     isfinite, sqrt, identity, single, ceil, log2
from numpy import matrix as mat
import numpy as np

from scipy.sparse.base import isspmatrix
from scipy.sparse.construct import eye as speye

# Local imports
from misc import norm
from basic import solve, inv
from special_matrices import triu, all_mat
from decomp import eig
from decomp_svd import orth, svd
from decomp_schur import schur, rsf2csf
import warnings

eps = np.finfo(float).eps
feps = np.finfo(single).eps

def expm(A, q=False):
    """Compute the matrix exponential using Pade approximation.

    Parameters
    ----------
    A : array, shape(M,M)
        Matrix to be exponentiated

    Returns
    -------
    expA : array, shape(M,M)
        Matrix exponential of A

    References
    ----------
    <NAME>,
    "The Scaling and Squaring Method for the Matrix Exponential Revisited",
    SIAM. J. Matrix Anal. & Appl. 26, 1179 (2005).

    """
    if q: warnings.warn("argument q=... in scipy.linalg.expm is deprecated.")
    A = asarray(A)
    A_L1 = norm(A,1)
    n_squarings = 0

    if A.dtype == 'float64' or A.dtype == 'complex128':
        if A_L1 < 1.495585217958292e-002:
            U,V = _pade3(A)
        elif A_L1 < 2.539398330063230e-001:
            U,V = _pade5(A)
        elif A_L1 < 9.504178996162932e-001:
            U,V = _pade7(A)
        elif A_L1 < 2.097847961257068e+000:
            U,V = _pade9(A)
        else:
            maxnorm = 5.371920351148152
            n_squarings = max(0, int(ceil(log2(A_L1 / maxnorm))))
            A = A / 2**n_squarings
            U,V = _pade13(A)
    elif A.dtype == 'float32' or A.dtype == 'complex64':
        if A_L1 < 4.258730016922831e-001:
            U,V = _pade3(A)
        elif A_L1 < 1.880152677804762e+000:
            U,V = _pade5(A)
        else:
            maxnorm = 3.925724783138660
            n_squarings = max(0, int(ceil(log2(A_L1 / maxnorm))))
            A = A / 2**n_squarings
            U,V = _pade7(A)
    else:
        raise ValueError("invalid type: "+str(A.dtype))

    P = U + V  # p_m(A) : numerator
    Q = -U + V # q_m(A) : denominator

    R = solve(Q,P)
    # squaring step to undo scaling
    for i in range(n_squarings):
        R = dot(R,R)

    return R

# implementation of Pade approximations of various degree using the algorithm presented in [Higham 2005]
# These should apply to both dense and sparse matricies.
def _pade3(A):
    b = (120., 60., 12., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
    A2 = A.dot(A)
    U = A.dot(b[3]*A2 + b[1]*ident)
    V = b[2]*A2 + b[0]*ident
    return U,V

def _pade5(A):
    b = (30240., 15120., 3360., 420., 30., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
    A2 = A.dot(A)
    A4 = A2.dot(A2)
    U = A.dot(b[5]*A4 + b[3]*A2 + b[1]*ident)
    V = b[4]*A4 + b[2]*A2 + b[0]*ident
    return U,V

def _pade5(A):
    b = (30240., 15120., 3360., 420., 30., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
    A2 = A.dot(A)
    A4 = A2.dot(A2)
    U = A.dot(b[5]*A4 + b[3]*A2 + b[1]*ident)
    V = b[4]*A4 + b[2]*A2 + b[0]*ident
    return U,V

def _pade7(A):
    b = (17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)<commit_msg>removed duplicate _pade5()<commit_after>#
# Author: <NAME>, March 2002
#

__all__ = ['expm','expm2','expm3','cosm','sinm','tanm','coshm','sinhm',
           'tanhm','logm','funm','signm','sqrtm']

from numpy import asarray, Inf, dot, floor, eye, diag, exp, \
     product, logical_not, ravel, transpose, conjugate, \
     cast, log, ogrid, imag, real, absolute, amax, sign, \
     isfinite, sqrt, identity, single, ceil, log2
from numpy import matrix as mat
import numpy as np

from scipy.sparse.base import isspmatrix
from scipy.sparse.construct import eye as speye

# Local imports
from misc import norm
from basic import solve, inv
from special_matrices import triu, all_mat
from decomp import eig
from decomp_svd import orth, svd
from decomp_schur import schur, rsf2csf
import warnings

eps = np.finfo(float).eps
feps = np.finfo(single).eps

def expm(A, q=False):
    """Compute the matrix exponential using Pade approximation.

    Parameters
    ----------
    A : array, shape(M,M)
        Matrix to be exponentiated

    Returns
    -------
    expA : array, shape(M,M)
        Matrix exponential of A

    References
    ----------
    <NAME>,
    "The Scaling and Squaring Method for the Matrix Exponential Revisited",
    SIAM. J. Matrix Anal. & Appl. 26, 1179 (2005).

    """
    if q: warnings.warn("argument q=... in scipy.linalg.expm is deprecated.")
    A = asarray(A)
    A_L1 = norm(A,1)
    n_squarings = 0

    if A.dtype == 'float64' or A.dtype == 'complex128':
        if A_L1 < 1.495585217958292e-002:
            U,V = _pade3(A)
        elif A_L1 < 2.539398330063230e-001:
            U,V = _pade5(A)
        elif A_L1 < 9.504178996162932e-001:
            U,V = _pade7(A)
        elif A_L1 < 2.097847961257068e+000:
            U,V = _pade9(A)
        else:
            maxnorm = 5.371920351148152
            n_squarings = max(0, int(ceil(log2(A_L1 / maxnorm))))
            A = A / 2**n_squarings
            U,V = _pade13(A)
    elif A.dtype == 'float32' or A.dtype == 'complex64':
        if A_L1 < 4.258730016922831e-001:
            U,V = _pade3(A)
        elif A_L1 < 1.880152677804762e+000:
            U,V = _pade5(A)
        else:
            maxnorm = 3.925724783138660
            n_squarings = max(0, int(ceil(log2(A_L1 / maxnorm))))
            A = A / 2**n_squarings
            U,V = _pade7(A)
    else:
        raise ValueError("invalid type: "+str(A.dtype))

    P = U + V  # p_m(A) : numerator
    Q = -U + V # q_m(A) : denominator

    R = solve(Q,P)
    # squaring step to undo scaling
    for i in range(n_squarings):
        R = dot(R,R)

    return R

# implementation of Pade approximations of various degree using the algorithm presented in [Higham 2005]
# These should apply to both dense and sparse matricies.
def _pade3(A):
    b = (120., 60., 12., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
    A2 = A.dot(A)
    U = A.dot(b[3]*A2 + b[1]*ident)
    V = b[2]*A2 + b[0]*ident
    return U,V

def _pade5(A):
    b = (30240., 15120., 3360., 420., 30., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
    A2 = A.dot(A)
    A4 = A2.dot(A2)
    U = A.dot(b[5]*A4 + b[3]*A2 + b[1]*ident)
    V = b[4]*A4 + b[2]*A2 + b[0]*ident
    return U,V

def _pade7(A):
    b = (17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.)
    if isspmatrix(A):
        ident = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
    else:
        ident = eye(A.shape[0], A.shape[1], dtype=A.dtype)
{'fraction_non_alphanumeric': 0.15229996134518747, 'fraction_numerical': 0.08426749130266718, 'mean_word_length': 2.3836094158674803, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7453298', 'n_tokens_mistral': 3418, 'n_tokens_neox': 3033, 'n_words': 754}