def faster_inverse(A):
    b = np.identity(A.shape[1], dtype=A.dtype)
    n_eq = A.shape[0]
    n_rhs = A.shape[1]
    pivots = np.zeros(n_eq, np.intc)
    identity  = np.eye(n_eq)
    results = lapack_lite.zgesv(n_eq, n_rhs, A, n_eq, pivots, b, n_eq, 0) 
    if results['info'] > 0:
        raise LinAlgError('Singular matrix') 
    return b
def regularized_inverse(A, lm):
    """
    This computes the gereralized inverse of A through a 
    regularized solution: (Ah*A + lm^2*I)*C = Ah*I
    
    use CBLAS to get the matrix product of (Ah*A + lm^2*I) in A2,
    and then use LAPACK to solve for C. 
    
    Note: in column-major...
    A is conj(Ah) 
    A2 is conj(Ah*A + lm^2*I) -- since A2 is hermitian symmetric
    
    If conj(AhA + (lm^2)I)*C = conj(Ah), then conj(C) (the conjugate of
    the LAPACK solution, which is in col-major) is the desired solution
    in column-major. So the final answer in row-major is the hermitian
    transpose of C.
    """

    m, n = A.shape
    # Ah is NxM
    Ah = np.empty((n, m), A.dtype)
    Ah[:] = A.transpose().conjugate()

    # A2 is NxN
    A2 = np.dot(Ah, A)
    # add lm**2 to the diagonal (ie, A2 + (lm**2)*I)
    A2.flat[0 : n * n : n + 1] += lm * lm

    pivots = np.zeros(n, intc)

    # A viewed as column major is considered to be NxNRHS (NxM)...
    # the solution will be NxNRHS too, and is stored in A
    results = lapack_lite.zgesv(n, m, A2, n, pivots, A, n, 0)

    # put conjugate solution into row-major (MxN) by hermitian transpose
    Ah[:] = A.transpose().conjugate()
    return Ah
Example #3
0
def regularized_inverse(A, lm):
    """
    This computes the gereralized inverse of A through a 
    regularized solution: (Ah*A + lm^2*I)*C = Ah*I
    
    use CBLAS to get the matrix product of (Ah*A + lm^2*I) in A2,
    and then use LAPACK to solve for C. 
    
    Note: in column-major...
    A is conj(Ah) 
    A2 is conj(Ah*A + lm^2*I) -- since A2 is hermitian symmetric
    
    If conj(AhA + (lm^2)I)*C = conj(Ah), then conj(C) (the conjugate of
    the LAPACK solution, which is in col-major) is the desired solution
    in column-major. So the final answer in row-major is the hermitian
    transpose of C.
    """

    m, n = A.shape
    # Ah is NxM
    Ah = np.empty((n, m), A.dtype)
    Ah[:] = A.transpose().conjugate()

    # A2 is NxN
    A2 = np.dot(Ah, A)
    # add lm**2 to the diagonal (ie, A2 + (lm**2)*I)
    A2.flat[0:n * n:n + 1] += (lm * lm)

    pivots = np.zeros(n, intc)

    # A viewed as column major is considered to be NxNRHS (NxM)...
    # the solution will be NxNRHS too, and is stored in A
    results = lapack_lite.zgesv(n, m, A2, n, pivots, A, n, 0)

    # put conjugate solution into row-major (MxN) by hermitian transpose
    Ah[:] = A.transpose().conjugate()
    return Ah