from tkinter import * def matrixprint(matrix): print("____") for i in matrix: for el in…
- User
from tkinter import *
def matrix_print(matrix): print("______") for i in matrix: for el in i: print(el, end=" ") print()
def matrix_metric(matrix): m = len(matrix) n = len(matrix[0]) metric = (m,n) return metric
def transpose_matrix(matrix): metric = matrix_metric(matrix) transposed = [] for i in range(metric[1]): column = [] transposed.append(column)
for i in range(metric[0]): for c in range(len(transposed)): transposed[c].append(matrix[i][c]) return transposeddef minor_matrix_i(matrix,m1,n1): result = [row[:] for row in matrix] result.pop(m1) for m in range(len(result)): result[m].pop(n1) return result
def minor_matrix_ii(matrix): m = matrix_metric(matrix)[0] if m == 3: minorred_matrix = [[] for i in range(matrix_metric(matrix)[0])] for i in range(len(matrix)): for b in range(len(matrix[i])): minorred_matrix[i].append(det_matrix_ii(minor_matrix_i(matrix, i, b))) return minorred_matrix elif m == 2: minorred_matrix = [[0, 0], [0, 0]] minorred_matrix[0][0] = matrix[1][1] minorred_matrix[0][1] = matrix[1][0] minorred_matrix[1][0] = matrix[0][1] minorred_matrix[1][1] = matrix[0][0] return minorred_matrix
def det_matrix_ii(matrix): m_sum = 0 for i in matrix: m_sum += 1 #print('сумма элементов',m_sum) determ_num = 0 for n in range(len(matrix)):
if m_sum == 2: determ_num += matrix[0][n] * (minor_matrix_i(matrix,0,n))[0][0] * algebraic_add(matrix)[0][n] if m_sum == 3: determ_num += matrix[0][n] * det_matrix_ii(minor_matrix_i(matrix, 0, n)) * algebraic_add(matrix)[0][n] return determ_numdef algebraic_add(matrix): algebraic_added = [[((-1)**(2+m+n)) for n in range(matrix_metric(matrix)[0])] for m in range(matrix_metric(matrix)[0])] return algebraic_added
def create_matrix(m, n): matrix = [] for i in range(m): matrix.append([0] * n) return matrix
def create_and_print_matrix(): # Get the matrix dimensions from the entry_fields list m = len(entry_fields) n = len(entry_fields[0])
matrix = create_matrix(m, n) # Get the values from the entry_fields and update the matrix for i in range(m): for j in range(n): entry_value = entry_fields[i][j].get() try: matrix[i][j] = int(entry_value) except ValueError: # Handle non-integer values entered in the entry fields print(f"Invalid value entered at row {i+1}, column {j+1}") # Display the matrix return matrixdef add_or_remove_row(): metric_sum = len(entry_fields) if metric_sum == 2: entry_fields.append([]) for j in range(len(entry_fields[0])): entry = Entry(root, width=5) entry.grid(row=len(entry_fields), column=j+1) entry_fields[-1].append(entry) elif metric_sum == 3: if len(entry_fields) > 2: for entry in entry_fields[-1]: entry.destroy() entry_fields.pop()
def add_or_remove_column(): metric_sum = len(entry_fields[0]) if metric_sum == 2: for row in entry_fields: entry = Entry(root, width=5) entry.grid(row=entry_fields.index(row)+1, column=len(row)+1) row.append(entry) elif metric_sum > 2: for row in entry_fields: if len(row) > 2: entry = row[-1] entry.destroy() row.pop()
- Assistant
To optimize the program, you can:
- Avoid unnecessary calculations and store the results of repeated calculations to reduce computation time.
- Implement error handling for invalid input values, such as non-integer entries in the matrix.
- Use more efficient algorithms, such as Strassen's algorithm, for matrix multiplication and determinant calculation.
- Restructure the code to remove redundant functions and improve readability.
- Implement multithreading or parallel processing techniques for computationally intensive operations.
- Use data structures and algorithms optimized for matrix operations, such as NumPy and SciPy libraries in Python.
- Minimize the number of tkinter widgets by dynamically creating and removing them as needed.
- Implement a more efficient data structure for storing matrices, such as a sparse matrix representation, if applicable.
- Cache results of expensive calculations to avoid repeating them when possible.
- Use generators or iterators instead of creating and storing the complete matrix in memory if you only need to iterate over it.