Exemplo n.º 1
0
def fixed_lag_smoothing(e_t, HMM, d, ev, t):
    """[Figure 15.6]
    Smoothing algorithm with a fixed time lag of 'd' steps.
    Online algorithm that outputs the new smoothed estimate if observation
    for new time step is given."""
    ev.insert(0, None)

    T_model = HMM.transition_model
    f = HMM.prior
    B = [[1, 0], [0, 1]]
    evidence = []

    evidence.append(e_t)
    O_t = vector_to_diagonal(HMM.sensor_dist(e_t))
    if t > d:
        f = forward(HMM, f, e_t)
        O_tmd = vector_to_diagonal(HMM.sensor_dist(ev[t- d]))
        B = matrix_multiplication(inverse_matrix(O_tmd), inverse_matrix(T_model), B, T_model, O_t)
    else:
        B = matrix_multiplication(B, T_model, O_t)
    t = t + 1

    if t > d:
        # always returns a 1x2 matrix
        return([normalize(i) for i in matrix_multiplication([f], B)][0])
    else:
        return None
def fixed_lag_smoothing(e_t, HMM, d, ev, t):
    """
    [Figure 15.6]
    Smoothing algorithm with a fixed time lag of 'd' steps.
    Online algorithm that outputs the new smoothed estimate if observation
    for new time step is given."""
    ev.insert(0, None)

    T_model = HMM.transition_model
    f = HMM.prior
    B = [[1, 0], [0, 1]]
    evidence = []

    evidence.append(e_t)
    O_t = vector_to_diagonal(HMM.sensor_dist(e_t))
    if t > d:
        f = forward(HMM, f, e_t)
        O_tmd = vector_to_diagonal(HMM.sensor_dist(ev[t - d]))
        B = matrix_multiplication(inverse_matrix(O_tmd), inverse_matrix(T_model), B, T_model, O_t)
    else:
        B = matrix_multiplication(B, T_model, O_t)
    t += 1

    if t > d:
        # always returns a 1x2 matrix
        return [normalize(i) for i in matrix_multiplication([f], B)][0]
    else:
        return None
Exemplo n.º 3
0
def fixed_lag_smoothing(e_t, HMM, d, ev, t):
    """Algoritmo de suavização com um intervalo de tempo fixo de passos 'd'.
     Algoritmo online que produz a nova estimativa suavizada se a observação
     Para novo passo de tempo é dado."""
    ev.insert(0, None)

    T_model = HMM.transition_model
    f = HMM.prior
    B = [[1, 0], [0, 1]]
    evidence = []

    evidence.append(e_t)
    O_t = vector_to_diagonal(HMM.sensor_dist(e_t))
    if t > d:
        f = forward(HMM, f, e_t)
        O_tmd = vector_to_diagonal(HMM.sensor_dist(ev[t - d]))
        B = matrix_multiplication(inverse_matrix(O_tmd),
                                  inverse_matrix(T_model), B, T_model, O_t)
    else:
        B = matrix_multiplication(B, T_model, O_t)
    t = t + 1

    if t > d:
        return [normalize(i) for i in matrix_multiplication([f], B)][0]
    else:
        return None
Exemplo n.º 4
0
def encrypt(message, matrix, encryption=True):
    """
    Hill encryption (decryption).
    """
    message = message.upper()
    if not utils.invertible(matrix):
        # The matrix should be invertible.
        return "Non invertible matrix"
    if len(message) % 2 != 0:
        message = message + 'X'
    couple = [
        list(message[i * 2:(i * 2) + 2]) for i in range(0,
                                                        len(message) / 2)
    ]
    result = [i[:] for i in couple]
    if not encryption:
        # To decrypt, just need to inverse the matrix.
        matrix = utils.inverse_matrix(matrix)
    for i, c in enumerate(couple):
        if c[0].isalpha() and c[1].isalpha():
            result[i][0] = chr(((ord(c[0])-65) * matrix[0][0] + \
                                    (ord(c[1])-65) * matrix[0][1]) % 26 + 65)
            result[i][1] = chr(((ord(c[0])-65) * matrix[1][0] + \
                                    (ord(c[1])-65) * matrix[1][1]) % 26 + 65)
    return "".join(["".join(i) for i in result])
Exemplo n.º 5
0
def hill(message, matrix, encryption = False):

    import utils

    if not utils.invertible(matrix):
        # The matrix should be invertible.
        raise Exception, "Non invertible matrix. Please enter a matrix that is invertible."

    try:
        if len(message) % 2 != 0:
            message = message + 'X'
        couple = [list(message[i*2:(i*2)+2]) for i in range(0, len(message)/2)]
        result = [i[:] for i in couple]
        if not encryption:
            # To decrypt, just need to inverse the matrix.
            matrix = utils.inverse_matrix(matrix)
        for i, c in enumerate(couple):
            if c[0].isalpha() and c[1].isalpha():
                result[i][0] = chr(((ord(c[0])-65) * matrix[0][0] + \
                                        (ord(c[1])-65) * matrix[0][1]) % 26 + 65)
                result[i][1] = chr(((ord(c[0])-65) * matrix[1][0] + \
                                        (ord(c[1])-65) * matrix[1][1]) % 26 + 65)
        return "".join(["".join(i) for i in result])

    except:
        raise Exception, "Input is not valid. Please enter valid matrix."
Exemplo n.º 6
0
def encrypt(message, matrix, encryption=True):
    
    message = message.upper()
    if not utils.invertible(matrix):
        # The matrix should be invertible.
        return "Non invertible matrix"
    if len(message) % 2 != 0:
        message = message + 'X'
    couple = [list(message[i*2:(i*2)+2]) for i in range(0, len(message)/2)]
    result = [i[:] for i in couple]
    if not encryption:
        # To decrypt, just need to inverse the matrix.
        matrix = utils.inverse_matrix(matrix)
    for i, c in enumerate(couple):
        if c[0].isalpha() and c[1].isalpha():
            result[i][0] = chr(((ord(c[0])-65) * matrix[0][0] + \
                                    (ord(c[1])-65) * matrix[0][1]) % 26 + 65)
            result[i][1] = chr(((ord(c[0])-65) * matrix[1][0] + \
                                    (ord(c[1])-65) * matrix[1][1]) % 26 + 65)
    return "".join(["".join(i) for i in result])
Exemplo n.º 7
0
def condition_number(m):
    return matrix_rate(m) * matrix_rate(inverse_matrix(m))