76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
|
import numpy as np
|
|||
|
import threading
|
|||
|
import time
|
|||
|
|
|||
|
|
|||
|
def determinant_gauss(matrix):
|
|||
|
"""Вычисление детерминанта методом Гаусса"""
|
|||
|
matrix_copy = matrix.astype(np.float64)
|
|||
|
n = matrix_copy.shape[0]
|
|||
|
det = 1.0
|
|||
|
|
|||
|
for i in range(n):
|
|||
|
if matrix_copy[i, i] == 0:
|
|||
|
for j in range(i + 1, n):
|
|||
|
if matrix_copy[j, i] != 0:
|
|||
|
matrix_copy[[i, j]] = matrix_copy[[j, i]]
|
|||
|
det *= -1
|
|||
|
break
|
|||
|
det *= matrix_copy[i, i]
|
|||
|
matrix_copy[i, i:] /= matrix_copy[i, i]
|
|||
|
|
|||
|
for j in range(i + 1, n):
|
|||
|
factor = matrix_copy[j, i]
|
|||
|
matrix_copy[j, i:] -= factor * matrix_copy[i, i:]
|
|||
|
|
|||
|
return det
|
|||
|
|
|||
|
|
|||
|
def determinant_parallel(matrix, num_threads=2):
|
|||
|
"""Параллельное вычисление детерминанта с использованием потоков"""
|
|||
|
|
|||
|
def compute_row(row, matrix_copy):
|
|||
|
n = matrix_copy.shape[0]
|
|||
|
for i in range(row, n, num_threads):
|
|||
|
for j in range(i + 1, n):
|
|||
|
if matrix_copy[i, i] == 0:
|
|||
|
continue
|
|||
|
factor = matrix_copy[j, i] / matrix_copy[i, i]
|
|||
|
matrix_copy[j, i:] -= factor * matrix_copy[i, i:]
|
|||
|
|
|||
|
matrix_copy = matrix.astype(np.float64)
|
|||
|
|
|||
|
threads = []
|
|||
|
for i in range(num_threads):
|
|||
|
t = threading.Thread(target=compute_row, args=(i, matrix_copy))
|
|||
|
threads.append(t)
|
|||
|
t.start()
|
|||
|
|
|||
|
for t in threads:
|
|||
|
t.join()
|
|||
|
|
|||
|
return matrix_copy[-1, -1]
|
|||
|
|
|||
|
|
|||
|
def benchmark(sizes):
|
|||
|
for size in sizes:
|
|||
|
matrix = np.random.randint(1, 11, (size, size))
|
|||
|
|
|||
|
start_time = time.time()
|
|||
|
det_regular = determinant_gauss(matrix)
|
|||
|
end_time = time.time()
|
|||
|
regular_time = end_time - start_time
|
|||
|
|
|||
|
start_time = time.time()
|
|||
|
det_parallel = determinant_parallel(matrix, num_threads=4)
|
|||
|
end_time = time.time()
|
|||
|
parallel_time = end_time - start_time
|
|||
|
|
|||
|
print(f"Размер матрицы: {size}x{size}")
|
|||
|
print(f"Детерминант (последовательно): {det_regular} | Время: {regular_time} секунд")
|
|||
|
print(f"Детерминант (параллельно): {det_parallel} | Время: {parallel_time} секунд")
|
|||
|
print("-" * 50)
|
|||
|
|
|||
|
|
|||
|
benchmark([100, 300, 500])
|