Ejemplo n.º 1
0
def log_inv(X):  # inverts a 3x3 matrix given by the logscale values
    if (X.shape[0] != X.shape[1]):
        raise Exception("X is not a square matrix and cannot be inverted")

    if (X.shape[0] == 1):
        return matrix((-X[0, 0]))

    ldet = log_det(X)
    if (ldet == nan):
        raise Exception(
            "The determinant of X is 0, cannot calculate the inverse")

    if (X.shape[0] == 2):  # X is a 2x2 matrix
        I = (-log_det(X)) * ones((2, 2))
        I[0, 0] += X[1, 1]
        I[0, 1] += X[0, 1] + complex(0, pi)
        I[1, 0] += X[1, 0] + complex(0, pi)
        I[1, 1] += X[0, 0]
        return I

    if (X.shape[0] == 3):  # X is a 3x3 matrix
        I = (-log_det(X)) * ones((3, 3))
        I[0, 0] += log_subt_exp(X[1, 1] + X[2, 2], X[1, 2] + X[2, 1])
        I[0, 1] += log_subt_exp(X[0, 2] + X[2, 1], X[0, 1] + X[2, 2])
        I[0, 2] += log_subt_exp(X[0, 1] + X[1, 2], X[0, 2] + X[1, 1])
        I[1, 0] += log_subt_exp(X[2, 0] + X[1, 2], X[1, 0] + X[2, 2])
        I[1, 1] += log_subt_exp(X[0, 0] + X[2, 2], X[0, 2] + X[2, 0])
        I[1, 2] += log_subt_exp(X[0, 2] + X[1, 0], X[0, 0] + X[1, 2])
        I[2, 0] += log_subt_exp(X[1, 0] + X[2, 1], X[2, 0] + X[1, 1])
        I[2, 1] += log_subt_exp(X[2, 0] + X[0, 1], X[0, 0] + X[2, 1])
        I[2, 2] += log_subt_exp(X[0, 0] + X[1, 1], X[0, 1] + X[1, 0])
        return I

    raise Exception("log_inv is only implemented for matrices of size < 4")
Ejemplo n.º 2
0
def log_inv(X): # inverts a 3x3 matrix given by the logscale values
    if (X.shape[0] != X.shape[1]):
        raise Exception("X is not a square matrix and cannot be inverted")
    
    if (X.shape[0] == 1):
        return matrix((-X[0,0]))
    
    ldet = log_det(X)
    if (ldet == nan):
        raise Exception("The determinant of X is 0, cannot calculate the inverse")
     
    if (X.shape[0] == 2): # X is a 2x2 matrix
        I = (-log_det(X)) * ones((2,2))
        I[0,0] += X[1,1]
        I[0,1] += X[0,1] + complex(0, pi)
        I[1,0] += X[1,0] + complex(0, pi)
        I[1,1] += X[0,0]
        return I
    
    if (X.shape[0] == 3): # X is a 3x3 matrix
        I = (-log_det(X)) * ones((3,3))
        I[0,0] += log_subt_exp(X[1,1]+X[2,2], X[1,2]+X[2,1])
        I[0,1] += log_subt_exp(X[0,2]+X[2,1], X[0,1]+X[2,2])
        I[0,2] += log_subt_exp(X[0,1]+X[1,2], X[0,2]+X[1,1])
        I[1,0] += log_subt_exp(X[2,0]+X[1,2], X[1,0]+X[2,2])
        I[1,1] += log_subt_exp(X[0,0]+X[2,2], X[0,2]+X[2,0])
        I[1,2] += log_subt_exp(X[0,2]+X[1,0], X[0,0]+X[1,2])
        I[2,0] += log_subt_exp(X[1,0]+X[2,1], X[2,0]+X[1,1])
        I[2,1] += log_subt_exp(X[2,0]+X[0,1], X[0,0]+X[2,1])
        I[2,2] += log_subt_exp(X[0,0]+X[1,1], X[0,1]+X[1,0])
        return I
    
    raise Exception("log_inv is only implemented for matrices of size < 4")
Ejemplo n.º 3
0
def log_det(X):
    if (X.shape[0] != X.shape[1]):
        raise Exception("X is not a square matrix so its determinant cannot be calculated")
    
    if (X.shape[0] == 1):
        return X[0,0]
    
    if (X.shape[0] == 2): # X is a 2x2 matrix
        return(log_subt_exp(X[0,0]+X[1,1], X[1,0]+X[0,1]))
    
    if (X.shape[0] == 3):    
        s_plus = log_sum_exp([X[0,0]+X[1,1]+X[2,2], X[0,2]+X[1,0]+X[2,1], X[0,1]+X[1,2]+X[2,0]])
        s_minus = log_sum_exp([X[0,1]+X[1,0]+X[2,2], X[0,2]+X[1,1]+X[2,0], X[0,0]+X[1,2]+X[2,1]])
        return log_subt_exp(s_plus, s_minus)

    raise Exception("log_det is only implemented for matrices of size < 4")
Ejemplo n.º 4
0
def log_det(X):
    if (X.shape[0] != X.shape[1]):
        raise Exception(
            "X is not a square matrix so its determinant cannot be calculated")

    if (X.shape[0] == 1):
        return X[0, 0]

    if (X.shape[0] == 2):  # X is a 2x2 matrix
        return (log_subt_exp(X[0, 0] + X[1, 1], X[1, 0] + X[0, 1]))

    if (X.shape[0] == 3):
        s_plus = log_sum_exp([
            X[0, 0] + X[1, 1] + X[2, 2], X[0, 2] + X[1, 0] + X[2, 1],
            X[0, 1] + X[1, 2] + X[2, 0]
        ])
        s_minus = log_sum_exp([
            X[0, 1] + X[1, 0] + X[2, 2], X[0, 2] + X[1, 1] + X[2, 0],
            X[0, 0] + X[1, 2] + X[2, 1]
        ])
        return log_subt_exp(s_plus, s_minus)

    raise Exception("log_det is only implemented for matrices of size < 4")