erster stand :)

This commit is contained in:
Phexi committed 2026-10-22 00:21:46 +02:00
commit c20f8a484e
11 files changed
+196

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
"""Compute module providing matrix multiplication functionality.
This module defines a single public function :func:`matmul`, which performs
standard matrix multiplication for two 2‑D lists.
The function explicitly checks that the number of columns of the first matrix
matches the number of rows of the second matrix. If they are mismatched the
function prints ``"Matrices not compatible :("`` and returns an empty list.
All variables are named distinctly to aid readability and to avoid
conflicts.
"""
__all__: list[str] = ["matmul", "transpose"]
import math
def matmul(
matrix_a: list[list[float]],
matrix_b: list[list[float]],
) -> list[list[float]]:
"""Return the product of two matrices.
Parameters
----------
matrix_a: list[list[float]]
The left‑hand operand, a 2‑D list where each inner list represents a row.
matrix_b: list[list[float]]
The right‑hand operand, also a 2‑D list.
Returns
-------
list[list[float]]
The resulting matrix as a 2‑D list. If the matrices cannot be
multiplied due to incompatible dimensions, an empty list is returned.
Examples
--------
# >>> matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]])
[[19.0, 22.0], [43.0, 50.0]]
"""
if not matrix_a or not matrix_b:
print("Matrices not compatible :(")
return []
rows_a: int = len(matrix_a)
cols_a: int = len(matrix_a[0])
rows_b: int = len(matrix_b)
cols_b: int = len(matrix_b[0])
if cols_a != rows_b:
print("Matrices not compatible :(")
return []
# Initialise result matrix with zeros
result: list[list[float]] = [[0.0 for _ in range(cols_b)] for _ in range(rows_a)]
for i in range(rows_a):
for j in range(cols_b):
sum_val: float = 0.0
for k in range(cols_a):
sum_val += matrix_a[i][k] * matrix_b[k][j]
result[i][j] = sum_val
return result
def get_rotate(matrix: list[list[float]], angle:int) -> list[list[float]]:
"""Apply a rotation matrix to a base matrix.
Parameters
----------
matrix: list[list[float]]
The matrix to be rotated.
angle: int
The angle to be rotated.
Returns
-------
list[list[float]]
Result of *matrix* rotated by *angle*.
"""
rows: int = len(matrix)
cols: int = len(matrix[0])
if cols != 1:
print("Can't rotate this matrix! Wrong amount of columns!")
return [[0, 0],[0, 0]]
if rows == 2:
return [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]]
else:
print("Can't rotate this matrix! Wrong amount of rows!")
return [[0, 0],[0, 0]]
return matmul(rotate_matrix, matrix)
def transpose(matrix: list[list[float]]) -> list[list[float]]:
"""Return the transpose of a 2‑D list.
Parameters
----------
matrix: list[list[float]]
The matrix to transpose.
Returns
-------
list[list[float]]
Transposed matrix.
"""
if not matrix:
return []
rows = len(matrix)
cols = len(matrix[0])
result: list[list[float]] = [[None for _ in range(rows)] for _ in range(cols)]
for i in range(rows):
for j in range(cols):
result[j][i] = matrix[i][j]
return result
if __name__ == "__main__":
print(matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]]))
print(transpose([[1, 2], [3, 4]]))
print(len([[1],[0]]))