Destroy the intermediate expression of the sweep method with Python

Sweeping method, troublesome

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

Destroy the intermediate expression of the sweep method with Python
Check the existence of the file with python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Hit a method of a class instance with the Python Bottle Web API
[Introduction to Python] What is the method of repeating with the continue statement?
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
Kernel Method with Python
the zen of Python
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Summary of the basic flow of machine learning with Python
Get the operation status of JR West with Python
Extract the band information of raster data with python
Calculate the product of matrices with a character expression?
Towards the retirement of Python2
[Python] Calculation method with numpy
About the ease of Python
Regular expression manipulation with Python
Call the API with python3.
About the features of Python
The Power of Pandas: Python
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
A simple Python implementation of the k-nearest neighbor method (k-NN)
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
Visualize the results of decision trees performed with Python scikit-learn
Calculate the square root of 2 in millions of digits with python
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Run the intellisense of your own python library with VScode.
[Python] LASSO regression with equation constraints using the multiplier method
I evaluated the strategy of stock system trading with Python.
Python --Find out number of groups in the regex expression
Check the scope of local variables with the Python locals function.
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
The story of rubyist struggling with python :: Dict data with pycall
[Homology] Count the number of holes in data with Python
Creating the finite element method (FEM) with python ~ vba → python translation ~
Try to automate the operation of network devices with Python
Rewrite the record addition node of SPSS Modeler with Python.
Let's use the distributed expression of words quickly with fastText!
Estimate the attitude of AR markers with Python + OpenCV + drone
Play with the password mechanism of GitHub Webhook and Python
Get the source of the page to load infinitely with python.
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
Extract the xz file with python
The story of Python and the story of NaN
Save the result of the life game as a gif with python
First Python 3 ~ The beginning of repetition ~
March 14th is Pi Day. The story of calculating pi with python