Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ge_fw(matrix):
- # Error Checks
- if type(matrix) != list:
- print("The input type is not a list")
- return False
- rowLength = len(matrix[0])
- for i in matrix:
- if type(i) != list:
- print("Type of rows are not lists")
- return False
- if len(i) != rowLength:
- print("Number of columns does not match")
- return False
- for val in i:
- if type(val) != float and type(val) != int:
- print("The input value type is non-numeric")
- return False
- # Begin
- m = []
- # Duplicated
- for i in matrix:
- m = m + [list(i)]
- for c in range(0, len(m), 1):
- # Swap zero start rows:
- swapped = True
- if m[c][c] == 0:
- swapped = False
- for i in range(c + 1, len(m), 1):
- # Do the swap
- if m[i][c] != 0:
- temp = list(m[c])
- m[c] = list(m[i])
- m[i] = temp
- swapped = True
- break
- if not swapped:
- continue
- for r in range(c + 1, len(m), 1):
- scale = m[r][c]/m[c][c]
- for v in range(c, len(m[0]), 1):
- m[r][v] = m[r][v] - scale*m[c][v]
- # Check zero rows
- for r in range(c, len(m), 1):
- zr = True
- for x in m[r]:
- if x != 0:
- zr = False
- break
- if zr:
- # Push row to the bottom
- m = m[0:r] + m[r+1:len(m)] + [m[r]]
- return m
- def ge_bw(matrix):
- # Error Checks
- if type(matrix) != list:
- print("The input type is not a list")
- return False
- rowLength = len(matrix[0])
- for i in matrix:
- if type(i) != list:
- print("Type of rows are not lists")
- return False
- if len(i) != rowLength:
- print("Number of columns does not match")
- return False
- for val in i:
- if type(val) != float and type(val) != int:
- print("The input value type is non-numeric")
- return False
- # Begin
- m = []
- # Duplicated
- for i in matrix:
- m = m + [list(i)]
- # Expects matrix with zero rows @ bottom
- i = 0
- for r in m:
- nonZero = False
- for c in r:
- if c != 0:
- nonZero = True
- break
- if not nonZero:
- break
- i = i + 1
- if i >= len(m):
- i = len(m) - 1
- for c in range(i, -1, -1):
- for r in range(c - 1, -1, -1):
- if m[c][c] == 0:
- continue
- scale = m[r][c]/m[c][c]
- for v in range(c, len(m[0]), 1):
- m[r][v] = m[r][v] - scale*m[c][v]
- # Put ones at the start of each row
- if m[c][c] == 0:
- continue
- scale = 1/m[c][c]
- for v in range(0, len(m[0]), 1):
- m[c][v] = m[c][v]*scale
- # Check zero rows
- for r in range(c, len(m), 1):
- zr = True
- for x in m[r]:
- if x != 0:
- zr = False
- break
- if zr:
- # Push row to the bottom
- m = m[0:r] + m[r+1:len(m)] + [m[r]]
- return m
Add Comment
Please, Sign In to add comment