Advertisement
Oppaceted

reduction_to_canonical_form

May 26th, 2024
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 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.     M = len(A) # количество уравнений/строк
  16.     N = len(A[0]) # количество переменных/столбцов
  17.     #print(W) #отладка
  18.     stolbec_number = 0
  19.     for i in range( len(W) ):
  20.         if W[i] < W[stolbec_number]:
  21.             stolbec_number = i
  22.     print(W[stolbec_number])#отладка    
  23.     for i in range(len(A)):
  24.         stroka_number = i
  25.         if A[i][stolbec_number] != 0:
  26.             break
  27.     indicator = 3**10
  28.     for i in range( len(A) ):
  29.         if not B[i]*A[i][stolbec_number]<0:
  30.             continue
  31.         #if abs( B[i]/A[i][stolbec_number] ) < abs(B[stroka_number]/A[stroka_number][stolbec_number]) :
  32.         if abs( B[i]/A[i][stolbec_number] ) < indicator :
  33.             indicator = abs( B[i]/A[i][stolbec_number] )
  34.             stroka_number = i
  35.     print(A[stroka_number][stolbec_number])#отладка
  36.     return (stroka_number, stolbec_number)
  37.  
  38. def ChangeVariables(A: list, B: list, stroka_number: int, stolbec_number: int, column: list, headers: list, W_or_F: bool, F = None):
  39.     B[stroka_number] = B[stroka_number]/A[ stroka_number ][ stolbec_number ]*-1
  40.     for j in range( len(A[0]) ):
  41.         if (j != stolbec_number):
  42.             A[ stroka_number ][j] = A[ stroka_number ][j]/A[ stroka_number ][ stolbec_number ]*-1
  43.     A[stroka_number][stolbec_number] = 1/A[ stroka_number ][ stolbec_number ]
  44.    
  45.     for i in range(len(A)):
  46.         if i == stroka_number:
  47.             continue
  48.         else:
  49.             B[i] += B[stroka_number]*A[i][stolbec_number]
  50.             for j in range(len(A[0])):
  51.                 if j != stolbec_number:
  52.                     A[i][j] += A[i][stolbec_number] * A[stroka_number][j]
  53.                 #A[i][stolbec_number] *= A[stroka_number][stolbec_number]
  54.             A[i][stolbec_number] *= A[stroka_number][stolbec_number]    
  55.            
  56.    
  57.  
  58.     column[stroka_number], headers[stolbec_number] = headers[stolbec_number], column[stroka_number]
  59.     #ShowTable(A, B, column, headers, CreateW(A, B, column, headers))
  60.     if not W_or_F:
  61.         F = ChangeF( A, B, F, ChooseVariables(A, B, F)[0], ChooseVariables(A, B, F)[1])
  62.     ShowTable(A, B, column, headers, (CreateW(A, B, column, headers) if W_or_F==True else F ) )
  63.  
  64. def RemoveExtraVars(A: list, B:list, N: int, column: list, headers: list) -> (list, list, list, list):
  65.     new_A = A.copy()
  66.     new_B = B.copy()
  67.     new_column = column.copy()
  68.     for i in range(len(A)):
  69.         if int(column[i][2]) > N:
  70.             new_A.remove(A[i])
  71.             new_B.remove(B[i])
  72.             new_column.remove(column[i])
  73.     column = new_column.copy()
  74.     B = new_B.copy()
  75.  
  76.     new_headers = headers.copy()
  77.     new_A = Transpose(new_A)
  78.     A = new_A.copy()
  79.     for j in range(len(A)):
  80.         if headers[j][0] == 'y':
  81.             new_A.remove(A[j])
  82.             new_headers.remove(headers[j])
  83.     A = Transpose(new_A).copy()
  84.     headers = new_headers.copy()
  85.     return (A, B, column, headers)
  86.     #
  87.     '''
  88.    A = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
  89.    new_A = A.copy()
  90.    new_headers = headers.copy()
  91.    for j in range(len(A)):
  92.        if headers[j][0] == 'y':
  93.            new_A.remove(A[i])
  94.            new_headers.remove(headers[i])
  95.    A = new_A.copy()
  96.    A = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
  97.    headers = new_headers.copy()
  98.    '''
  99.     '''
  100.    for i in :
  101.        if int(i[2]) > N:
  102.            A.pop(column.index(i))
  103.            B.pop(column.index(i))
  104.            column.remove(i)
  105.            '''
  106. '''
  107.    for l in A: #иногда столбцы не удаляются, поэтому нужно пройти ещё раз
  108.        for j in headers:
  109.            if j[0] == 'y':
  110.                for i in A:
  111.                    i.pop(headers.index(j))
  112.                headers.remove(j)
  113.                '''
  114. def Transpose(A: list):
  115.     A = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
  116.     return A
  117. def CheckCondition(F: list) -> bool:
  118.     for i in range(len(F)-1):
  119.         if F[i]<0:
  120.             return True
  121.     return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement