initial version of linalg-practice program with mult feature
This commit is contained in:
		
						commit
						4210f632d7
					
				
							
								
								
									
										71
									
								
								linalg-practice.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								linalg-practice.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,71 @@ | |||||||
|  | import sympy as sp | ||||||
|  | import time | ||||||
|  | import random | ||||||
|  | 
 | ||||||
|  | # in function to take arbitrary matrix input from user | ||||||
|  | def inmat(rowcol) : | ||||||
|  |     # initialise list of lists | ||||||
|  |     mat=[[]]*rowcol | ||||||
|  |     # take input from user into list | ||||||
|  |     print("Enter line by line with entries separated by space:") | ||||||
|  |     for i in range(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]) | ||||||
|  |     return mat | ||||||
|  | 
 | ||||||
|  | # multcheck function to check if two matrices are multiplied together | ||||||
|  | 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) | ||||||
|  | 
 | ||||||
|  | # 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" : | ||||||
|  |         f = multcheck | ||||||
|  |     else : | ||||||
|  |         exit() | ||||||
|  | 
 | ||||||
|  |     # initialise time measurement | ||||||
|  |     tic = time.perf_counter() | ||||||
|  | 
 | ||||||
|  |     # infinite loop of practice | ||||||
|  |     while True : | ||||||
|  |         # generate a random matrices | ||||||
|  |         a = sp.randMatrix(rowcol, rowcol, 0, intmax) | ||||||
|  |         b = sp.randMatrix(rowcol, rowcol, 0, intmax) | ||||||
|  | 
 | ||||||
|  |         # if determinant non-zero, radnom value less than difficulty, set a to its inverse | ||||||
|  |         if a.det() != 0 and random.random() <= dif : | ||||||
|  |             a = a.inv() | ||||||
|  | 
 | ||||||
|  |         # infinite loop until user succeeds  | ||||||
|  |         while True : | ||||||
|  |             sp.pprint(a) | ||||||
|  |             # if return of function is True | ||||||
|  |             if f(a, b, rowcol, intmax) : | ||||||
|  |                 # take time interval | ||||||
|  |                 toc = time.perf_counter() | ||||||
|  |                 tictoc=toc-tic | ||||||
|  |                 # increment count | ||||||
|  |                 count += 1 | ||||||
|  |                 # print success message | ||||||
|  |                 print("Correct! You have solved", count, "problems in %.2f seconds" %tictoc) | ||||||
|  |                 break | ||||||
|  |             else : | ||||||
|  |                 # print failure message | ||||||
|  |                 print("Try again") | ||||||
|  |                 continue | ||||||
|  | 
 | ||||||
|  | # take input from user on the type of practice they want to do | ||||||
|  | t = input("What do you want to practice? (mult, det, inv, eig) ") | ||||||
|  | practice(t) | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user