From a680f4bbd929f128dd19f8e3da44b1208238cf5c Mon Sep 17 00:00:00 2001 From: Lukasz M Date: Sat, 9 Apr 2022 18:22:03 +0200 Subject: [PATCH] improved error handling in the program and significant rewrite --- linalg-practice.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/linalg-practice.py b/linalg-practice.py index 72a9ff3..681c059 100644 --- a/linalg-practice.py +++ b/linalg-practice.py @@ -3,6 +3,8 @@ import time import random import readline +errs = (ValueError, TypeError) + def genmatrix(rowcol, intmax, dif) : # generate a random matrices a = sp.randMatrix(rowcol, rowcol, 0, intmax) @@ -21,7 +23,11 @@ def inmat(rowcol) : mat[i]=input().split(" ") for j in range(len(mat[i])) : # convert list using nsimplify in order to take rational number symbolically - mat[i][j] = sp.nsimplify(mat[i][j]) + try : + mat[i][j] = sp.nsimplify(mat[i][j]) + except errs : return False + try : mat = sp.Matrix(mat) + except errs : return False return mat # multcheck function to check if two matrices are multiplied together @@ -29,39 +35,41 @@ def multcheck(a, b, rowcol, intmax) : sp.pprint(b) print("Multiply these two matrices together") # return bool based on if input equals the two matrices multiplied together - return (sp.Matrix(inmat(rowcol)) == a*b) + return (inmat(rowcol) == a*b) # detcheck function to check if the determinant of the matrix is correct def detcheck(a, b, rowcol, intmax): det = sp.det(a) # return bool based on if input equals determinant - return (det == sp.nsimplify(input("What is the determinant of this matrix?: "))) + try : d = sp.nsimplify(input("What is the determinant of this matrix?: ")) + except errs : return False + return (det == d) # invcheck function to check if the inverse of the matrix is correct def invcheck(a, b, rowcol, intmax): # return bool based on if input equals inverse of matrix print("What is the inverse of this matrix?") - return (sp.Matrix(inmat(rowcol)) == a.inv()) + return (inmat(rowcol) == a.inv()) # eigcheck function to check if the eigenvalues are correct def eigcheck(a, b, rowcol, intmax): eigs = a.eigenvals() for i in range(len(eigs)) : - val = sp.nsimplify(input("Input eigenvalue: ")) - if not (val in eigs and eigs[val] == sp.nsimplify(input("Input its algebraic multiplicity: "))) : + try : + val = sp.nsimplify(input("Input eigenvalue: ")) + algm = sp.nsimplify(input("Input its algebraic multiplicity: ")) + except errs : return False + if not (val in eigs and eigs[val] == algm) : # return false if the eigenvalue not in dictionary and wrong alg multiplicity return False return True def diagcheck(a, b, rowcol, intmax): - return (a.diagonalize()[1] == sp.Matrix(inmat(rowcol))) + return (a.diagonalize()[1] == inmat(rowcol)) # practice function def practice(t) : count = 0 - rowcol = int(input("What size of matrix do you want to practice with? ")) - intmax = int(input("What maximum size of integer do you want the matrix to be made out of? ")) - dif = float(input("What difficulty (probability for a matrix of rational values between 0, 1) do you want? ")) # choose the function of the program that you want if t == "mult" : @@ -77,6 +85,15 @@ def practice(t) : else : exit() + while True : + try : + rowcol = abs(int(input("What size of matrix do you want to practice with? "))) + intmax = abs(int(input("What maximum size of integer do you want the matrix to be made out of? "))) + dif = float(input("What difficulty (probability for a matrix of rational values between 0, 1) do you want? ")) + break + except errs: + continue + # initialise time measurement tic = time.perf_counter()