-
Notifications
You must be signed in to change notification settings - Fork 0
/
RREF_Calculator.py
59 lines (53 loc) · 2.36 KB
/
RREF_Calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
def rrefMatrix(inputMatrix):
NUMROWS = len(inputMatrix[:,0])
NUMCOLS = len(inputMatrix[0,:])
#Multiplies a row and a scalar
def multiplyRow(multiplier, rowNum):
inputMatrix[rowNum,:] *= multiplier
#Adds two rows and replaces that sum into the row inputed last
def addRows(sourceRow, targetRow):
inputMatrix[targetRow,:] += inputMatrix[sourceRow,:]
#Makes an input column into RREF
def RREF (colNum):
for i in range(NUMCOLS-1):
#Makes the diagonal entry 1 in that column
if inputMatrix[colNum][colNum] != 1.0:
if (np.count_nonzero(inputMatrix[i,:])) == 0:
return 0
if inputMatrix[colNum][colNum] == 0:
for i in range(NUMCOLS-1):
if inputMatrix[i][colNum] != 0:
addRows(i, colNum)
if inputMatrix[colNum][colNum] == 0:
return 2
multiplyRow(1/inputMatrix[colNum][colNum], colNum)
#The if statement says that if the [colNum][colNum] is not 1 or any other value is not 0, run the RREF code
if inputMatrix[i][colNum] != 0:
#Checks if there is a row of zeros
if (np.count_nonzero(inputMatrix[i,:])) == 0:
return 0
if inputMatrix[colNum][colNum] == -1:
multiplyRow(-1, colNum)
#Multiplies the row with a 1 by the negative of the entry you are trying to make 0
multiplyRow(-inputMatrix[i][colNum], colNum)
#Adds the two rows, making one entry zero
addRows(colNum, i)
#Makes the diagonal entry 1 again
if inputMatrix[colNum][colNum] != 1.0 and inputMatrix[colNum][colNum] != 0.0:
if (np.count_nonzero(inputMatrix[i,:])) == 0:
return 0
if ((np.count_nonzero(inputMatrix[i,:])) == 1 and inputMatrix[NUMROWS-1][NUMCOLS-1] != 0):
return 2
multiplyRow((1/inputMatrix[colNum][colNum]), colNum)
if i == (NUMCOLS-2):
return 1
for i in range(NUMCOLS-1):
ret = RREF(i)
if ret == 0:
break
if ret == 2:
break
if ret == 1:
pass
return inputMatrix