Created
June 1, 2014 10:58
-
-
Save gerard/aab370f2d2d49c5d99d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from fractions import * | |
import copy | |
import texttable | |
import pprint | |
HORIZONTAL = 1 | |
VERTICAL = 2 | |
BN = 1000000 | |
M = [ | |
[ 0, 1, 1, 0, 0], | |
[ 8,-2, 5, 1, 0], | |
[30, 6, 1, 0, 1], | |
] | |
pi = 2 | |
pj = 1 | |
mode = HORIZONTAL | |
M = [ | |
[ Fraction(125, 16), Fraction(5, 32), Fraction(7, 32)], | |
[ Fraction( 71, 16), -Fraction(1, 32), Fraction(5, 32)], | |
[ Fraction( 54, 16), Fraction(3, 16), Fraction(1, 16)], | |
[ 0, 0, -1], | |
[ 0, -1, 0], | |
[-Fraction( 6, 16), -Fraction(3, 16), -Fraction(1, 16)], | |
] | |
pi = 5 | |
pj = 1 | |
mode = VERTICAL | |
M = [ | |
[ 0, -1, -1], | |
[ 0, -1, 0], | |
[ 0, 0, -1], | |
[ 8, -2, 5], | |
[30, 6, 1], | |
[BN, 1, 1], | |
] | |
pi = 5 | |
pj = 1 | |
while True: | |
T = copy.deepcopy(M) | |
pretty = copy.deepcopy(T) | |
for i in range(len(M)): | |
for j in range(len(M[i])): | |
if mode == HORIZONTAL and i == pi or mode == VERTICAL and j == pj: | |
f = Fraction(M[i][j], M[pi][pj]) | |
if mode == VERTICAL: | |
f = -f | |
else: | |
f = M[i][j] - Fraction(M[pi][j]*M[i][pj], M[pi][pj]) | |
T[i][j] = f | |
pretty[i][j] = "" | |
if i == pi and j == pj: | |
pretty[i][j] += "*" | |
if f.denominator == 1: | |
pretty[i][j] += "%d" % f.numerator | |
else: | |
pretty[i][j] += "%d/%d" % (f.numerator, f.denominator) | |
tpp = texttable.Texttable() | |
tpp.add_rows(pretty) | |
print tpp.draw() | |
M = T | |
drop_row_ans = str(raw_input("Drop row? ")) | |
if drop_row_ans and drop_row_ans[0] == "y": | |
M.pop() | |
new_row_ans = str(raw_input("New row? ")) | |
if new_row_ans and new_row_ans[0] == "y": | |
new_row = [] | |
for i in range(len(M[0])): | |
num = int(input("Col %d Num: " % i)) | |
den = int(input("Col %d Den: " % i)) | |
new_row.append(Fraction(num, den)) | |
M.append(new_row) | |
pi = int(input("New pi: ")) | |
pj = int(input("New pj: ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment