CAR-MAT.PY: The Matrix Row Operation Machine

Elementary matrix operations are used extensively in classes like linear algebra and operations research, and I am terrible at them.

While calculating row operations requires very basic math skills, you do these operations MANY times, and making one single mistake can ruin everything.

The internet is full of matrix calculators, but none of them that I could find were able to just do matrix operations on command, so they may be able to do the entire simplex algorithm and give you an optimal result, but NOT be able to do individual row operations — so I made this.

>>>>>HOW_TO_USE_CARMAT<<<<<

->copy the text in the box below

->paste the text into a python text editor OR paste it into an online python compiler (I personally like the one on the button below, but there are many that work well)

->edit the matrix and command section to fit your needs

->run the program and observe the output in the terminal

(Currently, I haven’t extensively checked this (I’ve been busy, okay) so feel free to let me know if there is a bug.)

UPDATE: I, for some reason, thought online compilers didn’t have numpy, so the original code output raw lists and it looked like mega garbo trash. Turns out, the one that I liked does! So now the matrices will look like actual matrices instead of “[[-1, 2000, 500],[ 1, 0, 0.333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333],[ 0, 0, 0]]”.

#   █████████    █████████   ███████████              ██████   ██████   █████████   ███████████
#  ███░░░░░███  ███░░░░░███ ░░███░░░░░███            ░░██████ ██████   ███░░░░░███ ░█░░░███░░░█
# ███     ░░░  ░███    ░███  ░███    ░███             ░███░█████░███  ░███    ░███ ░   ░███  ░ 
#░███          ░███████████  ░██████████   ██████████ ░███░░███ ░███  ░███████████     ░███    
#░███          ░███░░░░░███  ░███░░░░░███ ░░░░░░░░░░  ░███ ░░░  ░███  ░███░░░░░███     ░███    
#░░███     ███ ░███    ░███  ░███    ░███             ░███      ░███  ░███    ░███     ░███    
# ░░█████████  █████   █████ █████   █████            █████     █████ █████   █████    █████   
#  ░░░░░░░░░  ░░░░░   ░░░░░ ░░░░░   ░░░░░            ░░░░░     ░░░░░ ░░░░░   ░░░░░    ░░░░░    
#  ____   _____        __   ___  ____  _____ ____      _  _____ ___ ___  _   _  
# |  _ \ / _ \ \      / /  / _ \|  _ \| ____|  _ \    / \|_   _|_ _/ _ \| \ | | 
# | |_) | | | \ \ /\ / /  | | | | |_) |  _| | |_) |  / _ \ | |  | | | | |  \| | 
# |  _ <| |_| |\ V  V /   | |_| |  __/| |___|  _ <  / ___ \| |  | | |_| | |\  | 
# |_|_\_\\___/  \_/\_/____ \___/|_|  _|_____|_|_\_\/_/   \_\_| |___\___/|_| \_| 
# |  _ \|  _ \ / _ \ / ___|  _ \    / \  |  \/  |                               
# | |_) | |_) | | | | |  _| |_) |  / _ \ | |\/| |                               
# |  __/|  _ <| |_| | |_| |  _ <  / ___ \| |  | |                               
# |_|   |_| \_\\___/ \____|_| \_\/_/   \_\_|  |_|                               

#THIS IS WHERE YOU INPUT YOUR STARTING MATRIX
#EACH ROW MUST HAVE BRACKETS
#EACH NUMBER MUST HAVE A COMMA SEPARATING IT
basemat = [
    [1, 3, 2, 1, 1, 0, 0],
    [1, 7, 3, 3, 0, 1, 0],
    [2, 6, 4, 4, 0, 0, 1]
]


#THIS IS WHERE YOU INPUT THE STEPS YOU WANT TO PERFORM ON THE BASE MATRIX
#EVERY MATRIX MUST HAVE A MULTIPLIER, SO "1r1-1r2" CANNOT BE WRITTEN AS "r1-r2"
#THE FIRST MATRIX IS THE ONE THAT GETS CHANGED
#THE SECOND MATRIX DOES NOT GET IMPACTED BY AN OPERATION
#r1+r2 ->r1 = '1r1+1r2'
#r3-4r2 -> r3 = '1r3-4r2'
#r3-(1/2)r2 ->r3 = '1r3-1/2r2'
#5/4r1 -> r1 = '5/4r1'
#ALL INPUTS MUST BE CONTAINED IN QUOTATION MARKS
#YOU CAN EXPAND THE LIST BY FOLLOWING THE FORMAT, BUT THE FINAL DICTIONARY INPUT MUST BE "'end'"

commandlist = {
    0: '1r2-1r1',
    1: '1r3-2r1',
    2: '1r1-2r2',
    3: '1r2-1/2r3',
    4: '1r1+3/2r3',
    5: '-1/2r3',
    6: 'end',
    7: 'end',
    8: 'end',
    9: 'end',
    10: 'end',
}

import time
import numpy
pausetime = .5


def matprinter(inmatinput):
    inmat = inmatinput
    
    print(numpy.matrix(inmat))

    
    

def rowmat(dissasemblekey, basemat):
    mainmult = eval(dissasemblekey[0])
    mainline = dissasemblekey[1][1:-1]+dissasemblekey[1][-1]
    changemult =  eval(dissasemblekey[2])
    changeline = dissasemblekey[3][1:-1]+dissasemblekey[3][-1]

    if changeline != 'ullify':
        for x in range(len(basemat[int(mainline)-1])):
            basemat[int(mainline)-1][x] = ((mainmult)) * (basemat[int(mainline)-1][x]) + ((changemult)) * (basemat[int(changeline)-1][x])
        print(f'{mainmult}r{mainline}+{changemult}r{changeline}')
        
        

    if changeline == 'ullify':
        for x in range(len(basemat[int(mainline)-1])):
            basemat[int(mainline)-1][x] = ((mainmult)) * (basemat[int(mainline)-1][x])
        print(f'{mainmult}r{mainline}')
        
    
    basemat2 = basemat
    matprinter(basemat2)
    
    return basemat
    

def dissasemble(commandinput):
    stringmand = str(commandinput)
    rcount = 0
    

    for a in range(len(stringmand)):
        if stringmand[a] == 'r':
            rcount += 1

    
    for x in range(len(stringmand)):        
        if stringmand[x] != 'r':
            continue
        if stringmand[x] == 'r':
            firstcut = x
            break
    if rcount == 1:
        multiplier2 = 0
        operatorcut = 0
        thircut = 0

    if rcount == 2:
        y = firstcut + 1
        while y < len(stringmand):
            if (stringmand[y] != '+') and (stringmand[y] != '-'):
                
                
                y += 1
                continue
            if (stringmand[y] == '+') or (stringmand[y] == '-'):
                operatorcut = y
                y += 1
                break
        z = operatorcut + 1
        while z < len(stringmand):
            if stringmand[z] != 'r':
                z += 1
                continue
            if stringmand[z] == 'r':
                thircut = z
                break
    #print(stringmand[firstcut], stringmand[operatorcut], stringmand[thircut])
    if rcount == 2:
        outputlist = (stringmand[0:firstcut],stringmand[firstcut:operatorcut],stringmand[operatorcut:thircut],stringmand[thircut:-1]+stringmand[-1])
        #print(outputlist)
        return outputlist
    if rcount == 1:
        outputlist =(stringmand[0:firstcut], stringmand[firstcut:-1]+stringmand[-1], '0', 'nullify')
        #print(outputlist)
        return outputlist





print('STARTING MATRIX:')
time.sleep(pausetime)
matprinter(basemat)
for x in range(len(commandlist)):
    print(f'STEP {x+1}:')
    time.sleep(pausetime)
    if commandlist[x] == 'end':
        break
    disemblekey = dissasemble(commandlist[x])
    rowmat(disemblekey, basemat)
    time.sleep(pausetime)
    
    
print('STEPS COMPLETE')
print(numpy.matrix(basemat))

Leave a comment