DanielJackins

November 6th, 2009, 07:48 AM

So I'm taking a linear algebra course in university, and I thought it would be cool/good practice to create a program to perform various matrix operations (ie. determinants, inverses, etc.). This will probably be the first actually useful program I've ever made, so it has been a lot of fun making (just started tonight). So here's what I've got so far:

def det2(x1,x2,y1,y2):

matrix = [[x1,x2],[y1,y2]]

det = (matrix[0][0]*matrix[1][1])-(matrix[0][1]*matrix[1][0])

return det

def det3(x1,x2,x3,y1,y2,y3,z1,z2,z3):

matrix = [[x1,x2,x3],[y1,y2,y3],[z1,z2,z3]]

det = (matrix[0][0]*det2(y2,y3,z2,z3))-(matrix[1][0]*det2(x2,x3,z2,z3))+(matrix[2][0]*det2(x2,x3,y2,y3))

return det

print 'Please enter the size of the matrix:'

print

m = input('m = ')

n = input('n = ')

print

matrix = {}

choice = 1

for element in range(1,(m*n)+1):

matrix.update({element:input('Please enter element ' + str(element) + ': ')})

print

while choice != 0:

print 'What would you like to determine?: '

print

print '1) Determinant'

print '2) Inverse'

print '3) Eigenvalues and Eigenvectors'

print '0) Exit'

print

choice = input('Please enter your choice: ')

print

if choice == 1:

if m*n == 4:

print 'The determinant is', det2(matrix[1],matrix[2],matrix[3],matrix[4])

print

elif m*n == 9:

print 'The determinant is', det3(matrix[1],matrix[2],matrix[3],matrix[4],matrix[5],matrix[6],matrix[7],matrix[8],matrix[9])

print

I know that it correctly computes the determinants. Any comment on the structure/efficiency or other such things? (I'm also aware that at the moment you could enter, for example, a 1x9 matrix and it would still calculate a determinant, which it shouldn't)

Thanks for any comment/suggestions :)

def det2(x1,x2,y1,y2):

matrix = [[x1,x2],[y1,y2]]

det = (matrix[0][0]*matrix[1][1])-(matrix[0][1]*matrix[1][0])

return det

def det3(x1,x2,x3,y1,y2,y3,z1,z2,z3):

matrix = [[x1,x2,x3],[y1,y2,y3],[z1,z2,z3]]

det = (matrix[0][0]*det2(y2,y3,z2,z3))-(matrix[1][0]*det2(x2,x3,z2,z3))+(matrix[2][0]*det2(x2,x3,y2,y3))

return det

print 'Please enter the size of the matrix:'

m = input('m = ')

n = input('n = ')

matrix = {}

choice = 1

for element in range(1,(m*n)+1):

matrix.update({element:input('Please enter element ' + str(element) + ': ')})

while choice != 0:

print 'What would you like to determine?: '

print '1) Determinant'

print '2) Inverse'

print '3) Eigenvalues and Eigenvectors'

print '0) Exit'

choice = input('Please enter your choice: ')

if choice == 1:

if m*n == 4:

print 'The determinant is', det2(matrix[1],matrix[2],matrix[3],matrix[4])

elif m*n == 9:

print 'The determinant is', det3(matrix[1],matrix[2],matrix[3],matrix[4],matrix[5],matrix[6],matrix[7],matrix[8],matrix[9])

I know that it correctly computes the determinants. Any comment on the structure/efficiency or other such things? (I'm also aware that at the moment you could enter, for example, a 1x9 matrix and it would still calculate a determinant, which it shouldn't)

Thanks for any comment/suggestions :)