Advertisement
Guest User

Eugene Tan

a guest
Sep 7th, 2009
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import caesar
  2. import euclid
  3.  
  4. def determinant22(matrix):
  5.     """Return determinant of a 2x2 matrix
  6.  
  7.    >>> determinant22([[2, 4], [-2, 1]])
  8.    10
  9.    """
  10.     return (matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1])
  11.  
  12. def determinant33(matrix):
  13.     """Return determinant of a 3x3 matrix
  14.  
  15.    >>> determinant33([[2, 4, 3], [6, 1, 5], [-2, 1, 3]])
  16.    -92
  17.    """
  18.     m = matrix
  19.     return (m[0][0] * m[1][1] * m[2][2]) + (m[0][1] * m[1][2] * m[2][0]) + (m[0][2] * m[1][0] * m[2][1]) \
  20.         - (m[2][0] * m[1][1] * m[0][2]) - (m[2][1] * m[1][2] * m[0][0]) - (m[2][2] * m[1][0] * m[0][1])
  21.  
  22. def determinant(matrix):
  23.     """Returns the determinant of a nxn matrix
  24.  
  25.    Doesn't work for matrices above 3x3
  26.  
  27.    >>> matrix = [[3, 2, 0, 1], [4, 0, 1, 2], [3, 0, 2, 1], [9, 2, 3, 1]]
  28.    >>> determinant(matrix)
  29.    Traceback (most recent call last):
  30.        ...
  31.    NameError: unsupported
  32.  
  33.    Should be 24
  34.    """
  35.     if len(matrix) == 1:
  36.         return matrix[0][0]
  37.     if len(matrix) == 2:
  38.         return determinant22(matrix)
  39.     if len(matrix) == 3:
  40.         return determinant33(matrix)
  41.     # can't figure out how to solve for nxn matrix
  42.     raise NameError('unsupported')
  43.  
  44. def cofactor(matrix, i, j):
  45.     """Find cofactor of a matrix
  46.    >>> matrix = [[2, 4, 3], [6, 1, 5], [-2, 1, 3]]
  47.    >>> cofactor(matrix, 1, 2)
  48.    -10
  49.    >>> len(matrix)
  50.    3
  51.    >>> len(matrix[0])
  52.    3
  53.    """
  54.     m = [x[:] for x in matrix]
  55.     del m[i]
  56.     for row in m:
  57.         del row[j]
  58.     sign = 1 if (i + j) % 2 == 0 else -1
  59.     return determinant(m) * sign
  60.  
  61. def transpose(matrix):
  62.     """Transpose a matrix
  63.  
  64.    >>> transpose([[17, 17, 5], [21, 18, 21], [2, 2, 19]])
  65.    [[17, 21, 2], [17, 18, 2], [5, 21, 19]]
  66.    """
  67.     n = len(matrix)
  68.     for i in range(n):
  69.         for j in range(n):
  70.             if j > i:
  71.                 matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
  72.     return matrix
  73.  
  74. def inv(key):
  75.     """Find the inverse of a hill cipher key
  76.  
  77.    >>> inv([[17, 17, 5], [21, 18, 21], [2, 2, 19]])
  78.    [[4, 9, 15], [15, 17, 6], [24, 0, 17]]
  79.    >>> inv([[5, 8], [17, 3]])
  80.    [[9, 2], [1, 15]]
  81.    """
  82.     d = euclid.eeuclid1(determinant(key) % 26, 26, False)
  83.     m = len(key)
  84.     if key == 0:
  85.         return None
  86.     b = []
  87.     for i in range(m):
  88.         row = []
  89.         for j in range(m):
  90.             row.append(int(cofactor(key, j, i) * d) % 26)
  91.         b.append(row)
  92.     return b
  93.  
  94. def hill(text, key, decrypt=False):
  95.     """Hill cipher
  96.  
  97.    >>> key = [[17, 17, 5], [21, 18, 21], [2, 2, 19]]
  98.    >>> plaintext = "PAYMOREMONEY"
  99.    >>> ciphertext = hill(plaintext, key)
  100.    >>> ciphertext
  101.    'LNSHDLEWMTRW'
  102.    >>> hill(ciphertext, key, True)
  103.    'PAYMOREMONEY'
  104.    """
  105.     m = len(key)
  106.     if decrypt:
  107.         key = inv(key)
  108.     l = caesar.alpha_to_int(text.replace(' ', 'a'))
  109.     result = []
  110.     for k in range(len(text) / m):
  111.         p = l[0:m]
  112.         l = l[m:]
  113.         for i in range(m):
  114.             c = 0
  115.             for j in range(m):
  116.                 c += p[j] * key[i][j]
  117.             result.append(c % 26)
  118.     return ''.join(caesar.int_to_alpha(result)).upper()
  119.  
  120. if __name__ == '__main__':
  121.     import doctest
  122.     doctest.testmod()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement