Exemple #1
0
def expms(m, context=FloatContext, order=7):
    """Calculate quickly exp(t*A), for different t.
    Takes matrix M and returns function f(t) = exp(t*M).
    
    Each call to that function requires no matrix multiplications and several additions
    """
    a = _exp_pade_coeffs(order)
    n, n_ = shape_mat(m)
    assert (n == n_)

    #Prepare list of m powers and Pade coefficients
    m_a_list = zip(matrix_powers(m), a)

    def calculate_expm(t):
        num = zeros(n, n)
        den = zeros(n, n)
        s = 1
        k = context.one  # t^n is stored here
        for m_i, ai in m_a_list:
            aik = ai * k
            mat_combine_inplace(num, m_i, aik)
            mat_combine_inplace(den, m_i, s * aik)
            s = -s
            k *= t
        return solve(den, num)

    return calculate_expm
Exemple #2
0
def qr_inverse(M, context=FloatContext, eps=1e-14):
    """Matrix inverse, using QR transformation"""
    n, n_ = shape_mat(M)
    assert (n == n_)
    IQ, R = qrl_givens(M, context=context, eps=eps, inverse_q=True)
    IR = ltri_inverse(R, context=context)
    return mmul(IR, IQ)
Exemple #3
0
def expms( m, context=FloatContext, order=7 ):
    """Calculate quickly exp(t*A), for different t.
    Takes matrix M and returns function f(t) = exp(t*M).
    
    Each call to that function requires no matrix multiplications and several additions
    """
    a = _exp_pade_coeffs( order )
    n,n_ = shape_mat( m )
    assert( n == n_ )
    
    #Prepare list of m powers and Pade coefficients
    m_a_list = zip( matrix_powers(m), a )

    def calculate_expm(t):
        num = zeros(n,n)
        den = zeros(n,n)
        s = 1
        k = context.one # t^n is stored here
        for m_i, ai in m_a_list:
            aik = ai*k
            mat_combine_inplace( num, m_i, aik )
            mat_combine_inplace( den, m_i, s*aik)
            s = -s
            k *= t
        return solve( den, num )
    return calculate_expm
Exemple #4
0
def qr_inverse( M, context=FloatContext, eps=1e-14):
    """Matrix inverse, using QR transformation"""
    n,n_ = shape_mat(M)
    assert(n==n_)
    IQ,R = qrl_givens(M, context=context, eps=eps, inverse_q = True)
    IR = ltri_inverse(R, context=context)
    return mmul( IR, IQ )
Exemple #5
0
def expm(m, context=FloatContext, order=7):
    """Matrix exponent, using Pade approximation of given order
    """
    a = _exp_pade_coeffs(order)
    n, n_ = shape_mat(m)
    assert (n == n_)

    num = zeros(n, n)
    den = zeros(n, n)

    s = 1
    for m_i, ai in izip(matrix_powers(m), a):
        mat_combine_inplace(num, m_i, ai)
        mat_combine_inplace(den, m_i, s * ai)
        s = -s

    return solve(den, num)
Exemple #6
0
def expm( m, context=FloatContext, order=7 ):
    """Matrix exponent, using Pade approximation of given order
    """
    a = _exp_pade_coeffs( order )
    n,n_ = shape_mat( m )
    assert( n == n_ )
    
    num = zeros(n,n)
    den = zeros(n,n)

    s = 1
    for m_i, ai in izip( matrix_powers(m), a ):
        mat_combine_inplace( num, m_i, ai )
        mat_combine_inplace( den, m_i, s*ai)
        s = -s

    return solve( den, num )
Exemple #7
0
def qrl_givens(M, eps=1e-14, context=FloatContext, inverse_q=False):
    """QR decomposition of a rectangular matrix. R is right-triangular"""
    n, m = shape_mat(M)
    R = copy_mat(M)  #will be used for inplace computations
    Q = eye(n, context=context)
    for j in xrange(min(n, m) - 1, 0, -1):  #from last column to the first
        for i in xrange(j):
            k_sin = R[i][j]
            k_cos = R[i + 1][j]

            k2 = k_sin**2 + k_cos**2
            if k2 < eps: continue
            k = context.sqrt(k2)
            s = k_sin / k
            c = k_cos / k

            givens_inplace(Q, i, i + 1, -s, c)
            givens_inplace(R, i, i + 1, -s, c)
    if inverse_q: return Q, R
    else: return transpose(Q), R
Exemple #8
0
def qrl_givens(M, eps=1e-14, context=FloatContext, inverse_q=False ):
    """QR decomposition of a rectangular matrix. R is right-triangular"""
    n,m = shape_mat(M)
    R = copy_mat(M) #will be used for inplace computations
    Q = eye( n, context=context )
    for j in xrange(min(n,m)-1,0,-1): #from last column to the first
        for i in xrange(j):
            k_sin = R[i  ][j]
            k_cos = R[i+1][j]
            
            k2 = k_sin**2 + k_cos**2
            if k2 < eps: continue
            k = context.sqrt(k2)
            s = k_sin / k
            c = k_cos / k
            
            givens_inplace( Q, i, i+1, -s, c )
            givens_inplace( R, i, i+1, -s, c )
    if inverse_q: return Q,R
    else: return transpose(Q), R