diff --git a/solutions/sem02/lesson05/task1.py b/solutions/sem02/lesson05/task1.py index e9c7c3c5..b5a95bb3 100644 --- a/solutions/sem02/lesson05/task1.py +++ b/solutions/sem02/lesson05/task1.py @@ -6,7 +6,10 @@ class ShapeMismatchError(Exception): def can_satisfy_demand( - costs: np.ndarray, - resource_amounts: np.ndarray, - demand_expected: np.ndarray, -) -> bool: ... + costs: np.ndarray, resource_amounts: np.ndarray, demand_expected: np.ndarray +) -> bool: + if resource_amounts.shape[0] != costs.shape[0] or demand_expected.shape[0] != costs.shape[1]: + raise ShapeMismatchError + + total = costs @ demand_expected + return all(total <= resource_amounts) diff --git a/solutions/sem02/lesson05/task2.py b/solutions/sem02/lesson05/task2.py index be1fb9d2..f0fcc9c9 100644 --- a/solutions/sem02/lesson05/task2.py +++ b/solutions/sem02/lesson05/task2.py @@ -8,4 +8,16 @@ class ShapeMismatchError(Exception): def get_projections_components( matrix: np.ndarray, vector: np.ndarray, -) -> tuple[np.ndarray | None, np.ndarray | None]: ... +) -> tuple[np.ndarray | None, np.ndarray | None]: + if matrix.shape[0] != matrix.shape[1] or matrix.shape[1] != vector.size: + raise ShapeMismatchError + + if np.linalg.det(matrix) == 0: + return (None, None) + + mult = matrix @ vector + length = np.sum(matrix**2, axis=1) + coeff = (mult / length)[:, np.newaxis] + projections = coeff * matrix + + return (projections, vector - projections) diff --git a/solutions/sem02/lesson05/task3.py b/solutions/sem02/lesson05/task3.py index 0c66906c..aab86077 100644 --- a/solutions/sem02/lesson05/task3.py +++ b/solutions/sem02/lesson05/task3.py @@ -9,4 +9,10 @@ def adaptive_filter( Vs: np.ndarray, Vj: np.ndarray, diag_A: np.ndarray, -) -> np.ndarray: ... +) -> np.ndarray: + if Vs.shape[0] != Vj.shape[0] or Vj.shape[1] != diag_A.shape[0]: + raise ShapeMismatchError + + Vj_h = np.conj(Vj).transpose() + R_inv = np.linalg.inv(np.eye(Vj.shape[1]) + Vj_h @ Vj @ np.diag(diag_A)) + return Vs - Vj @ (R_inv @ (Vj_h @ Vs))