2024-10-20 17:47:37 +02:00
|
|
|
import math
|
2024-10-25 17:01:19 +02:00
|
|
|
from abc import ABC, abstractmethod
|
2024-10-20 17:47:37 +02:00
|
|
|
|
2024-10-25 17:01:19 +02:00
|
|
|
class LinearAlgebraUtils(ABC):
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def dot_product(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def matrix_vector_multiply(A, x):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def vector_norm(v):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def vector_scalar_divide(x, scalar):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def matrix_scalar_multiply(A, w):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def vector_vector_subtraction(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def vector_vector_addition(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def scalar_matrix_multiply(omega, vector):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def matrix_norm(A):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def matrix_matrix_subtraction(A, B):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def gaussian_elimination(A, b):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SequentialLinearAlgebraUtils(ABC):
|
2024-10-20 16:30:42 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def dot_product(v1, v2):
|
|
|
|
|
return sum(x*y for x, y in zip(v1, v2))
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_vector_multiply(A, x):
|
2024-10-25 17:01:19 +02:00
|
|
|
return [SequentialLinearAlgebraUtils.dot_product(row, x) for row in A]
|
2024-10-20 19:06:15 +02:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_norm(v):
|
|
|
|
|
return sum(x*x for x in v)**0.5
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_scalar_divide(x, scalar):
|
|
|
|
|
return [xi / scalar for xi in x]
|
2024-10-20 16:30:42 +02:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-20 17:47:37 +02:00
|
|
|
def matrix_scalar_multiply(A, w):
|
|
|
|
|
return [[w * A[i][j] for j in range(len(A[0]))] for i in range(len(A))]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_vector_subtraction(v1, v2):
|
2024-10-20 16:30:42 +02:00
|
|
|
return [x-y for x, y in zip(v1, v2)]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-20 17:47:37 +02:00
|
|
|
def vector_vector_addition(v1, v2):
|
2024-10-20 16:30:42 +02:00
|
|
|
return [x+y for x, y in zip(v1, v2)]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-20 17:47:37 +02:00
|
|
|
def scalar_matrix_multiply(omega, vector):
|
2024-10-20 16:30:42 +02:00
|
|
|
return [omega * x for x in vector]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-20 17:47:37 +02:00
|
|
|
def matrix_norm(A):
|
|
|
|
|
return math.sqrt(sum(sum(element ** 2 for element in row) for row in A))
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_matrix_subtraction(A, B):
|
|
|
|
|
return [[A[i][j] - B[i][j] for j in range(len(A[0]))] for i in range(len(A))]
|
2024-10-20 19:19:31 +02:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def gaussian_elimination(A, b):
|
|
|
|
|
n = len(A)
|
|
|
|
|
M = [row[:] for row in A]
|
|
|
|
|
|
|
|
|
|
for i in range(n):
|
|
|
|
|
M[i].append(b[i])
|
|
|
|
|
|
|
|
|
|
for k in range(n):
|
|
|
|
|
if M[k][k] == 0:
|
|
|
|
|
for i in range(k + 1, n):
|
|
|
|
|
if M[i][k] != 0:
|
|
|
|
|
M[k], M[i] = M[i], M[k]
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
for i in range(k + 1, n):
|
|
|
|
|
factor = M[i][k] / M[k][k]
|
|
|
|
|
for j in range(k, n + 1):
|
|
|
|
|
M[i][j] -= factor * M[k][j]
|
|
|
|
|
|
|
|
|
|
x = [0] * n
|
|
|
|
|
for i in range(n - 1, -1, -1):
|
|
|
|
|
x[i] = M[i][-1] / M[i][i]
|
|
|
|
|
for k in range(i - 1, -1, -1):
|
|
|
|
|
M[k][-1] -= M[k][i] * x[i]
|
|
|
|
|
|
|
|
|
|
return x
|
2024-10-25 17:01:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadsLinearAlgebraUtils(ABC):
|
|
|
|
|
@staticmethod
|
|
|
|
|
def dot_product(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_vector_multiply(A, x):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_norm(v):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_scalar_divide(x, scalar):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_scalar_multiply(A, w):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_vector_subtraction(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def vector_vector_addition(v1, v2):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def scalar_matrix_multiply(omega, vector):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_norm(A):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def matrix_matrix_subtraction(A, B):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def gaussian_elimination(A, b):
|
|
|
|
|
pass
|