It's too annoying to need an intermediate formula. It's boring, so I solved it with Python.
sweep.py
from fractions import Fraction
import copy
def swicth_rows(mat_list, num1, num2):
temp = mat_list[num1]
mat_list[num1] = mat_list[num2]
mat_list[num2] = temp
return mat_list
def check_chaneg(a, b):
if not a == b:
print("&=" + tex_print(a) + r"\\")
def tex_print(mat_list):
tex_txt = r"\left(\begin{array}{" + "c" * len(mat_list[0]) + r"}@\end{array}\right)"
inner_txt = ""
for l in mat_list:
line_txt = ""
for el in l:
if "/" in str(el):
line_txt += r"\frac{" + str(el).split("/")[0] + "}{" + str(el).split("/")[1] + "}"
else:
line_txt += str(el)
line_txt += "&"
line_txt = line_txt[: -1]
line_txt += r"\\"
inner_txt += line_txt
tex_txt = tex_txt.replace("@", inner_txt)
return tex_txt
rows = int(input("rows:"))
columns = int(input("columns:"))
mat_list = []
for _ in range(rows):
mat_list.append(list(map(int, input().split())))
print(mat_list)
now_row = 0 #What row are you currently in
pre_mat = []
for n in range(columns):
#Swap lines
pre_mat = copy.deepcopy(mat_list)
zero_flag = True
for i in range(now_row, rows):
if not mat_list[i][n] == 0:
swicth_rows(mat_list, now_row, i)
zero_flag = False
break
if zero_flag:
continue
check_chaneg(mat_list, pre_mat)
#Set nth column to 1
pre_mat = copy.deepcopy(mat_list)
temp = [0] * columns
for c in range(columns):
temp[c] = Fraction(mat_list[now_row][c], mat_list[now_row][n])
mat_list[now_row] = temp
check_chaneg(mat_list, pre_mat)
# now_Set the nth column other than row to 0
pre_mat = copy.deepcopy(mat_list)
for i in range(0, rows):
if i == now_row:
continue
if not mat_list[i][n] == 0:
temp = [0] * columns
for c in range(columns):
temp[c] = mat_list[i][c] - mat_list[i][n] * mat_list[now_row][c]
mat_list[i] = temp
check_chaneg(mat_list, pre_mat)
now_row += 1
if now_row > rows:
break
check_chaneg(mat_list, 0)
Recommended Posts