Thefaceofbo

GE (py)

Nov 9th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. def ge_fw(matrix):
  2.    # Error Checks
  3.    if type(matrix) != list:
  4.       print("The input type is not a list")
  5.       return False
  6.    rowLength = len(matrix[0])
  7.    for i in matrix:
  8.       if type(i) != list:
  9.          print("Type of rows are not lists")
  10.          return False
  11.       if len(i) != rowLength:
  12.          print("Number of columns does not match")
  13.          return False
  14.       for val in i:
  15.          if type(val) != float and type(val) != int:
  16.             print("The input value type is non-numeric")
  17.             return False
  18.    # Begin
  19.    m = []
  20.    #  Duplicated
  21.    for i in matrix:
  22.       m = m + [list(i)]
  23.    for c in range(0, len(m), 1):
  24.       # Swap zero start rows:
  25.       swapped = True
  26.       if m[c][c] == 0:
  27.          swapped = False
  28.          for i in range(c + 1, len(m), 1):
  29.             # Do the swap
  30.             if m[i][c] != 0:
  31.                temp = list(m[c])
  32.                m[c] = list(m[i])
  33.                m[i] = temp
  34.                swapped = True
  35.                break
  36.       if not swapped:
  37.          continue
  38.       for r in range(c + 1, len(m), 1):
  39.          scale = m[r][c]/m[c][c]
  40.          for v in range(c, len(m[0]), 1):
  41.             m[r][v] = m[r][v] - scale*m[c][v]
  42.       # Check zero rows
  43.       for r in range(c, len(m), 1):
  44.          zr = True
  45.          for x in m[r]:
  46.             if x != 0:
  47.                zr = False
  48.                break
  49.          if zr:
  50.             # Push row to the bottom
  51.             m = m[0:r] + m[r+1:len(m)] + [m[r]]
  52.    return m
  53.  
  54.  
  55. def ge_bw(matrix):
  56.    # Error Checks
  57.    if type(matrix) != list:
  58.       print("The input type is not a list")
  59.       return False
  60.    rowLength = len(matrix[0])
  61.    for i in matrix:
  62.       if type(i) != list:
  63.          print("Type of rows are not lists")
  64.          return False
  65.       if len(i) != rowLength:
  66.          print("Number of columns does not match")
  67.          return False
  68.       for val in i:
  69.          if type(val) != float and type(val) != int:
  70.             print("The input value type is non-numeric")
  71.             return False
  72.    # Begin
  73.    m = []
  74.    # Duplicated
  75.    for i in matrix:
  76.       m = m + [list(i)]
  77.    # Expects matrix with zero rows @ bottom
  78.    i = 0
  79.    for r in m:
  80.       nonZero = False
  81.       for c in r:
  82.          if c != 0:
  83.             nonZero = True
  84.             break
  85.       if not nonZero:
  86.          break
  87.       i = i + 1
  88.    if i >= len(m):
  89.       i = len(m) - 1
  90.    for c in range(i, -1, -1):
  91.       for r in range(c - 1, -1, -1):
  92.          if m[c][c] == 0:
  93.             continue
  94.          scale = m[r][c]/m[c][c]
  95.          for v in range(c, len(m[0]), 1):
  96.             m[r][v] = m[r][v] - scale*m[c][v]
  97.       # Put ones at the start of each row
  98.       if m[c][c] == 0:
  99.          continue
  100.       scale = 1/m[c][c]
  101.       for v in range(0, len(m[0]), 1):
  102.          m[c][v] = m[c][v]*scale
  103.    # Check zero rows
  104.    for r in range(c, len(m), 1):
  105.       zr = True
  106.       for x in m[r]:
  107.          if x != 0:
  108.             zr = False
  109.             break
  110.       if zr:
  111.          # Push row to the bottom
  112.          m = m[0:r] + m[r+1:len(m)] + [m[r]]
  113.    return m
Add Comment
Please, Sign In to add comment