def intersect(self, other): u = self % other t = u.transposition().simplify() a = [] isPivot = [False for i in range(t.m)] for i in range(t.n): for j in range(t.m): if (t.e[i][j] != rational.number(0)): if j < self.n: a += [self.row(j)] isPivot[j] = True break e = [] for i in range(self.n, t.m): if isPivot[i] == False: v = matrix(1, self.m, [[rational.number(0) for x in range(self.m)]]) for j in range(len(a)): v += a[j].multiply(t.e[j][i]) e += [copy.deepcopy(v.e[0])] return matrix(len(e), self.m, e)
def value(x): if (x.n != x.m): raise Exception("Error: The matrix isn't a determinant.") x = x.guass() product = rational.number(1) for i in range(x.n): product *= x.e[i][i] print product
def union(self, other): u = self % other t = u.transposition().simplify() e = [] for i in range(t.n): pivot = -1 for j in range(t.m): if (t.e[i][j] != rational.number(0)): pivot = j break if pivot >= 0: e += [u.e[pivot]] return matrix(len(e), self.m, e)
def simplify(self): c = self.guass() e = c.e for i in range(c.n): pivot = -1 for j in range(c.m): if e[i][j] != rational.number(0): pivot = j break for j in range(c.m)[::-1]: e[i][j] /= e[i][pivot] for j in range(i): for k in range(pivot, c.m)[::-1]: e[j][k] -= e[i][k] * e[j][pivot] / e[i][pivot] return c
def __mul__(self, other): if self.m != other.n: raise Exception( "Error: The first matrix's number of cloumns isn't equal to the second matrix's number of rows." ) return False e = [] for i in range(self.n): row = [] for j in range(other.m): count = rational.number(0) for k in range(self.m): count += self.e[i][k] * other.e[k][j] row += [count] e += [row] return matrix(self.n, other.m, e)
def guass(self): c = copy.deepcopy(self) e = c.e n = 0 for i in range(c.m): pivot = -1 for j in range(n, c.n): if (e[j][i] != rational.number(0)): if (pivot == -1 or e[j][i] < e[pivot][i]): pivot = j if (pivot == -1): continue e[n], e[pivot] = e[pivot], e[n] for j in range(n + 1, c.n): for k in range(i, c.m)[::-1]: e[j][k] -= e[j][i] / e[n][i] * e[n][k] n += 1 c.n = n return c
def identity(n): e = [[rational.number(0) for i in range(n)] for j in range(n)] for i in range(n): e[i][i] = rational.number(1) return matrix(n, n, e)
def solve(self): t = self.guass() if (t.n == t.m): print "It seems that there is no solution to this equation." return sol = [0 for i in range(t.m - 1)] isPivot = [False for i in range(t.m - 1)] for x in range(t.n)[::-1]: pivot = -1 for i in range(t.m - 1): if t.e[x][i] != rational.number(0): pivot = i break isPivot[pivot] = True sol[pivot] = t.e[x][t.m - 1] / t.e[x][pivot] for i in range(0, x): t.e[i][t.m - 1] -= sol[pivot] * t.e[i][pivot] for i in range(t.m - 1): sol[i] = str(sol[i]) print str(sol).replace('\'', '') print '+' free = [] for i in range(t.m - 1): if isPivot[i] == False: free += [i] freeCount = len(free) k = [[rational.number(0) for i in range(t.m - 1)] for i in range(t.m - 1)] for x in range(t.n)[::-1]: pivot = -1 for i in range(t.m - 1): if (pivot == -1): if t.e[x][i] != rational.number(0): pivot = i else: if isPivot[i]: for j in range(t.m - 1): k[pivot][j] -= k[i][j] * t.e[x][i] / t.e[x][pivot] else: k[pivot][i] -= t.e[x][i] / t.e[x][pivot] e = [] for x in range(freeCount): vector = [] for i in range(t.m - 1): if i == free[x]: vector += ['1'] elif isPivot[i]: vector += [str(k[i][free[x]])] else: vector += ['0'] print 'C' + str(x) + str(vector).replace('\'', '') if x == freeCount - 1: print 'C0...C' + str(x) + ' can be any integer.' else: print '+' e += vector return matrix(freeCount, t.m - 1, e)
def parseNumber(s): if '/' in s: ss = s.split('/') return rational.number(int(ss[0]), int(ss[1])) else: return rational.number(int(s))