464,748 Members | 1,251 Online Need help? Post your question and get tips & solutions from a community of 464,748 IT Pros & Developers. It's quick & easy.

# Scope Problems (I think?)

 P: 6 Hey all, I'm making a program to solve systems of equations using Cramer's rule. Here's how part of it works: Take a matrix (let's say it's 3*3). I represent this matrix by a 2D array, containing each row in an array. [[the,top,row],[m,i,d],[b,o,t]]. Then, I have a column of numbers, (1 column wide, and we'll say 3 high, again). Cramer's rule works by replacing the first column in the 3*3 matrix with the new column, then performing a calculation. Then, take the ORIGINAL matrix and replace the SECOND column with the new column, and perform the calculation. then, do the same thing with the THIRD column... In other words: Expand|Select|Wrap|Line Numbers def repCol(m, j, col):     #For consistency, we will define j on the scale of (0,x), not (1,x)     for row in range(0,len(col)):         m[row][j] = col[row]     return m   def cramer(exp, sol):     solutions = []     D = float(det(exp))     print D     for var in range(0,len(exp)):         solutions.append(det(repCol(exp,var,sol))/D)         print exp     return solutions   I'm running the cramer function, with exp being the 3*3, and sol being the 3*1 array. The first replacement and calculation go fine. The problem is on the second one. It ends up having replaced the first and the second columns, instead of just the second. I think the problem is that the repCol function doesn't just return the modified matrix, but it modifies the ORIGINAL matrix. How do I fix this? Aug 6 '08 #1 