import types import operator import copy """ Linear Algebra Matrix Class This class was adapted from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189971 Written by Bill McNeill Operations for inverting the matrix were added. This function was adapted from the outline of the algorithm found here: http://en.wikipedia.org/wiki/Gaussian_elimination There were some other minor modification to this class to enable the correct compuation of a finite matrix defined in a finite field. The Matrix class is an implementation of a linear algebra matrix. Arithmetic operations, trace, determinant, and minors are defined for it. This is a lightweight alternative to a numerical Python package for people who need to do basic linear algebra. Vectors are implemented as 1xN and Nx1 matricies. There is no separate vector class. This implementation enforces the distinction between row and column vectors. Indexing is zero-based, i.e. the upper left-hand corner of a matrix is element (0,0), not element (1,1). Matricies are stored as a list of lists, where the top level lists are the rows and the sub-lists are the columns. Because of the way Python handles list references, you have be careful when copying matrix objects. If you have a matrix a, assign b=a, and then change values in b, you will change values in a as well. Matrix copying should be done with copy.deepcopy. This implementation has no memory-saving optimization for sparse matricies. A derived class may implement a more sophisticated storage method by overriding the __getitem__ and __setitem__ functions. Determinants are taken by expanding by minors on the top row. The private functions supplied for expansion by minors are more generic than what is needed by this implementation. They may be used by a derived class that wishes to do more efficient expansion of sparse matricies. """ __author__ = "Bryan Mills " __version__ = "2.0" class Matrix_Error(Exception): """Abstract parent for all matrix exceptions """ pass class Matrix_Arithmetic_Error(Matrix_Error): """Incorrect dimensions for arithmetic This exception is thrown when you try to add or multiply matricies of incompatible sizes. """ def __init__(self, a, b, operation): self.a = a self.b = b self.operation = operation def __str__(self): return "Cannot %s a %dx%d and a %dx%d matrix" % \ (self.operation, \ self.a.rows(), self.a.cols(), \ self.b.rows(), self.b.cols()) class Matrix_Multiplication_Error(Matrix_Arithmetic_Error): """Thrown when you try to multiply matricies of incompatible dimensions. This exception is also thrown when you try to right-multiply a row vector or left-multiply a column vector. """ def __init__(self, a, b): Matrix_Arithmetic_Error.__init__(self, a, b, "multiply") class Matrix_Addition_Error(Matrix_Arithmetic_Error): """Thrown when you try to add matricies of incompatible dimensions. """ def __init__(self, a, b): Matrix_Arithmetic_Error.__init__(self, a, b, "add") class Square_Error(Matrix_Error): """Square-matrix only This exception is thrown when you try to calculate a function that is only defined for square matricies on a non-square matrix. """ def __init__(self, func): self.func = func def __str__(self): return "%s only defined for square matricies." % self.func class Trace_Error(Square_Error): """Thrown when you try to get the trace of a non-square matrix. """ def __init__(self): Square_Error.__init__(self, "The trace is") class Minor_Error(Square_Error): """Thrown when you try to take a minor of a non-square matrix. """ def __init__(self): Square_Error.__init__(self, "Minors are") class Determinant_Error(Square_Error): """Thrown when you try to take the determinant of a non-square matrix. """ def __init__(self): Square_Error.__init__(self, "The determinant is") class Matrix(object): """A linear algebra matrix This class defines a generic matrix and the basic matrix operations from linear algebra. An instance of this class is a single matrix with particular values. """ null_element = 0 identity_element = 1 inverse_element = -1 class_type = int def __init__(self,r,c=None,class_type=None): """Matrix constructor A matrix can be created in three ways. 1. A single integer argument is supplied. The constructor creates a null square matrix of that size. For example Matrix(2) creates the following matrix 0 0 0 0 2. Two integer arguments are supplied. The constructor creates a null matrix of size first argument x second argument. For example Matrix(2, 3) creates the following matrix 0 0 0 0 0 0 3. A list of lists is supplied. It represents a set of initial matrix values. Each element is a row and each sub-list is a column. For example Matrix([[1,2,3], [4,5,6], [7,8,9]]) creates the following matrix 1 2 3 4 5 6 7 8 9 """ if class_type: self.class_type = class_type self.null_element = self.class_type(0) self.identity_element = self.class_type(1) self.inverse_element = self.class_type(-1) if c: # Create an null n,m matrix. row, col = r,c self.__create_null_matrix(row, col) else: if isinstance(r, types.IntType): # Create a square null matrix. self.__create_null_matrix(r, r) else: # Create a matrix from initial values. self.m = map(lambda x: map(self.class_type,x),r) def __create_null_matrix(self, row, col): """ Create a matrix using the null value This is a private function called by __init__. """ if not row > 0: raise ValueError("invalid number of rows %d" % row) if not col > 0: raise ValueError("invalid number of columns %d" % col) if row == 1 and col == 1: raise ValueError("Cannot create 1x1 matrix") # Note, you cannot simply write # self.m = [[self.null_element]*col]*row # because this will make all the rows references of a single instance. self.m = [] for i in xrange(row): self.m.append([]) for j in xrange(col): self.m[i].append(self.null_element) def __create_identity_matrix(cls, n): """Creates an nxn identity matirx (aka unit matrix) The unit matrix is a diagonal matrix whose diagonal is composed of identity elements. For example, Matrix.create_identity_matrix(3) returns the matrix 1 0 0 0 1 0 0 0 1 """ m = cls(n) for i in xrange(m.rows()): m[(i,i)] = m.identity_element return m create_identity_matrix = classmethod(__create_identity_matrix) def __create_row_vector(cls, v): """Creates a row vector. v is a list of the column values """ if not isinstance(v, types.ListType): raise TypeError("Row vector data must be a list") return cls([v]) create_row_vector = classmethod(__create_row_vector) def __create_column_vector(cls, v): """Creates a column vector. v is a list of the row values """ if not isinstance(v, types.ListType): raise TypeError("Column vector data must be a list") return self.__class__(map(lambda x: [x], v), class_type=self.class_type) create_column_vector = classmethod(__create_column_vector) def __str__(self): s = "" for row in self.m: s += "%s\n" % row return s def __cmp__(self, other): if not isinstance(other, Matrix): raise TypeError("Cannot compare matrix with %s" % type(other)) return cmp(self.m, other.m) def __getitem__(self, (row, col)): """The value at (row, col) For example, to get the value of element 1,3 say m[(1,3)] """ return self.m[row][col] def __setitem__(self, (row, col), value): """Sets the value at (row, col) For example, to set the value of element 1,3 to 5 say m[(1,3)] = 5 """ self.m[row][col] = value def rows(self): """The number of rows in the matrix """ return len(self.m) def cols(self): """The number of columns in the matrix """ return len(self.m[0]) def row(self, i): """The ith row of the matrix """ return self.m[i] def col(self, j): """The jth row of the matrix """ r = [] for row in self.m: r.append(row[j]) return r def __add__(self, other): """Add matrix self+other """ if not isinstance(other, Matrix): raise TypeError("Cannot add a matrix to type %s" % type(other)) if not (self.cols() == other.cols() and self.rows() == other.rows()): raise Matrix_Addition_Error(self, other) r = [] for row in xrange(self.rows()): r.append([]) for col in xrange(self.cols()): r[row].append(self[(row, col)] + other[(row, col)]) return self.__class__(r,class_type=self.class_type) def __neg__(self): """Negate the current matrix """ return self.inverse_element*self def __sub__(self, other): """Subtract matrix self-other """ return self + -other def __mul__(self, other): """Multiply matrix self*other other can be another matrix or a scalar. If matrix the this is the dot product. """ if self.is_scalar_element(other): return self.__scalar_multiply(other) if not isinstance(other, Matrix): raise TypeError("Cannot multiply matrix and type %s" % type(other)) if other.is_row_vector(): raise Matrix_Multiplication_Error(self, other) return self.matrix_multiply(other) def __rmul__(self, other): """Multiply other*self This is only called if other.__add__ is not defined, so assume that other is a scalar. """ if not self.is_scalar_element(other): raise TypeError("Cannot right-multiply by %s" % type(other)) return self.__scalar_multiply(other) def __scalar_multiply(self, scalar): """Multiply the matrix by a scalar value. This is a private function called by __mul__ and __rmul__. """ r = [] for row in self.m: r.append(map(lambda x: x*scalar, row)) return Matrix(r, class_type=self.class_type) def matrix_multiply(self, other): """Multiply the matrix by another matrix. This is a private function called by __mul__. """ # Take the product of two matricies. r = [] assert(isinstance(other, Matrix)) if not self.cols() == other.rows(): raise Matrix_Multiplication_Error(self, other) for row in xrange(self.rows()): r.append([]) for col in xrange(other.cols()): r[row].append( \ self.vector_inner_product(self.row(row), other.col(col))) if len(r) == 1 and len(r[0]) == 1: # The result is a scalar. return r[0][0] else: # The result is a matrix. return Matrix(r, class_type=self.class_type) def is_row_vector(self): """Is the matrix a row vector? """ return self.rows() == 1 and self.cols() > 1 def is_column_vector(self): """Is the matrix a column vector? """ return self.cols() == 1 and self.rows() > 1 def is_square(self): """Is the matrix square? """ return self.rows() == self.cols() def transpose(self): """The transpose of the matrix """ r = [] for col in xrange(self.cols()): r.append(self.col(col)) return Matrix(r, class_type=self.class_type) def trace(self): """The trace of the matrix """ if not self.is_square(): raise Trace_Error() t = 0 for i in xrange(self.rows()): t += self[(i,i)] return t def determinant(self): """The determinant of the matrix """ if not self.is_square(): raise Determinant_Error() # Calculate 2x2 determinants directly. if self.rows() == 2: return self[(0, 0)]*self[(1, 1)] - self[(0, 1)]*self[(1, 0)] # Expand by minors for larger matricies. return self.expand_by_minors_on_row(0) def expand_by_minors_on_row(self, row): """Calculates the determinant by expansion of minors This function returns the determinant of the matrix by doing an expansion of minors on the specified row. """ assert(row < self.rows()) d = 0 for col in xrange(self.cols()): # Note: the () around -1 are needed. Otherwise you get -(1**col). d += (-1)**(row+col)* \ self[(row, col)]*self.minor(row, col).determinant() return d def expand_by_minors_on_column(self, col): """Calculates the determinant by expansion of minors This function returns the determinant of the matrix by doing an expansion of minors on the specified column. """ assert(col < self.cols()) d = 0 for row in xrange(self.rows()): # Note: the () around -1 are needed. Otherwise you get -(1**col). d += (-1)**(row+col) \ *self[(row, col)]*self.minor(row, col).determinant() return d def minor(self, i, j): """A minor of the matrix This function returns the minor given by striking out row i and column j of the matrix. """ # Verify parameters. if not self.is_square(): raise Minor_Error() if i<0 or i>=self.rows(): raise ValueError("Row value %d is out of range" % i) if j<0 or j>=self.cols(): raise ValueError("Column value %d is out of range" % j) # Create the output matrix. m = Matrix(self.rows()-1, self.cols()-1, class_type=self.class_type) # Loop through the matrix, skipping over the row and column specified # by i and j. minor_row = minor_col = 0 for self_row in xrange(self.rows()): if not self_row == i: # Skip row i. for self_col in xrange(self.cols()): if not self_col == j: # Skip column j. m[(minor_row, minor_col)] = self[(self_row, self_col)] minor_col += 1 minor_col = 0 minor_row += 1 return m def vector_inner_product(self, a, b): """Takes the inner product of vectors a and b a and b are lists. This is a private function called by matrix_multiply. """ assert(isinstance(a, types.ListType)) assert(isinstance(b, types.ListType)) return reduce(operator.add, map(operator.mul, a, b)) def is_scalar_element(self, x): """Is x a scalar By default a scalar is an element in the complex number field. A class that wants to perform linear algebra on things other than numbers may override this function. """ return isinstance(x, types.IntType) or \ isinstance(x, types.FloatType) or \ isinstance(x, types.ComplexType) def gauss_jordan(self, eps = 1.0/(10**10)): """ Used by inverse to produce the inverting matrix. Places this matrix into reduced row echelon form. Returns true if possible, false otherwise. Adapted from: http://elonen.iki.fi/code/misc-notes/python-gaussj/index.html and http://en.wikipedia.org/wiki/Gaussian_elimination Modified to work in finite fields. """ (h, w) = (self.rows(), self.cols()) for y in range(0,h): maxrow = y # locate the maximum pivot # not necessary but improves performance for y2 in range(y+1, h): if self.m[y2][y] > self.m[maxrow][y]: maxrow = self.class_type(y2) # swap the max row with the current row (self.m[y], self.m[maxrow]) = (self.m[maxrow], self.m[y]) # this matrix is probally singular return false if self.m[y][y] <= eps: return False # eliminate column y by multipling by the given factor c for y2 in range(y+1, h): c = self.class_type(self.class_type(self.m[y2][y]) / self.class_type(self.m[y][y])) for x in range(y, w): self.m[y2][x] = self.m[y2][x] - (self.m[y][x] * c) # self.m[y2][x] -= self.m[y][x] * c # backsubstitute for y in range(h-1, 0-1, -1): c = self.class_type(self.m[y][y]) for y2 in range(0,y): for x in range(w-1, y-1, -1): # self.m[y2][x] = self.m[y2][x] - (self.m[y][x] * self.m[y2][y] / c) self.m[y2][x] = self.m[y2][x] - (self.class_type(self.m[y][x]) * self.m[y2][y] / c) # self.m[y2][x] -= self.m[y][x] * self.m[y2][y] / c self.m[y][y] = self.m[y][y]/c # self.m[y][y] /= c # Normalize row y for x in range(h, w): self.m[y][x] = self.m[y][x] / c # self.m[y][x] /= c return True def inverse(self): """ Uses gauss jordan elimination to caculate the inverse matrix. Concatanates the identity matrix to the right of the original matrix then runs the function gauss_jordan. If this call return true the the inverse exists otherwise it does not. The inverse of this matrix is then located where the identity matrix once did. This function returns just the inverse matrix. """ ax = copy.deepcopy(self) ax.__concat_to_right(self.__class__.create_identity_matrix(self.rows())) ax.gauss_jordan() rows = self.rows() cols = self.cols() # collect the inverse matrix k = [] for y in range(rows): k.append([]) for x in range(cols): k[y].append(ax.m[y][cols+x]) return Matrix(k, class_type=self.class_type) def __concat_to_right(self, other): """ PRIVATE METHOD This is used by inverse to concat the identity matrix to the right of the original matrix """ s_rows, s_cols = (self.rows(), self.cols()) o_rows, o_cols = (other.rows(), other.cols()) for r in xrange(o_rows): if r >= s_rows: self.m.append([]) for c2 in xrange(s_cols): self.m[x][c2] = 0.0 for c in xrange(o_cols): self.m[r].append(other.m[r][c]) class GfMatrix( Matrix ): null_element = 0 identity_element = 1.0 inverse_element = -1 def __init__(self, *args): super(GfMatrix, self).__init__(*args) self.null_element = gf(0) self.identity_element = gf(1) self.inverse_element = gf(1) def setbit(v, i, b): v = v | (1 << i) return v def bitv(v, i): q = v & (1 << i) if q > 0: return 1 else: return 0 import math from SmallField import Field bitvector = "1011" class gf(int): def __init__(self, v): super(gf, self).__init__(v) self.F = Field(bitvector) self.v = int(v) def __add__(self, other): if(isinstance(other, gf)): ov = other.v else: ov = other return self.F.plus(self.v,ov) def __sub__(self, other): return self.__add__(other) def __mul__(self, other): if(isinstance(other, gf)): ov = other.v else: ov = other if(self.v == 0 or ov == 0): return gf(0) return gf(int(self.F.prod(self.v,ov))) def __inv__(self): if(self.v == 0): return gf(0) return gf(int(self.F.inv(self.v))) def __div__(self, other): if other == 0: return gf(0) if(isinstance(other, gf)): ov = other else: ov = gf(other) return gf(int(self.F.prod(self.v,self.F.inv(ov.v)))) def __rdiv__(self, other): if(isinstance(other, gf)): ov = other else: ov = gf(other) return gf(ov.__div__(self)) def __pow__(self, e): retval = gf(self.v) for i in range(0,e): retval = retval.__mul__(self) return retval def __neg__(self): return self.__inv__() def __rpow__(self, e): return self.__pow__(e) def __abx__(self): return self def __rmul__(self, other): return self.__mul__(other) def __radd__(self, other): return self.__add__(other) def __rsub__(self, other): return self.__add__(other) def __str__(self): return self.v.__str__() def __repr__(self): return self.v.__str__() import unittest class TestStandardMatrixStuff(unittest.TestCase): def testAdd(): pass if __name__ == "__main__": p = 7 m = [[gf(1),gf(1),gf(1)],[gf(4),gf(3),gf(6)],[gf(0),gf(0),gf(1)]] d = [[gf(2),gf(1),gf(0),gf(7),gf(6),gf(5),gf(4),gf(3)], [gf(3),gf(1),gf(2),gf(4),gf(7),gf(5),gf(6),gf(0)], [gf(5),gf(6),gf(3),gf(4),gf(1),gf(2),gf(7),gf(0)]] mat = Matrix(m, class_type=gf) d_mat = GfMatrix(d) # print m # print mat.gauss_jordan() print mat print "matrix inverse", "*"*5 k = mat.inverse() print k print mat*k print k*d_mat # r= m*k # print r # print p for i in range(0,p+1): d = [] for j in range(0,p+1): gg = gf(i) d.append(gg+j) print d print "-"*20 for i in range(0,p+1): d = [] for j in range(0,p+1): gg = gf(i) d.append(gg*j) print d print "*"*20 for i in range(0,p+1): gg = gf(i) print gg.__inv__() from numpy import * from numpy.linalg import * a = array([[gf(1),gf(1),gf(1)],[gf(4),gf(3),gf(6)],[gf(0),gf(0),gf(1)]], gf) # print a - 6 # print dot(a,a) # a_inv = inv(a) # print a_inv # print dot(a_inv,a)