Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #reduction_to_canonical_form
- from drawing import ShowMatrixElement, ShowTable
- from CreateW import CreateW, ChangeF
- def SetToCanon (A: int, N: int, M: int, more: bool) -> None:
- for i in range(M):
- for j in range(M):
- if j == i:
- A[i].append( (more-0.5000)*(-2) )
- else:
- A[i].append(0)
- ShowMatrixElement(A, -1, -1)
- def ChooseVariables(A: list, B: list, W: list) -> (int, int):
- #print(W) #отладка
- stolbec_number = 0
- for i in range( len(W) ):
- if W[i] < W[stolbec_number]:
- stolbec_number = i
- #print(W[stolbec_number])#отладка
- for i in range(len(A)):
- stroka_number = i
- if A[i][stolbec_number] != 0:
- break
- indicator = 3**10
- for i in range( len(A) ):
- if not B[i]*A[i][stolbec_number]<0:
- continue
- #if abs( B[i]/A[i][stolbec_number] ) < abs(B[stroka_number]/A[stroka_number][stolbec_number]) :
- if abs( B[i]/A[i][stolbec_number] ) < indicator :
- indicator = abs( B[i]/A[i][stolbec_number] )
- stroka_number = i
- #print(A[stroka_number][stolbec_number])#отладка
- return (stroka_number, stolbec_number)
- def ChangeVariables(A: list, B: list, stroka_number: int, stolbec_number: int, column: list, headers: list, W_or_F: bool, F = None):
- B[stroka_number] = B[stroka_number]/A[ stroka_number ][ stolbec_number ]*-1
- for j in range( len(A[0]) ):
- if (j != stolbec_number):
- A[ stroka_number ][j] = A[ stroka_number ][j]/A[ stroka_number ][ stolbec_number ]*-1
- A[stroka_number][stolbec_number] = 1/A[ stroka_number ][ stolbec_number ]
- for i in range(len(A)):
- if i == stroka_number:
- continue
- else:
- B[i] += B[stroka_number]*A[i][stolbec_number]
- for j in range(len(A[0])):
- if j != stolbec_number:
- A[i][j] += A[i][stolbec_number] * A[stroka_number][j]
- A[i][stolbec_number] *= A[stroka_number][stolbec_number]
- column[stroka_number], headers[stolbec_number] = headers[stolbec_number], column[stroka_number]
- if not W_or_F:
- F = ChangeF( A, B, F, ChooseVariables(A, B, F)[0], ChooseVariables(A, B, F)[1])
- ShowTable(A, B, column, headers, (CreateW(A, B, column, headers) if W_or_F==True else F ) )
- def RemoveExtraVars(A: list, B:list, N: int, column: list, headers: list) -> (list, list, list, list):
- new_A = A.copy()
- new_B = B.copy()
- new_column = column.copy()
- for i in range(len(A)):
- if int(column[i][2]) > N:
- new_A.remove(A[i])
- new_B.remove(B[i])
- new_column.remove(column[i])
- column = new_column.copy()
- B = new_B.copy()
- new_headers = headers.copy()
- new_A = Transpose(new_A)
- A = new_A.copy()
- for j in range(len(A)):
- if headers[j][0] == 'y':
- new_A.remove(A[j])
- new_headers.remove(headers[j])
- A = Transpose(new_A).copy()
- headers = new_headers.copy()
- return (A, B, column, headers)
- def Transpose(A: list):
- A = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
- return A
- def CheckCondition(F: list) -> bool:
- for i in range(len(F)-1):
- if F[i]<0:
- return True
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement