Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions solutions/sem02/lesson05/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 13 additions & 1 deletion solutions/sem02/lesson05/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 7 additions & 1 deletion solutions/sem02/lesson05/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Loading