Vermiculus

This song is creepy as shit

Oct 25th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. def makematrix(cols, rows):
  2.     return [cols*[0] for i in range(rows)]
  3.  
  4. def select(matrix):
  5.     # finds the objective row (the z-row)
  6.     obj = len(matrix)-1
  7.    
  8.     ## Find the pivot column
  9.     pivotcol = 0
  10.     mincol = 100**100
  11.    
  12.     # for every possible index in the objective function
  13.     # (less the answer column, which is last, hence len-1 (remember that range is upper-bound-exclusive)
  14.     for i in range(len(matrix[obj]) - 1):
  15.         # matrix[obj][i] is a candidate pivot column
  16.         if matrix[obj][i] < mincol:
  17.             pivotcol = i
  18.             mincol = matrix[obj][i]
  19.    
  20.     # pivot column is now found
  21.    
  22.     pivotrow = 0
  23.     ratio = 100**100
  24.     curindex = 0
  25.     for (pivotnum, ans) in [(row[pivotcol], row[len(row)-1]) for row in matrix]:
  26.         if pivotnum > 0:
  27.             if ans / pivotnum < ratio:
  28.                 ratio = ans / pivotnum
  29.                 pivotrow = curindex
  30.         curindex += 1
  31.    
  32.     return (pivotrow, pivotcol)
  33.  
  34. mat = [[ 1,  4,  3,  6,  6,  3,  5,  3,  3],
  35.        [ 1,  4,  3,  2,  5,  6,  7, -3,  2],
  36.        [-3,  0,  1, -4, 12,  1, 10, -5,  2],
  37.        [ 4, -2,  2,  3,  2,  1,  0,  2,  3],
  38.        [ 4,  1,  1,  2,  4,  0,  4,  0, 12],
  39.        [ 4, 12,  4,  2,  6, -1,  9,  1,  2]]
  40.  
  41. (row, col) = select(mat)
  42. print row
  43. print col
Advertisement
Add Comment
Please, Sign In to add comment