Advertisement
Oppaceted

reduction_to_canonical_form

May 27th, 2024
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #reduction_to_canonical_form
  2. from drawing import ShowMatrixElement, ShowTable
  3. from CreateW import CreateW, ChangeF
  4.  
  5. def SetToCanon (A: int, N: int, M: int, more: bool) -> None:
  6.     for i in range(M):
  7.         for j in range(M):
  8.             if j == i:
  9.                 A[i].append( (more-0.5000)*(-2) )
  10.             else:
  11.                 A[i].append(0)
  12.     ShowMatrixElement(A, -1, -1)
  13.  
  14. def ChooseVariables(A: list, B: list, W: list) -> (int, int):
  15.     #print(W) #отладка
  16.     stolbec_number = 0
  17.     for i in range( len(W) ):
  18.         if W[i] < W[stolbec_number]:
  19.             stolbec_number = i
  20.     #print(W[stolbec_number])#отладка
  21.     for i in range(len(A)):
  22.         stroka_number = i
  23.         if A[i][stolbec_number] != 0:
  24.             break
  25.     indicator = 3**10
  26.     for i in range( len(A) ):
  27.         if not B[i]*A[i][stolbec_number]<0:
  28.             continue
  29.         #if abs( B[i]/A[i][stolbec_number] ) < abs(B[stroka_number]/A[stroka_number][stolbec_number]) :
  30.         if abs( B[i]/A[i][stolbec_number] ) < indicator :
  31.             indicator = abs( B[i]/A[i][stolbec_number] )
  32.             stroka_number = i
  33.     #print(A[stroka_number][stolbec_number])#отладка
  34.     return (stroka_number, stolbec_number)
  35.  
  36. def ChangeVariables(A: list, B: list, stroka_number: int, stolbec_number: int, column: list, headers: list, W_or_F: bool, F = None):
  37.     B[stroka_number] = B[stroka_number]/A[ stroka_number ][ stolbec_number ]*-1
  38.     for j in range( len(A[0]) ):
  39.         if (j != stolbec_number):
  40.             A[ stroka_number ][j] = A[ stroka_number ][j]/A[ stroka_number ][ stolbec_number ]*-1
  41.     A[stroka_number][stolbec_number] = 1/A[ stroka_number ][ stolbec_number ]
  42.    
  43.     for i in range(len(A)):
  44.         if i == stroka_number:
  45.             continue
  46.         else:
  47.             B[i] += B[stroka_number]*A[i][stolbec_number]
  48.             for j in range(len(A[0])):
  49.                 if j != stolbec_number:
  50.                     A[i][j] += A[i][stolbec_number] * A[stroka_number][j]
  51.             A[i][stolbec_number] *= A[stroka_number][stolbec_number]    
  52.  
  53.     column[stroka_number], headers[stolbec_number] = headers[stolbec_number], column[stroka_number]
  54.     if not W_or_F:
  55.         F = ChangeF( A, B, F, ChooseVariables(A, B, F)[0], ChooseVariables(A, B, F)[1])
  56.     ShowTable(A, B, column, headers, (CreateW(A, B, column, headers) if W_or_F==True else F ) )
  57.  
  58. def RemoveExtraVars(A: list, B:list, N: int, column: list, headers: list) -> (list, list, list, list):
  59.     new_A = A.copy()
  60.     new_B = B.copy()
  61.     new_column = column.copy()
  62.     for i in range(len(A)):
  63.         if int(column[i][2]) > N:
  64.             new_A.remove(A[i])
  65.             new_B.remove(B[i])
  66.             new_column.remove(column[i])
  67.     column = new_column.copy()
  68.     B = new_B.copy()
  69.  
  70.     new_headers = headers.copy()
  71.     new_A = Transpose(new_A)
  72.     A = new_A.copy()
  73.     for j in range(len(A)):
  74.         if headers[j][0] == 'y':
  75.             new_A.remove(A[j])
  76.             new_headers.remove(headers[j])
  77.     A = Transpose(new_A).copy()
  78.     headers = new_headers.copy()
  79.     return (A, B, column, headers)
  80. def Transpose(A: list):
  81.     A = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
  82.     return A
  83. def CheckCondition(F: list) -> bool:
  84.     for i in range(len(F)-1):
  85.         if F[i]<0:
  86.             return True
  87.     return False
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement